Go: package main

import "fmt"

func main() {
	var nPairs int
	fmt.Scanln(&nPairs)

	var a, b string
	for i := 0; i < nPairs; i++ {
		fmt.Scanln(&a)
		fmt.Scanln(&b)
		fmt.Println(lcsSize(a, b))
	}
}

func lcsSize(sa, sb string) int {
	dp := make([][]int, len(sa)+1)
	for i := 0; i <= len(sa); i++ {
		dp[i] = make([]int, len(sb)+1)
	}

	for a := 1; a <= len(sa); a++ {
		for b := 1; b <= len(sb); b++ {
			if sa[a-1] == sb[b-1] {
				dp[a][b] = dp[a-1][b-1] + 1
			} else {
				dp[a][b] = max(dp[a][b-1], dp[a-1][b])
			}
		}
	}

	return dp[len(sa)][len(sb)]
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>

#define debug(x) cout << #x << "==" << x << endl;

using namespace std;

//??\???
#define MAX_LEN 1000
int N;
int dp[MAX_LEN + 1][MAX_LEN + 1];

void print_dp_table(int n, int m) {
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
      cout <<setw(2) << dp[i][j];
    }
    cout << "\n";
  }
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(false);

  cin >> N;

  string s1, s2;

  while( cin >> s1 >> s2 ) {

    std::vector<char> a;
    std::vector<char> b;
    for(char& c : s1) {
      a.push_back(c);
    }
    for(char& c : s2) {
      b.push_back(c);
    }


    // dp[i][j] := a???i???????????§???b???j???????????§????????¨??????????????±?????¨??????
    // dp ????????????????????????
    fill(dp[0], dp[MAX_LEN + 1], 0);

    for(int i = 1; i <= (int)a.size(); i++ ) {
      for(int j = 1; j <= (int)b.size(); j++) {
        if( a[i-1] == 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] );
        }
      }
    }

    // for( int i = 0; i < (int)a.size(); i++) {
    //   for( int j = 0; j < (int)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]);
    //     }
    //   }
    // }

    // print_dp_table(a.size(), b.size());
    cout << dp[a.size()][b.size()] << endl;

  }

  return 0;
}

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

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int dp[1005][1005];

int main() {
    int n; cin >> n;
    REP(k, n){
        REP(i, 1005) REP(j, 1005) dp[i][j] = 0;
        string a, b;
        cin >> a >> b;
        REP(i, a.length()){
            string q = a.substr(i, 1);
            REP(j, b.length()){
                string t = b.substr(j, 1);
                if(q == t){
                    if(i == 0 || j == 0)
                        dp[i][j] = 1;
                    else
                        dp[i][j] = max(dp[i-1][j], dp[i][j-1] + 1);
                }
                if (i != 0 && j != 0)
                    dp[i][j] = (dp[i-1][j] > dp[i][j-1]) ? max(dp[i][j], dp[i-1][j]) : max(dp[i][j], dp[i][j-1]);
                else if (i != 0)
                    dp[i][j] = max(dp[i][j], dp[i-1][j]);
                else if (j != 0)
                    dp[i][j] = max(dp[i][j], dp[i][j-1]);
            }
        }
        cout << dp[a.length()-1][b.length()-1] << endl;
    }
    return 0;
}

C++: #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int f(string &x, string &y, int xi, int yi, vector<vector<int>> &memo) {
    if (xi < 0 || yi < 0) return 0;
    if (memo[xi][yi] != -1) return memo[xi][yi];
    int ret;
    if (x[xi] == y[yi])
        ret = f(x, y, xi - 1, yi - 1, memo) + 1;
    else
        ret = max(f(x, y, xi - 1, yi, memo), f(x, y, xi, yi - 1, memo));
    memo[xi][yi] = ret;
    return ret;
}

