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 <bits/stdc++.h>
using namespace std;
int longest_common_subsequence_length(vector<int> &A, vector<int> &B){
	vector<vector<int>> dp(A.size() + 1, vector<int>(B.size() + 1, 0));
	for (int i = 0; i < A.size(); i++){
		for (int j = 0; j < B.size(); j++){
			if (A[i] == B[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]);
			}
		}
	}
	return dp[A.size()][B.size()];
}
int main(){
  int q;
  cin >> q;
  for (int i = 0; i < q; i++){
      string X, Y;
      cin >> X >> Y;
      vector<int> A(X.begin(), X.end());
      vector<int> B(Y.begin(), Y.end());
      cout << longest_common_subsequence_length(A, B) << endl;
  }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
    using namespace std;
int lcs(string x,string y){
    int c[1010][1010];
    int m = x.size();
    int n = y.size();
    int maxl=0;
    x = ' '+x;
    y = ' '+y;
    for (int i=1; i<=m; i++) {
        c[i][0] = 0;
    }
    for(int j=1;j<=n;j++){
        c[0][j] = 0;
    }

    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            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(){
    int q;
    string x;
    string y;
    scanf("%d",&q);
    for (int i=0; i<q; i++) {
        cin>>x>>y;
        cout<<lcs(x, y)<<endl;
    }
    return 0;
}

C++: #include "bits/stdc++.h"
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;
	Y=' '+Y;
	for (int i = 1; i <= m; ++i) {c[i][0]=0;	}
	for (int j = 1; j <= n; ++j) {c[0][j]=0;	}
	
	for (int i = 1; i <= m; ++i) {
		for (int j = 1; j < n; ++j) {
			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(){
string s1,s2;
int n;cin>>n;
for (int i = 0; i < n; ++i) {
	cin >>s1>>s2;
	cout<<lcs(s1,s2)<<endl;
}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[2][1010];
int main(){
  int q;
  cin>>q;
  for(int cn=0;cn<q;cn++){
    string x,y;
    cin>>x>>y;
    memset(dp,0,sizeof(dp));
    for(int i=0;i<x.size();i++){
      int nw=i%2;
      int nt=!nw;
      for(int k=0;k<1010;k++)dp[nt][k]=0;
      for(int j=0;j<y.size();j++){
	if(j)dp[nw][j]=max(dp[nw][j],dp[nw][j-1]);
	if(x[i]==y[j])dp[nt][j+1]=max(dp[nt][j+1],dp[nw][j]+1);
	dp[nt][j]=max(dp[nt][j],dp[nw][j]);
      }
    }
    int ans=0;
    for(int i=0;i<1010;i++)ans=max(ans,dp[x.size()%2][i]);
    cout<<ans<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++)
#define PRINT(x) cout<<(x)<<endl
#define INF 1000000000000 // 10^12
#define MOD 1000000007 // 10^9+7
#define PB push_back
#define MP make_pair

const int MAX_X = 1000, MAX_Y = 1000;

int q;
string X, Y;

int dp[MAX_X + 1][MAX_Y + 1];

int main() {
    cin >> q;
    REP(_, q) {
        cin >> X >> Y;
        REP(i, X.size() + 1) {
            REP(j, Y.size() + 1) dp[i][j] = 0;
        }
        REP(i, X.size()) {
            REP(j, Y.size()) {
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j]);
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
                if (X[i] == Y[j]) dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
            }
        }
        int Z_size = dp[X.size()][Y.size()];
        PRINT(Z_size);
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>

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();
  
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (X[i] == Y[j]) 
        if (i == 0 || j == 0) 
          c[i][j] = 1;
        else 
          c[i][j] = c[i - 1][j - 1] + 1;
      else
        if (i != 0 && j != 0) 
          c[i][j] = max(c[i][j - 1], c[i - 1][j]);
        else
          c[i][j] = 0;
    }
  }
  return c[m - 1][n - 1];
}

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

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

using namespace std;
using ll =long long;
typedef pair<int,int> P;
#define SORT(a) sort((a).begin(),(a).end())
#define rSORT(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//Write From this Line

int main()
{
	int q;
	cin >> q;
	while(q--){
		string a, b;
		cin >> a >> b;
		int dp[1010][1010];
		dp[0][0] = 0;
		rep(i,a.size()){
			rep(j,b.size()){
				if(a[i] == b[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[a.size()][b.size()] << endl;
	}
}