Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Position struct {
	X int
	Y int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}
	for i := 0; i < n; i++ {
		strX := nextText(sc)
		strY := nextText(sc)
		fmt.Println(calcLcs(strX, strY))
	}
}

func calcLcs(strX, strY string) int {
	return lcs(strX, strY, Position{0, 0}, make(map[Position]int))
}

func lcs(strX string, strY string, position Position, memo map[Position]int) int {
	if position.X >= len(strX) || position.Y >= len(strY) {
		return 0
	}
	if v, ok := memo[position]; ok {
		return v
	}
	if strX[position.X] == strY[position.Y] {
		result := lcs(strX, strY, Position{position.X + 1, position.Y + 1}, memo)
		memo[position] = result + 1
		return memo[position]
	} else {
		result1 := lcs(strX, strY, Position{position.X + 1, position.Y}, memo)
		result2 := lcs(strX, strY, Position{position.X, position.Y + 1}, memo)
		var result int
		if result1 > result2 {
			result = result1
		} else {
			result = result2
		}
		memo[position] = result
		return result
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[1010][1010];
int main(){
  int q;
  cin>>q;
  for(int cn=0;cn<q;cn++){
    string x,y;
    cin>>x>>y;
    memset(dp,0,sizeof(dp));
    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-1]+1;
	dp[i][j]=max(dp[i-1][j],dp[i][j]);
	dp[i][j]=max(dp[i][j-1],dp[i][j]);
      }
    }
    cout<<dp[x.size()][y.size()]<<endl;
  }
  return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAX 1000

int main(){
  int n;
  cin>>n;
  string a,b;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    int C[MAX+1][MAX+1]={0};
    for(int i=1;i<=a.size();i++){
      for(int j=1;j<=b.size();j++){
        if(a[i-1]==b[j-1]) C[i][j]=C[i-1][j-1]+1;
        else{
          C[i][j]=max(C[i-1][j],C[i][j-1]);
        }
      }
    }
    cout<<C[a.size()][b.size()]<<endl;
  }

}

C++: #include <bits/stdc++.h>
using namespace std;
#define r(i,n) for(auto i=0;i<n;i++)
#define s(c) static_cast<int>((c).size())

int dp[1001][1001];
int main(){
	int n = 0;
	cin >> n;
	int x, y;
	string X, Y;
	r(k, n){
		cin >> X >> Y;
		x = X.length();
		y = Y.length();
		r(i, x+1)
			r(j, y+1)
				dp[i][j] = 0;

		r(i, x){
			r(j, y){
				if(X[i] == Y[j]){
					if(i == 0 || j == 0){
						dp[i][j] = 1;
					}else{
						dp[i][j] = dp[i-1][j-1] + 1;
					}
				}else{
					if(i == 0 || j == 0){
						dp[i][j] = 0;
					}else{
						dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
					}
				}

			}
		}
		cout << dp[x-1][y-1] << endl;
	}
	return 0;
}

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

using namespace std;

int maxa[1000];
int main(){
  int n;
  int k;
  int count;
  cin>>n;
  char X[1001];
  char Y[1001];
  for(int i=0;i<n;i++){
    cin>>X;
    cin>>Y;

    for(int a=0;X[a]!='\0';a++){
    count=0;
    k=0;
      for(int s=a;X[s]!='\0';s++){
	for(int j=k;Y[j]!='\0';j++){
	  if(X[s]==Y[j]){
	    //	    cout<<X[s];
	    count++;
	    k=j;
	    break;
	  }
	}
      }
      if(maxa[i]<count)maxa[i]=count;
    }
  }
  for(int i=0;i<n;i++){
    cout<<maxa[i]<<endl;
  }
  
  return 0;
}

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

int n;
string x, y;
bool mat[1000][1000];
int a[1000][1000];

int max(int i, int j) {
	int r = (!mat[i - 1][j] ? a[i - 1][j] : 0);
	r = ((!mat[i][j - 1] && a[i][j - 1] > r) ? a[i][j - 1] : r);
	return r > a[i - 1][j - 1] ? r : a[i - 1][j - 1];
}

int main(){
	cin >> n;
	for (int ia = 0; ia < n; ia++) {
		int ans = 0, p = 0;
		cin >> x >> y;
		for (int i = 0; i < x.length(); i++) {
			mat[i][0] = x[i] == y[0] ? true : false;
			a[i][0] = mat[i][0] ? 1 : 0;
		}
		for (int i = 1; i < y.length(); i++) {
			mat[0][i] = x[0] == y[i] ? true : false;
			a[0][i] = mat[0][i] ? 1 : 0;
		}
		for (int i = 1; i < x.length(); i++) {
			for (int j = 1; j < y.length(); j++) {
				mat[i][j] = x[i] == y[j] ? true : false;
				a[i][j] = max(i, j) + (mat[i][j] ? 1 : 0);
				if (a[i][j] > p) p = a[i][j];
			}
		}
		if (a[x.length() - 1][0] > p) p = a[x.length() - 1][0];
		if (a[0][y.length() - 1] > p) p = a[0][y.length() - 1];
		cout << p << endl;
	}


	return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <queue>
#include <string>
#include <tuple>
using namespace std;

int solve(string& X, string& Y){
  int x_size = X.size();
  int y_size = Y.size();
  
  vector<vector<int>> dp(y_size+1);
  for(int i=0;i<=y_size;i++){
    for(int j=0;j<=x_size;j++){
      dp[i].push_back(0);
    }
  }
  
  for(int i=1;i<=y_size;i++){
    for(int j=1;j<=x_size;j++){
      if(Y[i-1]==X[j-1]){
        dp[i][j] = dp[i-1][j-1]+1;
      } else {
        dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
      }
    }
  }
  
  /*
  for(int i=0;i<=y_size;i++){
    for(int j=0;j<=x_size;j++){
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  */
  
  return dp[y_size][x_size];
}

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

C++: #include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define ALL(a) (a).begin(),(a).end()
 
using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;
 
const ll MOD  = 1e9+7;
const ll LINF = 1LL << 60;
const int INF = 1e9 + 7;

int solve(string x, string y){
    //dp[i][j] xの i文字目、yのj文字目までの最長共通部分列の長さ
    int dp[1010][1010] = {};

    rep(i, x.size())rep(j, y.size()){
        dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
        if(x[i] == y[j])dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j]+1);
    }

    return dp[x.size()][y.size()];
}
int main(){
    int q;
    cin >> q;

    int ans[q];

    rep(i, q){
        string x, y;
        cin >> x >> y;
        
        ans[i] = solve(x, y);
    }

    rep(i, q)cout << ans[i] << endl;
}

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

const int N = 1000;

int lcs(string a, string b) {
  int C[N+1][N+1];
  int n = a.size();
  int m = b.size();
  for (int i = 0; i <= n; i++) C[i][0] = 0;
  for (int i = 0; i <= m; i++) C[0][i] = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i-1] == b[j-1]) {
        C[i][j] = C[i-1][j-1] + 1;
      } else {
        C[i][j] = C[i][j-1] > C[i-1][j] ? C[i][j-1] : C[i-1][j];
      }
    }
  }
  return C[n][m];
}

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

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

using namespace std;

int N=1000;

int LCS(string x,string y){
	int c[N+1][N+1],LCSlen=0;
	int m=x.size();
	int n=y.size();
	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][j-1],c[i-1][j]);
			LCSlen=max(LCSlen,c[i][j]);
		}
	}
	return LCSlen;
}

int main(){
	string x,y;
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		cin>>x;
		cin>>y;
		printf("%d\n",LCS(x,y));
	}
}