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>

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);
  }
  return 0;
}

C++: #include <iostream>

using namespace std;

int lcs(string X, string Y) {
  int memo[1010][1010];
  // 初期化
  for (int i = 0; i < X.size(); i++) {
    memo[i][0] = 0;
  }
  for (int i = 0; i < Y.size(); i++) {
    memo[0][i] = 0;
  }
  int maxl = 0;
  for (int i = 0; i < X.size(); i++) {
    for (int j = 0; j < Y.size(); j++) {
      if (X[i] == Y[j]) {
        memo[i][j] = memo[i-1][j-1] + 1;
      } else {
        memo[i][j] = max(memo[i-1][j], memo[i][j-1]);
      }
      maxl = max(maxl, memo[i][j]);
    }
  }
  return maxl;
}

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

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


using namespace std;int n[1003][1003]={};
int main(){

string A,B;
int N;
cin>>N;
for(int I=0;I<N;I++){
	cin>>A;cin>>B;

	
	int max=0;
	for(int i=0;i<(int)A.size();i++)
		for(int j=0;j<(int)B.size();j++)
			{
				
				if(A[i]==B[j])n[i+1][j+1]=n[i][j]+1;
				else{
					n[i+1][j+1]=n[i+1][j];
				if(n[i][j+1]>n[i+1][j])n[i+1][j+1]=n[i][j+1];
				}
				
		}
		
		
		
		for(int i=A.size()+1;i>=0;i--){int c=0;int y=i;
		for(int j=B.size()+1;j>=0;j--)
		{if(n[y][j]==n[y-1][j-1]+1&&y>0){y--;c++;
		}
	}
		if(max<c)max=c;
		}
		cout<<max<<endl;
}
return 0;
}

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

int main(){
  int n;
  cin >> n;

  vector<int> anss;
  for (int i = 0; i < n; i++)
  {
    string s,t;
    cin >> s >> t;

    int lens = s.size();
    int lent = t.size();
    int dp[lens][lent];
    bool flg = false;
    for (int j = 0; j < lens; j++)
    {
      if(t[0]==s[j]) flg = true;
      if(flg) dp[j][0] = 1;
      else dp[j][0] = 0;
    }
    flg = false;
    for (int j = 0; j < lent; j++)
    {
      if(s[0]==t[j]) flg = true;
      if(flg) dp[0][j] = 1;
      else dp[0][j] = 0;
    }

    for (int j = 1; j < lens; j++)
    {
      for (int k = 1; k < lent; k++)
      {
        if(s[j]==t[k]) dp[j][k] = dp[j-1][k-1]+1;
        else dp[j][k] = max(dp[j-1][k],dp[j][k-1]);
      }      
    }
    anss.push_back(dp[lens-1][lent-1]);
  }
  for(int ans:anss) cout << ans << endl;
}

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

int main() {
  int q; cin >> q;
  string A, B;
  for (int i=0; i<q; i++) {
    cin >> A >> B;
    vector<vector<int>> LCS(A.length()+1, vector<int>(B.length()+1,0));
    int ans=0;
    for (int j=1; j<=A.length(); j++) {
      for (int k=1; k<=B.length(); k++) {
        if(A[j]==B[k]) LCS[j][k]=LCS[j-1][k-1]+1;
        else LCS[j][k]=max(LCS[j-1][k],LCS[j][k-1]);
        ans = max(ans, LCS[j][k]);
      }
    }
    cout << ans << "\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++ ) {
    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 q;
  cin >> q;

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

  return 0;
}

C++: #include <stdio.h>

int judge(char A[], char B[]){
  int a=0, b=0, sw=0, ans=0;
  int i, j;
  int C[1001][1001];
  
  while(sw == 0){
    if(A[a] != EOF)
      a++;
    else
      sw = 1;
  }
  
  sw = 0;
  
  while(sw == 0){
    if(B[b] != EOF)
      b++;
    else
      sw = 1;
  }
  
  for(i=1; i<a+1; i++)
    C[i][0] = 0;
  for(i=1; i<b+1; i++)
    C[0][i] = 0;
  
  for(i=1; i<a+1; i++){
    for(j=1; j<b+1; j++){
      if(A[i-1] == B[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];
      if(ans < C[i][j])
        ans = C[i][j];
    }
  }
  
  return ans;
}

int main(){
  char A[1000], B[1000];
  int n, i;
  
  scanf("%d", &n);
  
  for(i=0; i<n; i++){
    scanf("%s", A);
    scanf("%s", B);
    printf("%d\n", judge(A, B));
  }
  
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>

using namespace std;

int main() {
	int n;	
	int c[1001][1001];
	string str1, str2;

	cin >> n;

	for (int k = 0; k < n; k++) {
		int Max = 0;
		cin >> str1 >> str2;
		
		int m = str1.size();
		int n = str2.size();


		str1 = ' ' + str1;
		str2 = ' ' + str2;

		//cout << str1 << str2 << endl;


		for (int i = 0; i <= m; i++) {
			for (int j = 0; j <= n; j++) {
				if (i == 0 || j == 0) c[i][j] = 0;
				else if (str1[i] == str2[j]) c[i][j] = c[i - 1][j - 1] + 1;
				else c[i][j] = max(c[i][j - 1], c[i - 1][j]);

				Max = max(Max, c[i][j]);

				//cout << c[i][j] << " ";

			}

			//cout << endl;

		}

		cout << Max << endl;
	}

	return 0;

}

C++: #include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;

int dp[1010][1010];
string x, y;

int solve(int a, int b) {
    if (dp[a][b] >= 0) {
        return dp[a][b];
    }
    
    if (a * b == 0) {
        dp[a][b] = 0;
        return 0;
    }
    
    if (char(x[a - 1]) == char(y[b - 1])) dp[a][b] = solve(a - 1, b - 1) + 1;
    else dp[a][b] = solve(a - 1, b - 1);
    
    dp[a][b] = max(dp[a][b], max(solve(a - 1, b), solve(a, b - 1)));
    
    return dp[a][b];
}

int main(void){
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> x >> y;
        
        for (int i = 0; i <= x.size(); i++) {
            for (int j = 0; j <= y.size(); j++) dp[i][j] = -1;
        }
        dp[0][0] = 0;
        
        cout << solve(x.size(), y.size()) << endl;
    }
}