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 <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

int dat[1024][1024];
int getLcbLen(char* x, int xe, char* y, int ye){
	if(xe <= 0 || ye <= 0) return 0;
	if(dat[xe][ye] != -1) return dat[xe][ye];
	return dat[xe][ye] = (x[xe-1] == y[ye-1] ? 1 + getLcbLen(x, xe-1, y, ye-1) : max(getLcbLen(x, xe-1, y, ye), getLcbLen(x, xe, y, ye-1)));
}

int main() {
	int set;
	scanf("%d", &set);
	
	while(set--){
		char x[1024], y[1024];
		scanf("%s %s", x, y);
		int xe = (int)strlen(x), ye = (int)strlen(y);
		
		for(int i = 0; i < xe; i++){
			for(int k = 0; k < ye; k++){
				dat[i][k] = -1;
			}
		}
		printf("%d\n", getLcbLen(x, xe, y, ye));
	}
	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <list>
#include <utility>
#include <cmath>
#include <sstream>
#define F first
#define S second
#define FOR(i,n) for(int i=0;i<(int)n;i++)
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define FORI(i,k,n) for(int i=k;i<(int)n;i++)
#define repp(i,k,n) for(int i=k;i<(int)n;i++)
using namespace std;
typedef pair<double, double> pdd;
typedef pair<int,int> pii;
const double EPS = 10e-6;
int c[2000][2000]={};
string x,y;
int dp(int o,int p)
{
	if(o==x.size()||p==y.size())return 0;
	if(c[o][p]!=-1)return c[o][p];
	int ans =0;
	if(x[o]==y[p])
	{
		ans=max(dp(o+1,p+1)+1,max(dp(o+1,p),dp(o,p+1)));
	}
	else ans=max(dp(o+1,p),dp(o,p+1));

	return c[o][p]=ans;
}

int main ()
{
	int n;
	cin>>n;
	rep(i,n)
	{
		
		cin>>x>>y;
		memset(c, -1, sizeof(c));
		cout<<dp(0,0)<<endl;;
	}



}

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

int main() {
	long long Q; cin >> Q;
	for (long long i = 0; i < Q; i++) {
		string X, Y; cin >> X >> Y;
		long long X_size = X.size(), Y_size = Y.size();
		vector<vector<long long>>DP(1100, vector<long long>(1100, 0));
		for (long long j = 0; j < X_size; j++) {
			DP.at(j).at(0) = 0;
		}
		for (long long j = 0; j < Y_size; j++) {
			DP.at(0).at(j) = 0;
		}
		for (long long j = 0; j < X_size; j++) {
			for (long long k = 0; k < Y_size; k++) {
				if (X.at(j) == Y.at(k)) DP.at(j + 1).at(k + 1) = max({ DP.at(j).at(k) + 1, DP.at(j).at(k + 1), DP.at(j + 1).at(k) });
				else DP.at(j + 1).at(k + 1) = max(DP.at(j).at(k + 1), DP.at(j + 1).at(k));
			}
		}
		cout << DP.at(X_size).at(Y_size) << endl;
	}
}

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

using namespace std;

int main(){

  int q;

  cin >> q;


  for(int k = 0; k < q; k++){
    string a, b;
    cin >> a >> b;
    int dp[a.size()][b.size()] = {};
    //初期設定
    if(a[0] == b[0]){
      dp[0][0] = 1;
    }
    for(int i = 1; i < a.size(); i++){
      if(a[i] == b[0] || dp[i-1][0] == 1){
        dp[i][0] = 1;
      }
    }
    for(int j = 1; j < b.size(); j++){
      if(a[0] == b[j] || dp[0][j-1] == 1){
        dp[0][j] = 1;
      }
    }
    for(int i = 1; i < a.size(); i++){
      for(int j = 1; j < b.size(); j++){
        if(a[i] == b[j]){
          if(dp[i-1][j-1] >= dp[i-1][j] && dp[i-1][j-1] >= dp[i][j-1]){
            dp[i][j] = dp[i-1][j-1]+1;

          }
          else{
            dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
          }
        }
        else{
          dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
      }
    }
    cout << dp[a.size()-1][b.size()-1] << "\n";
  }

  return 0;
}

C++: #include <iostream>

using namespace std;

int LCS[1001][1001];

int lcs(string X, string Y)
{
    int m = X.size();
    int n = Y.size();
    X = ' ' + X;
    Y = ' ' + Y;
    for(int i = 0; i <= m; i++)
        LCS[i][0] = 0;
    for(int j = 1; j <= n; j++)
        LCS[0][j] = 0;
    
    for(int i = 1; i <= m; i++)
    {
        for(int j = 1; j <= n; j++)
        {
            if(X[i] == Y[j])
                LCS[i][j] = LCS[i - 1][j - 1] + 1;
            else
                LCS[i][j] = max(LCS[i][j - 1], LCS[i - 1][j]);
        }
    }
    return LCS[m][n];
}

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

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 q;
  cin >> q;

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

  return 0;
}

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

int main(){
	int q;
	scanf(" %d", &q);
	for(int i=0; i<q; ++i){
		string a, b;
		cin >> a >> b;
		
		int m=a.size(), n=b.size();
		
		string x=" "+a;
		string y=" "+b;
		
		int c[1001][1001]={0};
		for(int i=1; i<=m; ++i) c[i][0]=0;
		for(int i=1; i<=n; ++i) c[0][n]=0;
		
		int ans=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]);
				
				ans=max(ans,c[i][j]);		
			}
		}
		
		printf("%d\n", ans);
	}
}

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

#define MAX_N 1000
#define MAX_M 1000

int n, m;
std::string s, t;

int dp[MAX_N+1][MAX_M+1];

void solve() {
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (s[i+1] == t[j+1]) {
        dp[i+1][j+1] = dp[i][j] + 1;
      }
      else {
        dp[i+1][j+1] = std::max(dp[i][j+1], dp[i+1][j]);
      }
    }
  }
}

int main() {
  int q;
  std::cin >> q;
  for (int i = 0; i < q; ++i) {
    std::cin >> s >> t;
    n = s.size();
    m = t.size();
    solve();
    std::cout << dp[n][m] << std::endl;
  }
}

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