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<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int dp[1001][1001];
char x[1001], y[1001];
int main() {
	int q; scanf("%d", &q);
	rep(i, q) {
		scanf("%s%s", x, y); int a = strlen(x), b = strlen(y);
		rep(i, a)rep(j, b) {
			if (x[i] == y[j])dp[i + 1][j + 1] = dp[i][j] + 1;
			else dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
		}
		printf("%d\n", dp[a][b]);
	}
}

C++: #include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#define N 1050
//LCS

using namespace std;

int LCS (string x, string y) {
    
    int maxl = 0;
    int str[N+1][N+1]; 
    int m = x.size();
    int n = y.size();
    x = ' ' + x;
    y = ' ' + y;

    //初期化
    for (int i = 0; i <= m; i++) {
        str[i][0] = 0;
    }
    for (int i = 1; i <= n; i++) {
        str[0][i] = 0;
    }


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

int main() {
    int q;
    cin >> q;
    string X, Y;
    for(int i = 0;i < q; i++) {
        cin >> X >> Y;
        cout << LCS(X,Y) << 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 max1 = 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]);
		}
		max1 = max(max1,C[i][j]);
		}
	} 
	return max1;
}



int main(){
	int q;
	string s1,s2;
	cin>>q;
	for(int i=0;i<q;i++){
		cin>>s1>>s2;
		cout<<lcs(s1,s2)<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;

int lcs(string X,string Y){
  //staticにしないと実行時にスタックオーバーフローになる
  static int c[1001][1001];
  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=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(){
  int q;cin>>q;
  while(q--){
    string a,b;cin>>a>>b;
    cout << lcs(a,b) << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define ll long long int
#define FOR(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n)
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;
  REP(i, m + 1) c[i][0] = 0;
  REP(i, n + 1) c[0][j] = 0;

  REP1(i, m + 1) {
    REP1(j, n + 1) {
      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 dp[1005][1005];

int lcs(string x, string y){
    for (int i = 1; i <= x.size(); i++){
        for (int j = 1; j <= y.size(); j++)
        {
            if(x[i-1] != y[j-1])
                dp[i][j] = dp[i-1][j] > dp[i][j-1] ? dp[i-1][j] : dp[i][j-1];
            else
                dp[i][j] = dp[i-1][j-1] + 1;
        }
    }
    return dp[x.size()][y.size()];
}

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

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

int main(void){
  int q;
  cin >> q;

  for(int i = 0; i < q; i++){
    string a, b;
    cin >> a;
    cin >> b;

    vector<vector<int> > dp(a.size() + 1, vector<int>(b.size() + 1, 0));
    for(int i = 0; i < (int)a.size(); i++){ dp[i][0] = 0; }
    for(int i = 0; i < (int)b.size(); i++){ dp[0][i] = 0; }

    for(int i = 1; i <= (int)a.size(); i++){
      for(int j = 1; j <= (int)b.size(); j++){
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        if(a[i - 1] == b[j - 1])
          dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      }
    }

    cout << dp[a.size()][b.size()] << endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
constexpr int di[] = { 0, 1, 0, -1 }, dj[] = { 1, 0, -1, 0 };
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
#define vmax 10000
constexpr ll inf = 100000000000000;
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/

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;//x[0]に空白を挿入
  y=' '+y;//y[0]に空白を挿入
  for(int i=0;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(){
  string s1,s2;
  int n;cin>>n;
  rep(i,n){
    cin>>s1>>s2;
    cout<<lcs(s1,s2)<<endl;
  }
  return 0;
}

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

using namespace std;

int main(void)
{
    int q;
    char x[1001], y[1001];
    int table[1001][1001];
    
    cin >> q;
    for (int i = 0; i < q; i++) {
	cin >> x >> y;
	int xl = strlen(x), yl = strlen(y);

	for (int i = 0; i < xl; i++)
	    for (int j = 0; j < yl; j++)
		if (x[i] == y[j])
		    table[i+1][j+1] = 1 + table[i][j];
		else
		    table[i+1][j+1] = max(table[i][j+1], table[i+1][j]);

	cout << table[xl][yl] << endl;
    }

    return 0;
}