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 <iostream>
#include <string>
#include <algorithm>
using namespace std;

int lcs(string X, string Y) {
    int m = X.size();
    int n = Y.size();
    int c[m + 1][n + 1];
    c[0][0] = 0;
    for (int i = 1; i <= m; i++) c[i][0] = 0;
    for (int j = 1; j <= n; j++) c[0][j] = 0;

    int max_len = 0;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (X[i - 1] == Y[j - 1]) {
                c[i][j] = c[i - 1][j - 1] + 1;
            } else {
                c[i][j] = max(c[i - 1][j], c[i][j - 1]);
            }
            max_len = max(max_len, c[i][j]);
        }
    }

    return max_len;
}

int main() {
    int n;
    cin >> n;
    string s1, s2;
    int max_len[n];
    for (int i = 0; i < n; i++) {
        cin >> s1 >> s2;
        max_len[i] = lcs(s1, s2);
    }

    for (int i = 0; i < n; i++) {
        cout << max_len[i] << endl;
    }

    return 0;
}

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


int LCS(const string& a, const string& b) {
    int ans = 0;
    const int len_a = a.length(), len_b = b.length();

    int dp[len_a + 1][len_b + 1] = {};
    for(int i=1; i<=len_a; i++) {
	for(int j=1; j<=len_b; j++) {
	    dp[i][j] = max(dp[i][j], dp[i-1][j] );
	    dp[i][j] = max(dp[i][j], dp[i][j-1] );
	    dp[i][j] = max(dp[i][j], dp[i-1][j-1] + (a[i-1] == b[j-1] ? 1 : 0) );
	}
    }
    ans = dp[len_a][len_b];
    return ans;
}

int main() {

    int N;
    cin >> N;

    for(int i=0; i<N; i++) {
	string a,b;
	cin >> a >> b;
	cout << LCS(a,b) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#define N 1000
using namespace std;
 
int lcs(string S1,string S2){
    int c[N+1][N+1];
    int S1_size = S1.size();
    int S2_size = S2.size();
     
    for(int i=1;i<S1_size;i++) c[i][0] = 0;
    for(int i=1;i<S2_size;i++) c[0][i] = 0;
     
    S1 = ' '+S1;
    S2 = ' '+S2;
     
    int max_lcs=0;
    for(int j=1;j<=S1_size;j++){
        for(int k=1;k<=S2_size;k++){
            if(S1[j]==S2[k]){
                c[j][k] = c[j-1][k-1] + 1;
            }else{
                c[j][k] = max(c[j-1][k],c[j][k-1]);
            }
            max_lcs = max(max_lcs,c[j][k]);
        }
    }
    return max_lcs;
}
 
int main() {
    // your code goes here
    int q;
    string str1,str2;
    cin>>q;
    for(int i=0;i<q;i++){
        cin>>str1>>str2;
        cout<<lcs(str1,str2)<<endl;
    }
    return 0;
}

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

int Max(int &a,int b){return a=max(a,b);}

int LCS(string &a,string &b){
  int n = a.size();
  int m = b.size();
  vector <vector<int> > dp(n+1,vector<int>(m+1));

  for(int i=0;i<=n;i++)
    for(int j=0;j<=m;j++){
      if(i<n && j<m && a[i] == b[j]) Max(dp[i+1][j+1],dp[i][j]+1);
      if(i<n)Max(dp[i+1][j],dp[i][j]);
      if(j<m)Max(dp[i][j+1],dp[i][j]);

    }
  return dp[n][m];
}

int main(){
  int q;
  cin>>q;
  while(q--){
    string a,b;
    cin>>a>>b;
    cout<<LCS(a,b)<<endl;
  }
  return 0;
}

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

// ???????????§???????????§?????§??????
int DP[1024][1024] = {};

int main(){
    int N;
    cin >> N;
    for(int c=0; c<N; ++c){
        string X,Y;
        cin >> X >> Y;
        int xlen = (int)X.size();
        int ylen = (int)Y.size();

        for(int i=0;i<=xlen;++i){
          DP[i][0] = 0;
        }
        for(int i=0;i<=ylen;++i){
          DP[0][i] = 0;
        }

        for(int i=1;i<=xlen;++i){
            for(int j=1;j<=ylen;++j){
                if(X[i-1] == Y[j-1]){ // X???i???????????¨Y???j?????????????????????????????????index?????????????????¨?????¨??????
                    DP[i][j] = DP[i-1][j-1] + 1;
                } else {
                    DP[i][j] = max(DP[i-1][j], DP[i][j-1]);
                }
            }
        }
        cout << DP[xlen][ylen] << endl;
    }
}

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

int LCS(int len[],char S[][1001]){//???????????????2?????§?????????
    int DP[1001][1001] = {};
    for (int i = 1; i <= len[0]; i++) {
        for (int j = 1; j <= len[1]; j++) {
            if(S[0][i-1] == S[1][j-1]){
                DP[i][j] = DP[i-1][j-1]+1;
            }else{
                DP[i][j] = max(DP[i-1][j],DP[i][j-1]);
            }
        }
    }
    
    return DP[len[0]][len[1]];
}

void solve(){
    char S[2][1001];
    int len[2];
    scanf("%s %s",S[0],S[1]);
    len[0] = strlen(S[0]);
    len[1] = strlen(S[1]);
    int ll;
    ll = LCS(len,S);
    printf("%d\n",ll);
}

int main(){
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        solve();
    }
    
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include<string.h>
using namespace std;
int LCS(char *x,char *y);
char x[1001], y[1001];
int lcs[1001][1001];
int main()
{
	int q,i,j;

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> x;
		cin >> y;
		cout << LCS(x, y) << endl;
	}
}

int LCS(char *x, char *y)
{
	int i, j;
	int len_x, len_y;
	len_x = strlen(x);
	len_y = strlen(y);
	
	for (i = 0; i < len_x; i++) {
		if (x[0] == y[i]) {
			lcs[0][i] = 1;
		}
		else {
			lcs[0][i] = 0;
		}
	}
	for (i = 0; i < len_x; i++) {
		if (x[i] == y[0]) {
			lcs[i][0] = 1;
		}
		else {
			lcs[i][0] = 0;
		}
	}

	for (i = 0; i < len_x; i++) {
		for (j = 0; j < len_y; j++) {
			if (x[i] == y[j]) {
				lcs[i][j] = lcs[i - 1][j - 1] + 1;
				//printf("lcs[%d][%d]=%d\n", i, j, lcs[i - 1][j - 1] + 1);
			}
			if (x[i] != y[j]) {
				lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]);
				//printf("lcs[%d][%d]=%d\n", i, j, max(lcs[i - 1][j], lcs[i][j - 1]));
			}
		}
	}
	//cout << endl;

	for (i = 0; i < len_x; i++) {
		for (j = 0; j < len_y; j++) {
			//printf("%d ", lcs[i][j]);
		}
		//cout << endl;
	}
	//cout << "len_x=" << len_x << endl << "len_y=" << len_y << endl;
			
	return lcs[len_x-1][len_y-1];
}