Go: package main

import (
	"bufio"
	"os"
	"fmt"
	"strconv"
)

const max int = 1000 + 1
var A [][]int

func lcs(x, y string) int {
	x = " " + x
	y = " " + y
	for i:=0;i<len(x); i++ {
		for j:=0;j<len(y); j++ {
			A[i][j] = 0
		}
	}
	for i:=1; i<len(x); i++ {
		for j:=1; j<len(y); j++ {
			if x[i] == y[j] {
				A[i][j] = A[i-1][j-1] + 1
			} else if A[i-1][j] > A[i][j-1] {
				A[i][j] = A[i-1][j]
			} else {
				A[i][j] = A[i][j-1]
			}
		}
	}
	return A[len(x)-1][len(y)-1]
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num 
}

func nextStr(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}


func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	A = make([][]int, max)
	for i := range A {
		A[i] = make([]int, max)
	}
	n := nextInt(scanner)
	for i:=0; i<n; i++ {
		x := nextStr(scanner)
		y := nextStr(scanner)
		fmt.Printf("%d\n", lcs(x, y))
	}
}

C++: #include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int q;

  	cin >> q;
	for(int i=0;i<q;i++){
		string x, y;
		cin >> x >> y;
        int n, m;
        n = x.size();
        m = y.size();
		int dp[n+1][m+1];
		for(int j=0;j<=n;j++){
          for(int k=0;k<=m;k++){
			dp[j][k] = 0;
			}
		}
		
		for(int j=0;j<n;j++){
			for(int k=0;k<m;k++){
				if(x[j]==y[k]){
					dp[j+1][k+1] = max(dp[j][k+1], max(dp[j+1][k], dp[j][k]+1));
				}else{
					dp[j+1][k+1] = max(dp[j][k+1], max(dp[j+1][k], dp[j][k]));
				}
			}
		}
		
		
		
		cout << dp[n][m] <<endl;
  
	}
}

C++: #include <algorithm>
#include <iostream>
#include <string>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <limits>
#include <map>
#include <queue>
#include <stack>
#include <vector>

#define FOR(i,k,n) for(int (i) = (k); (i) < (n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define all(v) begin(v), end(v)
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x,y) cerr << #x << ": " << x << ", " << #y << ": " << y << endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<i_i, int> p_i;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
template<class T> using vv=vector<vector<T>>;
typedef deque<int> di;
typedef deque<deque<int>> ddi;


