Go: package main

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

func max(x, y int) int {
	if x < y {
		return y
	} else {
		return x
	}
}

/*********************************************************************
長さmの列X_(m)と長さnの列Y_(n)の最長共通部分列LCSは、
	(1)X[m] == Y[n]ならば、X_(m-1)とY_(n-1)のLCSにX[m]を繋げたもの
	(2)X[m] != Y[n]ならば、X_(m-1)とY_(n)のLCSもしくはX_(m)とY_(n-1)のLCS
	のいずれかのうち長い方

c[m+1][n+1]：c[i][j]にX_(i)とY_(j)のLCSの長さを格納する二次元配列
*********************************************************************/

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		s1 := scanner.Text()
		scanner.Scan()
		s2 := scanner.Text()
		s1, s2 = s1 + " ", s2 + " "
		c := make([][]int, len(s1))
		for i := range c {
			c[i] = make([]int, len(s2))
		}

		LCS := 0

		for i := 1; i < len(s1); i++ {
			for j := 1; j < len(s2); j++ {
				if s1[i] == s2[j] {
					c[i][j] = c[i-1][j-1] + 1
				} else {
					c[i][j] = max(c[i-1][j], c[i][j-1])
				}
				LCS = max(LCS, c[i][j])
			}
		}
		fmt.Println(LCS)
	}
}

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';
    }
}