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 <iostream>
#include <algorithm>
using namespace std;
const int MAX=1001;
//char result[MAX];
//int cnt=0;
//string ans[MAX];
int lcs(string x,string y,int xlen,int ylen) 
{
	int i=0,j=0,c[MAX][MAX];
	for (i=0;i<=xlen;i++)c[i][0]=0;
	for (i=0;i<=ylen;i++)c[0][i]=0;
	for (i=1;i<=xlen;i++) 
	for (j=1;j<=ylen;j++) 
	if (x[i -1]==y[j-1])
	{
		c[i][j]=c[i-1][j-1]+1; 
		//b[i][j]=1;
	}
	else if (c[i-1][j]>c[i][j-1]) 
	{
		c[i][j]=c[i-1][j]; 
		//b[i][j]=2;
	}
	else if(c[i-1][j]<c[i][j-1])
	{
		c[i][j]=c[i][j-1]; 
		//b[i][j]=3;
	}
	else
	{
		c[i][j]=c[i][j-1]; //??????c[i][j]=c[i-1][j];
		//b[i][j]=4;
	}
	 /*cout << "???????????????????????????????????????" << endl;
	 for(i = 1; i <= xlen; i++)
	 {
		for(j = 1; j < ylen; j++)cout << c[i][j] << " ";
		cout << endl;
	  }*/
	  return c[xlen][ylen];
}
/*void show(int i,int j,string x,int b[][MAX],int cur,int max) 
{
	if(i==0||j==0) 
	{
		for(int s=0;s<max;s++)ans[cnt].push_back(result[s]);
		++cnt;
		return;
	}
	if(b[i][j]==1) 
	{ 
		--cur;
		result[cur]=x[i-1];
		show(i-1, j-1, x, b,cur,max);
	}
	else if(b[i][j]==2)show(i-1, j, x, b,cur,max);
	else if(b[i][j]==3)show(i, j-1, x, b,cur,max);
	else
	{
		show(i,j-1,x,b,cur,max);
		show(i-1,j,x,b,cur,max);
	}
}*/
 int main()
 {
	int n;cin>>n;
	string X,Y;
	while(n--)
	{
		//while(
		cin>>X>>Y;
		//)
		//{
			//cnt=0;
			//for(int i=0;i<MAX;++i)ans[i].clear();
			int x=X.length(),y=Y.length(),len=lcs(X,Y,x,y);//b[MAX][MAX];
			cout<<len<<endl;
			//show(x,y,X,b,len,len);
			//sort(ans,ans+cnt);
			//for(int i=0;i<cnt;++i)cout<<ans[i]<<"\n";
		//}
	}
	
 }

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 = 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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
char A[N],B[N];
int dp[N][N];
void lcs(char A[],char B[]){
    int lenA = strlen(A+1);
    int lenB = strlen(B+1);
    
    for(int i=0;i<=lenA;i++){
        dp[i][0] = 0;
    }
    for(int i=0;i<lenB;i++){
        dp[0][i] = 0;
    }
    for (int i=1; i<=lenA; i++) {
        for(int j=1;j<=lenB;j++){
            if(A[i] == B[j]){
                dp[i][j] = dp[i-1][j-1] + 1;
            }
            else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
    }
    printf("%d\n",dp[lenA][lenB]);
}

int main(){
    int n;
    scanf("%d",&n);
    for (int i=0; i<n; i++) {
        //从下标为1开始读入
        gets(A+1);
        gets(B+1);
        lcs(A, B);
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(i, n) for(int i=0; i<=n; i++)
#define repr(i, n) for(int i=n; i>0; i--)
#define REPR(i, n) for(int i=n; i>=0; i--)
#define FOR(i, a, b) for(int i=a; i<b; i++)
#define ALL(v) v.begin(),v.end()
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9+7;
template<class T> void puts(T x) { std::cout << x << std::endl; }
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 q;

int mmax(int a, int b, int c) {
    int ret;
    ret = max(a, b);
    ret = max(ret, c);
    return ret;
}

int lcs(string s1, string s2) {
    int n1 = s1.length();
    int n2 = s2.length();

    int dp[n1][n2];
    dp[0][0] = s1[0] == s2[0];
    for (int i = 1; i < n1; i++) {
        dp[i][0] = s1[i] == s2[0] ? 1 : dp[i-1][0];
    }
    for (int i = 1; i < n2; i++) {
        dp[0][i] = s2[i] == s1[0] ? 1 : dp[0][i-1];
    }

    for (int i = 1; i < n1; i++) {
        for (int j = 1; j < n2; j++) {
            int t = s1[i] == s2[j];
            dp[i][j] = mmax(dp[i-1][j-1] + t,
                            dp[i-1][j],
                            dp[i][j-1]);
        }
    }

    return dp[n1-1][n2-1];
}

int main() {
    cin >> q;
    rep(i, q) {
        string s1, s2; cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
}

C++: #include <iostream>
#include <string>
using namespace std;

int lcs(string X, string Y) {
  int cs[1001][1001];
  int m = X.size();
  int n = Y.size();
  int answer = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  for (int i = 1; i <= m; i++) cs[i][0] = 0;
  for (int j = 1; j <= n; j++) cs[0][j] = 0;

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[j]) {
        cs[i][j] = cs[i - 1][j - 1] + 1;
      } else {
        cs[i][j] = max(cs[i - 1][j], cs[i][j - 1]);
      }
      answer = max(answer, cs[i][j]);
    }
  }

  return answer;
}

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

  int n;
  cin >> n;

  string X, Y;
  int answers[n];

  for (int i = 0; i < n; i++) {
    cin >> X >> Y;
    answers[i] = lcs(X, Y);
  }

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

  return 0;
}