int main() {
    int q;
    cin >> q;
    rep(i,q) {
        string x,y;
        cin >> x >> y;
        int dp[1050][1050] = {};
        rep(i,(int)x.length()) {
            rep(j,(int)y.length()) {
                if(i == 0 || j == 0) {
                    if(x[i] == y[j]) {
                        dp[i][j] = 1;
                    }
                } else if(x[i] == y[j]) {
                    dp[i][j] = dp[i-1][j-1] +1;
                } else {
                    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        cout << dp[x.length()-1][y.length()-1] << endl;
    }
	return 0;
}

C++: #include"bits/stdc++.h"

using namespace std;

#define rep(i,n) for(int i=0;i<(int)(n);i++)
void sort2array(double *a,long *b,long n){
    for(int i=0;i<n;i++){
        b[i]=i;
    }
    sort(b,b+n,[a](long i,long j)->bool {return a[i]<a[j];} );
}
//bを何回足せばaを超えるか
int wtover(int a,int b){
    if(a%b>0)return (a/b)+1;
    else return a/b;
}
const int INF=100000001,ID_MAX=20;
int bi_e[ID_MAX]={0};
//2進数表示したときの最高桁
int bi_max(long n){
    int m=0;
    for(m;pow(2,m)<=n;m++);m=m-1;
    return m;
}
//bi_eに二進数表示したやつを代入
void bi_exs(long n){
    rep(i,ID_MAX){
        bi_e[i]=0;
    }
    if(n<pow(2,ID_MAX)){
        int m=bi_max(n);
        long l;
        for(m;m>-1;m--){
            l=pow(2,m);
            bi_e[m]=n/l;
            n=n%l;
        }
    }
}

const int N_MAX=1001,Q_MAX=150;
char X[Q_MAX][N_MAX],Y[Q_MAX][N_MAX],x[N_MAX],y[N_MAX];
int ans[Q_MAX],q,dp[N_MAX][N_MAX];

int rec(int i,int j,int k){
    if(dp[i][j]>=0)return dp[i][j];
    int res;
    if(i==0||j==0){
        res=0;
    }else if(X[k][i-1]==Y[k][j-1]){
        res=rec(i-1,j-1,k)+1;
    }else{
        res=max(rec(i-1,j,k),rec(i,j-1,k));
    }
    return dp[i][j]=res;
}

int main(){
    int i;
    //入力
    cin>>q;
    rep(i,q){
        cin>>X[i];
        cin>>Y[i];
    }
    //処理
    rep(i,q){
        memset(dp,-1,sizeof(dp));
        cout<<rec(strlen(X[i]),strlen(Y[i]),i)<<endl;
    }
    //出力
}

C++: #include <stdio.h>
#include <string.h>
#define N 1010
char a[N];
char b[N];
int dp[N][N];
//int LCS(int i, int j){
//    if(i == -1 || j == -1) return 0;
//    if(a[i] == b[j]) return LCS(i-1, j-1) + 1;
//    else return LCS(i-1, j) > LCS(i, j-1) ? LCS(i-1, j) : LCS(i, j-1);
//}
int main(){
    int k;
    scanf("%d", &k);
    while(k--){
        scanf("%s", a);
        scanf("%s", b);
        int n = strlen(a);
        int m = strlen(b);
        for(int i = n-1; i >= 0; i--) a[i+1] = a[i];
        for(int i = m-1; i >= 0; i--) b[i+1] = b[i];
        int max = n > m ? n : m;
        for(int i = 0; i <= max; i++) dp[0][i] = dp[i][0] = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                if(a[i] == b[j]) dp[i][j] = dp[i-1][j-1] + 1;
                else {
                    dp[i][j] = dp[i-1][j] > dp[i][j-1] ? dp[i-1][j] : dp[i][j-1];
                }
            }
        }
        printf("%d\n", dp[n][m]);
        //printf("%d\n", LCS(strlen(a)-1, strlen(b)-1));
    }
    return 0;
}

C++: #include<cstdio>
#include<iostream>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define _USE_MATH_DEFINES // M_PI????????????????????????
#include<math.h>
#pragma warning(disable:4996)
using namespace std;

static const int N = 1000;

int lcs(string X,string Y){
	int c[N+1][N+1];
	int m = X.size();
	int n = Y.size();
	int maxl=0;

	X = ' ' + X; // X[0]?????????????????\
	Y = ' ' + Y; // Y[0]?????????????????\

	for(int i=1; i<=m; i++){
		for(int j=1; j<=n; j++){
			if( X[i]==Y[i] ){
				c[i][j] = c[i-1][j-1] + 1;
			}else{
				c[i][j] = max(c[i-1][j], c[i][j-1]);
			}
			maxl = max(maxl, c[i][j]);
		}
	}
	return maxl;
}

int main(){
	string s1,s2;
	int n;
	cin >> n;
	for(int i=0; i<n; i++){
		cin >> s1 >> s2;
		cout << lcs(s1,s2) << "\n";
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i=0;i<(n);++i)
#define per(i, n) for(int i=(n)-1;i>=0;--i)
#define repa(i, n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
const ll MOD = (ll)1e9+7;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
using namespace std;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}

ll modpow(ll x, ll b){
	ll res = 1;
	while(b){
		if(b&1)res = res * x % MOD;
		x = x * x % MOD;
		b>>=1;
	}
	return res;
}

ll modinv(ll x){
	return modpow(x, MOD-2);
}

string str1;
string str2;
int len1;
int len2;

vvector<int> dp;

int func(int p1, int p2){
	if(p1==len1||p2==len2)return 0;
	if(dp[p1][p2]>=0)return dp[p1][p2];
	int res = 0;
	if(str1[p1]==str2[p2])res = 1 + func(p1+1, p2+1);
	chmax(res, func(p1+1, p2));
	chmax(res, func(p1, p2+1));
	return dp[p1][p2] = res;
}

int main(){
	int n;
	cin >> n;
	rep(i, n){
		cin >> str1 >> str2;
		len1 = str1.size();
		len2 = str2.size();
		dp.assign(len1, vector<int>(len2, -1));
		cout << func(0, 0) << endl;
	}
	return 0;
}