int main() {
    int q;
    cin >> q;
    for (; q; --q) {
        string x, y;
        cin >> x >> y;
        int xl = x.size(), yl = y.size();
        vector<vector<int>> memo(xl, vector<int>(yl, -1));
        cout << f(x, y, xl - 1, yl - 1, memo) << endl;
    }
    return 0;
}

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

#define rep(i,n) for(int i=0;i<(n);i++)

template<class T>
bool chmax(T &a, const T &b) {
    if(a<b) {
        a=b;
        return false;
    }
    return true;
}

using namespace std;

int dp[1003][1003];
int main() {
    int q; cin>>q;
    rep(_,q) {
        string s,t;
        cin>>s>>t;
        s=" "+s;
        t=" "+t;
        rep(i,s.size()) rep(j,s.size()) if(i&&j) {
            if(s[i]==t[j]) dp[i][j]=dp[i-1][j-1]+1;
            else {
                chmax(dp[i][j],dp[i-1][j]);
                chmax(dp[i][j],dp[i][j-1]);
            }
        }
        int ans=0;
        rep(i,s.size()) chmax(ans,*max_element(dp[i],dp[i]+t.size()));
        cout<<ans<<endl;
    }

    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();
    int maxl = 0;
    X = " " + X ;
    Y = " " + Y ;
    for (int i = 0; i <= m; i++) c[i][0] = 0;
    for (int j = 0; 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][j-1], c[i-1][j]) ;
            }
            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;
using Int = unsigned long long;
typedef pair<int,int> P;
typedef priority_queue<int> Pr;
Int const MOD = 1000000007;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }


int main(){
    cin.tie(0); ios::sync_with_stdio(false);

    int q; cin>>q;
    for (int i = 0; i < q; i++){
        int dp[1001][1001]={};
        string s1,s2; cin>>s1>>s2;
        for (int j = 0; j < s1.size(); j++){
            for (int k = 0; k < s2.size(); k++){
                if(s1[j]==s2[k]){
                    chmax(dp[j+1][k+1],max(dp[j][k+1],max(dp[j+1][k],dp[j][k]+1)));
                }else chmax(dp[j+1][k+1],max(dp[j][k+1],dp[j+1][k]));
            }
        }
        cout<<dp[s1.size()][s2.size()]<<'\n';
    }
}

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

vector<vector<int> > D(2000, vector<int>(2000, -1));

// Xのi番目以前とYのj番目以前の最長共通部分列の大きさを返す
int lcs(string &X, string &Y, int i, int j)
{
  // メモされているとき
  if (D.at(i).at(j) > -1)
  {
    return D.at(i).at(j);
  }

  // ベースケース
  if (i == 0)
  {
    for (int tmp = 0; tmp <= j; tmp++)
    {
      if (X.at(i) == Y.at(tmp))
      {
        D.at(i).at(j) = 1;
        return 1;
      }
    }
    D.at(i).at(j) = 0;
    return 0;
  }
  if (j == 0)
  {
    for (int tmp = 0; tmp <= i; tmp++)
    {
      if (X.at(tmp) == Y.at(j))
      {
        D.at(i).at(j) = 1;
        return 1;
      }
    }
    D.at(i).at(j) = 0;
    return 0;
  }
  // 再帰ステップ
  int M = 0;
  if (i > 0)
  {
    M = max(M, lcs(X, Y, i - 1, j));
  }
  if (j > 0)
  {
    M = max(M, lcs(X, Y, i, j - 1));
  }
  if ((i > 0 && j > 0) && (X.at(i)) == Y.at(j))
  {
    M = max(M, lcs(X, Y, i - 1, j - 1) + 1);
  }
  D.at(i).at(j) = M;
  return M;
}

int main()
{
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    string X, Y;
    cin >> X;
    cin >> Y;
    cout << lcs(X, Y, X.size() - 1, Y.size() - 1) << endl;
    vector<vector<int> > E(2000, vector<int>(2000, -1));
    D = E;
  }
}