Go: package main

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

// define
var (
	sc = bufio.NewScanner(os.Stdin)
)

func bordersInt(in_map [][]int, invalid int) [][]int{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]int, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]int, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func bordersString(in_map [][]string, invalid string) [][]string{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]string, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]string, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func scanIntLine(len int) []int{
	arr := make([]int, len)
	for i:=0; i<len; i++{
		sc.Scan()
		tmp, err := strconv.Atoi(sc.Text())
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func scanStringLine(len int) []string{
	arr := make([]string, len)
	for i:=0; i<len; i++{
		sc.Scan()
		arr[i] = sc.Text()
	}
	return arr
}

func scanIntSplit(len int) []int{
	arr := make([]int, len)
	sc.Scan()
	for i, v := range strings.Split(sc.Text(), " "){
		tmp, err := strconv.Atoi(v)
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func maxInt(args... int) int{
	sort.Ints(args)
	return args[len(args)-1]
}

func f() {
	var S, T string
	fmt.Scan(&S)
	fmt.Scan(&T)
	lenS, lenT := len(S), len(T)
	dp := make([][]int, lenS+1)
	for i:=0; i<lenS+1; i++ {
		dp[i] = make([]int, lenT+1)
	}
	for i:=1; i<lenS+1; i++ {
		for j:=1; j<lenT+1; j++ {
			if S[i-1] == T[j-1] {
				dp[i][j] = maxInt(dp[i-1][j-1]+1, dp[i-1][j], dp[i][j-1])
			} else {
				dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
			}
		}
	}
	fmt.Println(dp[lenS][lenT])
}

func main(){
	var n int 
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		f()
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
struct node{
  int value;
  node *next;
  node(int value,node *next):value(value),next(next){}
};
int INF=1LL<<55LL;
string lcs(string x,string y){
  int n=x.size(),m=y.size();
  map<char,vector<int> > M;
  for(int j=m-1;j>=0;j--) M[y[j]].push_back(j);
  vector<int> xs(n+1,INF);
  xs[0]=-INF;
  vector<node* > link(n+1);
  for(int i=0;i<n;i++){
    if(M.count(x[i])){
      vector<int> ys=M[x[i]];
      for(int j=0;j<(int)ys.size();j++){
	int k=distance(xs.begin(),lower_bound(xs.begin(),xs.end(),ys[j]));
	xs[k]=ys[j];
	link[k]=new node(y[ys[j]],link[k-1]);
      }
    }
  }
  string res;
  int l=distance(xs.begin(),lower_bound(xs.begin(),xs.end(),INF-1))-1;
  for(node *p=link[l];p!=NULL;p=p->next)
    res.push_back(p->value);
  return res;
}
signed main(){
  int q;
  cin>>q;
  while(q--){
    string x,y;
    cin>>x>>y;
    cout<<lcs(x,y).size()<<endl;
  }
  return 0;
}
/*
verified on 2017/06/22
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C
*/

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))