Go: package main

import (
	. "fmt"
    "math"
)

// func init() { Println("----------------------------------------") }

func main() {
    const L_MAX = 1003

	var q int
	Scanf("%d", &q)
	X := make([]string, 0, 150)
	Y := make([]string, 0, 150)
	for i := 0; i < 2 * q; i++ {
	    var tmp string
	    Scanf("%s", &tmp)

	    if i % 2 == 0 {
	        X = append(X, tmp)
	    } else {
	        Y = append(Y, tmp)
	    }
	}

    for i := 0; i < q; i++ {
        dp := make([][]int, L_MAX)
        for k, _ := range dp {
            dp[k] = make([]int, L_MAX)
        }

        x, y := X[i], Y[i]

        // init
        isXFound := false
        for c := 0; c < len(x); c++ {
            if isXFound || y[0] == x[c] {
                dp[0][c] = 1
                isXFound = true
            }
        }
        isYFound := false
        for r := 0; r < len(y); r++ {
            if isYFound || x[0] == y[r] {
                dp[r][0] = 1
                isYFound = true
            }
        }

        for r := 1; r < len(y); r++ {
            for c := 1; c < len(x); c++ {
                // dp[r][c]
                if x[c] == y[r] {
                    dp[r][c] = int(math.Max(float64(dp[r - 1][c - 1] + 1), float64(dp[r][c])))
                }

                dp[r][c] = int(math.Max(float64(dp[r][c]), math.Max(float64(dp[r - 1][c]), float64(dp[r][c - 1]))))
            }
        }
        Println(dp[len(y) - 1][len(x) - 1])

        // LCSも求めておく
        // ans := ""
        // r, c := len(y) - 1, len(x) - 1
        // for r >= 0 && c >= 0 {
        //     if y[r] == x[c] {
        //         ans = string(x[c]) + ans
        //         r--
        //         c--
        //     } else {
        //         if dp[r - 1][c] > dp[r][c - 1] {
        //             r--
        //         } else {
        //             c--
        //         }
        //     }
        // }
        // Println(ans)
    }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int dp[1002][1002];

int main(){
	int n;cin >> n;
	string x, y;
	while(n--){
		fill((int*)dp, (int*)(dp+1002), 0);
		cin >> x >> y;
		for(int i = 1;i <= x.size();i++){
			for(int j = 1;j <= y.size();j++){
				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.size()][y.size()] << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;
#define fordebug int hoge;cin>>hoge;
#define DEKAI 1000000007
#define INF (1<<28)
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)

		int dp[1010][1010];
int main(){
	int n;
	cin>>n;
	lp(z,n){
		string s1,s2;
		cin>>s1>>s2;
		lp(i,s1.size()+1){
			lp(j,s2.size()+1){
				dp[i][j]=0;
			}
		}
		lp(i,s1.size()){
			lp(j,s2.size()){
				dp[i+1][j+1]=max(dp[i+1][j+1],max(dp[i+1][j],dp[i][j+1]));
				if(s1[i]==s2[j]){
					dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+1);
				}
			}
		}
		cout<<dp[s1.size()][s2.size()]<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

const int MAX = 1010;
int dp[MAX][MAX];

int main()
{
  int n; cin >> n;
  while(n--) {
    string s1, s2; cin >> s1 >> s2;
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < s1.size(); i++)
      for (int j = 0; j < s2.size(); j++)
        if (s1[i] == s2[j])
          dp[i+1][j+1] = dp[i][j] + 1;
        else
          dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
    cout << dp[s1.size()][s2.size()] << endl;
  }
  return EXIT_SUCCESS;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>
#include <iomanip>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL<<60;
const double PI=3.14159265358979323846;
const int NMAX=100005;
using namespace std;


void solve(string x, string y){
    int lx = x.length();
    int ly = y.length();
    int dp[1001][1001];
    for(int i = 0; i < 1001; i++){
        for(int j = 0; j < 1001; j++){
            dp[i][j] = 0;
        }
    }

    for(int i = 1; i <= lx; i++){
        for(int j = 1; j <= ly; j++){
            if(x[i-1] == y[j-1]){
                dp[i][j] = max(dp[i][j-1],dp[i-1][j-1]+1);
                dp[i][j] = max(dp[i][j],dp[i-1][j]);
            } else {
                dp[i][j] = max(dp[i][j-1],dp[i-1][j-1]);
                dp[i][j] = max(dp[i][j],dp[i-1][j]);
            }
        }
    }

    cout << dp[lx][ly] << endl;
}

int main(){
    int q;
    string x,y;
    cin >> q;
    for(int i = 0; i < q; i++){
        cin >> x >> y;
        solve(x,y);
    }

    return 0;

}

C++: #include<iostream>
#include<string>
using namespace std;

#if 0
所用時間:0.5h

感想:

#endif

int solve(string X, string Y){
  int rows = Y.size();
  int cols = X.size();
  int L[1001][1001] = {}; //L[y][x]の初期化

  //テーブルLの更新
  for(int r = 1; r <= rows; ++r){
    for(int c = 1; c <= cols; ++c){
      if(X[c] == Y[r]) L[r][c] = 1 + L[r-1][c-1];
      else L[r][c] = max(L[r][c-1], L[r-1][c]);
    }
  }

  // //テーブルの表示
  // cout << "Ltable:" << endl;
  // for(int row=0; row <= rows; ++row){
  //   for(int col = 0; col <= cols; ++col){
  //     cout << L[row][col];
  //   }
  //   cout << endl;
  // }

  return L[rows][cols];
}

int main(){
  int q;
  string X, Y;
  cin >> q;

  for(int i = 0; i < q; ++i){
    cin >> X >> Y;

    int ans = solve(X, Y);

    cout << ans << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;

int lcs(string s1, string s2) {
    int n = s1.size(), m = s2.size();

    int c[n + 1][m + 1];
    for (int i = 0; i <= n; i++)
    {
        c[i][0] = 0;
    }
    for (int i = 0; i <= m; i++) {
        c[0][i] = 0;
    }

    // indexを合わせるため、各文字列に空白を挿入
    s1 = ' ' + s1;
    s2 = ' ' + s2;

    int maxl=0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++) {
            if (s1.at(i) == s2.at(j))
                c[i][j] = 1 + c[i - 1][j - 1];
            else
            {
                c[i][j] = max(c[i - 1][j], c[i][j - 1]);
            }
            maxl = max(maxl, c[i][j]);
        }
    }
    return maxl;
}

int main() {
    int q;
    cin >> q;

    for (int i = 0; i < q; i++)
    {
        string s1, s2;
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
}

C++: def lcs(str_a,str_b):    
    if len(str_a) == 0 or len(str_b) == 0:
        return 0
    dp = [[0 for kb in range(len(str_b) + 1)] for ka in range(len(str_a) + 1)]
    for i in range(1, len(str_a) + 1):
        for j in range(1, len(str_b) + 1):
            if str_a[i-1] == str_b[j-1]:
                dp[i][j] = dp[i-1][j-1] + 1
            else:
                dp[i][j] = max([dp[i-1][j], dp[i][j-1]])
  #print ("length of LCS is :",dp[len(str_a)][len(str_b)])
    return dp[len(str_a)][len(str_b)]

n=int(input())
for i in range(n):
    s1=input()
    s2=input()
    print(lcs(s1,s2))

C++: #include <bits/stdc++.h>
#define REP0(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define REPB0(i,n) for(int i=n-1;i>=0;i--)
#define REPB1(i,n) for(int i=n;i>0;i--)
#define llong long long
#define INF 2000000000
#define NIL -1
#define N 1000
using namespace std;
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;
  Y=' '+Y;
  C[0][0]=0;
  REP1(i,m) C[i][0]=0;
  REP1(i,n) C[0][i]=0;
  REP1(i,m){
    REP1(j,n){
      if(X[i]==Y[j]) 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(){
  char a[1000],b[1000];
  string s1,s2;
  int n;
  scanf("%d",&n);
  REP0(i,n){

    scanf("%s %s",a,b);
    s1=a;s2=b;
    printf("%d\n",LCS(s1,s2));
  }
  return 0;
}