Go: package main

import "fmt"

func initialize_grid(width int,height int)[][]int{
	var grid [][]int
	for i:=0;i<height+1;i++{
		var grid_row  []int
		for j:=0;j<width+1;j++{
			grid_row = append(grid_row,0)
		}
		grid = append(grid,grid_row[:])
	}
	return grid
}

func ma(x int,y int)int{
	if x >= y{
		return x
	}else{
		return y
	}
}

func main()  {
	//Take input
	var num int
	var st1,st2 string
	var string_pairs [][]string
	fmt.Scan(&num)
	for i:=0;i<num;i++{
		var string_pair []string
		fmt.Scan(&st1)
		fmt.Scan(&st2)
		string_pair = append(string_pair,st1,st2)
		string_pairs = append(string_pairs,string_pair)
	}
	//DP
	//initialize grid
	for i:=0;i<num;i++{
		string_1 := string_pairs[i][0]
		string_2 := string_pairs[i][1]

		dp := initialize_grid(len(string_2),len(string_1))

		for j:=1;j<len(string_1)+1;j++{
			for k:=1;k<len(string_2)+1;k++{
				if string_1[j-1] == string_2[k-1]{
					dp[j][k] = dp[j-1][k-1] + 1
				}else{
					dp[j][k] = ma(dp[j-1][k],dp[j][k-1])
				}
			}
		}
		fmt.Println(dp[len(string_1)][len(string_2)])
	}
}

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 i = 1; i <= n; i++) c[0][i] = 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()
{
	int n;
	string s1, s2;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> s1 >> s2;
		cout << lcs(s1, s2) << endl;
	}
    return 0;
}

C++: #include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>

#define MAX_LENGTH 1000
#define NIL -1
using namespace std;
//think easy make easy

int q;//num of datasets
string X;
string Y;
int map[MAX_LENGTH+1][MAX_LENGTH+1];

int findMaxLength(int x,int y){//x:Xの終端.y:Yの終端(0オリジン)
	int val;

	if(x < 0 || y < 0) return 0;

	if(map[x][y] == NIL){
		if(X.at(x) == Y.at(y)){
			val = findMaxLength(x-1,y-1) +1;
		}else{
			val = max(findMaxLength(x-1,y),findMaxLength(x,y-1));
		}
		map[x][y] = val;//メモ
		return val;
	}else return map[x][y];
}

int main(){
	
	scanf("%d",&q);

	for(int i=0;i<q;i++){
		cin >>X; cin >> Y;
		for(int i=0;i<X.size();i++){
			for(int j=0;j<Y.size();j++){
				map[i][j] = NIL;
			}
		}
	
		cout <<findMaxLength(X.size()-1,Y.size()-1) <<endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <vector>
#include <string>

class TLcsSolver
{
    typedef std::vector<std::vector<int> > TTable;
    TTable FTable;
    std::string FArray1;
    std::string FArray2;

    int Count(int i, int j)
    {
        if(i == 0 || j == 0) return 0;
        if(FArray1[i-1] == FArray2[j-1]){
            return FTable[i-1][j-1] + 1;
        }
        return std::max(FTable[i-1][j], FTable[i][j-1]);
    }
    void Solve()
    {
        for(std::size_t i = 1; i < FTable.size(); ++i){
            for(std::size_t j = 1; j < FTable[i].size(); ++j){
                FTable[i][j] = Count(i, j);
            }
        }
    }
public:
    TLcsSolver(const std::string& a, const std::string& b)
     : FArray1(a), FArray2(b)
     , FTable(a.size()+1, std::vector<int>(b.size()+1, 0))
    {
        Solve();
    }
    int Answer()const
    {
        return FTable[FArray1.size()][FArray2.size()];
    }
};

int main()
{
    int n;
    std::scanf("%d", &n);
    char a[1000];
    char b[1000];
    for(int i = 0; i < n; ++i){
        std::fgets(a, 1000, stdin);
        std::fgets(b, 1000, stdin));
        std::printf("%d", TLcsSolver(a, b).Answer());
    }
    return 0;
}

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

int Longest(string x, string y) {
  int L[1001][1001];
  int maxl = -1;
  int xlen = x.size();
  int ylen = y.size();

  x = ' ' + x;
  y = ' ' + y;

  for (int i = 1; i <= xlen; i++) L[i][0] = 0;
  for (int j = 1; j <= ylen; j++) L[0][j] = 0;

  for (int i = 1; i <= xlen; i++) {
    for (int j = 1; j <= ylen; j++) {
      if (x[i] == y[j])
        L[i][j] = L[i-1][j-1] + 1;
      else
        L[i][j] = max(L[i][j-1], L[i-1][j]);
      
      maxl = max(maxl, L[i][j]);
    }
  }
  
  return maxl;
}

int main() {
  string X, Y;
  int n;
  int result[10000];

  cin >> n;
  
  for (int i = 0; i < n; i++) {
    cin >> X;
    cin >> Y;
    result[i] =  Longest(X, Y);
  }
  
  for (int i = 0; i < n; i++)
    cout << result[i] << 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 q;
	cin >> q;
	for (int i=0; i<q;i++){
		cin >> s1 >> s2;
		cout << lcs(s1, s2) << endl;
	}
	return 0;

}

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