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++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <stdlib.h>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define max 1001
int n,c[max][max];
string x,y;
void SerchMinStr(){
  int i,j,ans,k;
  for(i=1;i<(int)x.length()+1;i++){
    for(j=1;j<(int)y.length()+1;j++){
      if(x[i-1]==y[j-1]){c[i][j]=c[i-1][j-1]+1;}
      else if(c[i-1][j]>=c[i][j-1])c[i][j]=c[i-1][j];
      else c[i][j]=c[i][j-1];
    }
  }
  i--;j--;
  ans=c[i][j];

  for(k=1;k<(int)x.length();k++)
    if(ans<c[k][j])ans=c[k][j];
  for(k=1;k<(int)y.length();k++)
    if(ans<c[i][k])ans=c[i][k];
  cout<<ans<<endl;
}
int main(){
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>x>>y;
    SerchMinStr();
  }  
  return 0;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <queue>

using namespace std;

const char *X, *Y;
int X_length, Y_length;

int table_lcs[1001][1001];

int lcs(int x_begin, int y_begin) {
	int i, j, to;
	int max = 0;
	int v_lcs;
	bool alreadySeenTable[256];
	if (table_lcs[x_begin][y_begin] != -1) {
		return table_lcs[x_begin][y_begin];
	}
	
	for (i = 0; i < 256; i++) {
		alreadySeenTable[i] = false;
	}
	i = x_begin;
	to = Y_length-1;
	while(i <= X_length-1) {
		j = y_begin;
		if (alreadySeenTable[X[i]] == true) {
			i++;
			continue;
		}
		bool found = false;
		while (j <= to) {
			if (X[i] == Y[j]) {
				v_lcs = lcs(i + 1, j + 1) + 1;
				if (v_lcs > max) {
					max = v_lcs;
				}
				found = true;
				break;
			}
			else {
				j++;
			}
		}
		if (found == false) {
			alreadySeenTable[X[i]] = true;
			i++;
		}
		else {
			alreadySeenTable[X[i]] = true;
			i++;
			to = j - 1;
		}
	}
	return table_lcs[x_begin][y_begin] = max;
}

int lcs2(int x_begin, int y_begin) {
	int i, j, to;
	int max = 0;
	int v_lcs;
	if (table_lcs[x_begin][y_begin] != -1) {
		return table_lcs[x_begin][y_begin];
	}

	i = x_begin;
	to = Y_length - 1;
	while (i <= X_length - 1) {
		j = y_begin;
		bool found = false;
		while (j <= to) {
			if (X[i] == Y[j]) {
				v_lcs = lcs2(i + 1, j + 1) + 1;
				if (v_lcs > max) {
					max = v_lcs;
				}
				found = true;
				break;
			}
			else {
				j++;
			}
		}
		if (found == false) {
			i++;
		}
		else {
			i++;
			to = j - 1;
		}
	}
	return table_lcs[x_begin][y_begin] = max;
}

int main() {

	int N;
	cin >> N;
	string stringX, stringY;

	for (int i = 1; i <= N; i++) {
		for (int i = 0; i < 1001; i++) {
			for (int j = 0; j < 1001; j++) {
				table_lcs[i][j] = -1;
			}
		}
		cin >> stringX >> stringY;
		X = stringX.c_str();
		X_length = stringX.size();
		Y = stringY.c_str();
		Y_length = stringY.size();
		printf("%d\n", lcs2(0, 0));
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <fstream>
#include <numeric>


using namespace std;

#define max(a,b) a > b ? a : b
#define min(a,b) a < b ? a : b

const int INF = 1 << 29;        // 十分大きい値にする

// 入力
string S, T;

// DP テーブル
int dp[1010][1010];
int n;
int ans[150];

int main() {

    cin >> n;

    for (int i = 0; i < n; i++)
    {
        cin >> S >> T;
        memset(dp, 0, sizeof(dp));    // 初期化
        for (int i = 0; i < S.size(); ++i) {
            for (int j = 0; j < T.size(); ++j) {
                if (S[i] == T[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]);
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
            }
        }

        ans[i] = dp[S.size()][T.size()];

    }

    for (int i = 0; i < n; i++)
    {
        cout << ans[i] << endl;
    }

}

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

int lcs(const string& X,
    const string& Y) 
{
    int lenX = X.length();
    int lenY = Y.length();
    vector<vector<int>> dp(lenX + 1, vector<int>(lenY + 1));
    for (int i = 1; i <= lenX; ++i) {
        for (int j = 1; j <= lenY; ++j) {
            if (X[i-1] == Y[j-1]) {
                dp[i][j] = dp[i-1][j-1] + 1;
            }
            else {
                dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
            }
        }
    }
    return dp[lenX][lenY];
}

int main() {
    int q;
    cin >> q;
    vector<string> Xs(q);
    vector<string> Ys(q);
    for (int i = 0; i < q; ++i) {
        cin >> Xs[i];
        cin >> Ys[i];
    }
    for (int i = 0; i < q; ++i) {
        cout << lcs(Xs[i], Ys[i]) << endl;
    }



}

C++: // #include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <set>
#include <queue>
#include <cstdio>
using namespace std;
#define rep(i, n) for(int i=0;i<n;i++)
#define irep(i, n) for(int i=n-1;i>=0;i--)
#define lrep(i, n) for(long long i=0;i<n;i++)
typedef long long ll;
typedef vector<long long> llv;
typedef vector<vector<long long> > llvv;

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;
    rep(i,n){
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
    return 0;
}

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

using namespace std;

int lcslen[1001][1001]={0};
int compute_lcslen(const string &x,const string &y);

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

  return 0;
}

int compute_lcslen(const string &x,const string &y){
  for(int i=1;i<=x.length();++i){
    for(int j=1;j<=y.length();++j){
      if(x[i-1]==y[j-1]){
        lcslen[i][j] = lcslen[i-1][j-1]+1;
      }else{
        lcslen[i][j] = max(lcslen[i-1][j],lcslen[i][j-1]);
      }
    }
  }
  return lcslen[x.length()][y.length()];
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int dp[111][111];
int lcs(string x,string y){
	for(int i=0;i<12321;i++)dp[i/111][i%111]=0;
	for(int i=0;i<=y.size();i++){
		for(int j=0;j<=x.size();j++){
			if(i==0 || j==0){dp[i][j]=0;}
			else if(x[j]==y[i]){dp[i][j]=dp[i-1][j-1]+1;}
			else{dp[i][j]=max(dp[i][j-1],dp[i-1][j]);}
		}
	}
	int MAXN=0;
	for(int i=0;i<=y.size();i++){
		for(int j=0;j<=x.size();j++){
			MAXN=max(MAXN,dp[i][j]);
		}
	}
	return MAXN;
}
int main(){
	int n;string S,T;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>S>>T;cout<<lcs(S,T)<<endl;
	}
	return 0;
}