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 main()
{
    int N; cin >> N;
    string S1,S2;
    int n,m;
    for (int i=0;i<N;i++)
    {
        cin >> S1 >> S2;
        n=S1.size();m=S2.size();
        vector<vector<int>> dp(n+1,vector<int> (m+1,0));
        for (int i=1;i<n+1;i++)
        {
            for (int j=1;j<m+1;j++)
            {
                if (S1[i-1]==S2[j-1])
                {
                    dp[i][j]=max({dp[i-1][j],dp[i][j-1],dp[i-1][j-1]+1});
                }
                else
                {
                    dp[i][j]=max({dp[i-1][j],dp[i][j-1]});
                }
                
            }
        }
        cout << dp[n][m] << endl;
    }
    return 0;
}

C++: import sys

def dp_init(m, n):
    return [[0] * n] * m

def solve(str_a, str_b):
    dp = dp_init(len(str_a), len(str_b))
    return rec(len(str_a), len(str_b), str_a, str_b, dp)

def rec(i, j, str_a, str_b, dp):
    if i == 0 or j == 0:
        return 0
    if dp[i][j] > 0:
        return dp[i][j]
    if str_a[i-1] == str_b[j-1]:
        dp[i][j] = rec(i-1, j-1, str_a, str_b, dp) + 1
        return dp[i][j]
    dp[i][j] = max(rec(i, j-1, str_a, str_b, dp), rec(i-1, j, str_a, str_b, dp))
    return dp[i][j]

nb_pairs = int(input())
pairs = []
for i in range(0, nb_pairs):
    pairs.append((input(), input()))

for i in range(0, nb_pairs):
    str_a, str_b = pairs[i]
    print(solve(str_a, str_b))

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

using namespace std;



int n;
string a, b;
string x[10000], y[10000];




int main(){
  cin >> n;
  while(n != 0){
    n = n -1;
    int longest[1000][1000];
    cin >> a;
    cin >> b;
    a = ' ' + a;
    b = ' ' + b;
    int mm = 0;
    /*
    for(int i = 0; i < a.size(); ++i){
      for(int j = 0; j < b.size(); ++j){
        
        x[i] = a;
        y[j] = b;
      }
    }
    */
   //longest[0][0] =0;
   for(int i = 1; i <= a.size(); ++i){
     longest[i][0] = 0;
    }
   for(int j = 1; j <= b.size(); ++j){
     longest[0][j] = 0;
   }
    for(int i = 1; i <= a.size(); ++i){
      for(int j = 1; j <= b.size(); ++j){
        
        /*
        if(i <= 0 || j <=0){
          longest[i][j] = 0;
        }
        */
        if(a[i] == b[j]){
          longest[i][j] = 1 + longest[i-1][j - 1];
        }
        else{
          longest[i][j] = max(longest[i][j-1], longest[i-1][j]);
        }
        mm = max(mm, longest[i-1][j-1]);
      }
    }
    //cout << a[1] << endl;
    cout << mm << endl;
  }
}

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

using namespace std;

int lcs(string s1, string s2) {
    int c[1005][1005];
    int m = s1.length();
    int n = s2.length();
    s1 = " " + s1;
    s2 = " " + s2;
    int max1 = 0;
    for (int i = 0; i <= m; i++) {
        c[i][0] = 0;
    }
    for (int i = 0; i <= n; i++) {
        c[0][i] = 0;
    }
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (s1[i] == s2[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;
    cin >> q;
    string s1, s2;
    for (int k = 0; k < q; k++) {
        cin >> s1;
        cin >> s2;
        cout << lcs(s1, s2) << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int dp[1001][1001];
int n;
int strlen_1(char a[]){int len=0;while(a[len]!='\0')len++;return len;}
int solve(char a[],char b[])
{	
	
	int maxn=0;
	int len1=strlen_1(a),len2=strlen_1(b);
	

	for(int i=1;i<=strlen_1(a);i++)
		for(int j=1;j<=strlen_1(b);j++)
		{
			if(a[i-1]==b[j-1])
				dp[i][j]=dp[i-1][j-1]+1;
			else 
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);//这个只是表示某个状态的不是最大的，这个是自己设定的
			maxn=max(maxn,dp[i][j]);
		}
		return maxn;
		
		
	
}
char a[1001],b[1001];
int main()
{
	
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a>>b;
		cout<<solve(a,b)<<endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<list>

using namespace std;

int n;
string x,y;

vector<vector<int> > q(1001,vector<int>(1001,-1));

int search(int i,int j){
  int ans;
  if(q[i][j] != -1) return q[i][j];
  if(i==0 || j==0) return 0;
  if( x[i] == y[j] ){
    ans=max( {search(i-1,j-1)+1,search(i-1,j),search(i,j-1)} );
    q[i][j]=ans;
    return ans;
  }
  else{
    ans=max( search(i-1,j),search(i,j-1) );
    q[i][j]=ans;
    return ans;
  }

}


int main(void){

  cin>>n;

  for(int i=0;i<n;i++){
    cin>>x;
    cin>>y;

    for(int ix=0;ix<1001;ix++){
      for(int iy=0;iy<1001;iy++){
        q[ix][iy]=-1;
      }
    }

    search( x.size(),y.size() );
    cout << q[x.size()][y.size()] << 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++)
	    table[i][0] = x[i] == y[0];
	for (int j = 0; j < yl; j++)
	    table[0][j] = x[0] == y[j];

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

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

    return 0;
}

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

string S,T;
int x[10000],y[10000];
int dp[10000][10000];
char U[5]="abcd";
int MAXN;

int main(){
	int q;
	cin>>q;
	for(int i=0;i<q;i++){
		MAXN=0;
		cin>>S>>T;
		for(int i=0;i<S.size();i++){
			for(int j=0;j<4;j++){
				if(S[i]==U[j]){
					x[i+1]=j;
				}
			}
		}
		for(int i=0;i<T.size();i++){
			for(int j=0;j<4;j++){
				if(T[i]==U[j]){
					y[i+1]=j;
				}
			}
		}
		for(int i=0;i<=T.size();i++){
			for(int j=0;j<=S.size();j++){
				if(i==0 || j==0){
					dp[i][j]=0;
				}
				else if(x[j]==y[i]){
					dp[i][j]=dp[i-1][j-1]+1;
				}
				else{
					dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
				}
			}
		}
		for(int i=0;i<=T.size();i++){
			for(int j=0;j<=S.size();j++){
				MAXN=max(MAXN,dp[i][j]);
			}
		}
		cout<<MAXN<<endl;
	}
	return 0;
}