Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const N = 1001

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func lcs(x, y string) int {
	var c [N][N]int
	m := len(x)
	n := len(y)
	var maxl int

	x = " " + x
	y = " " + y

	for i := 1; i <= m; i++ {
		for 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
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())

	var out bytes.Buffer
	for i := 0; i < q; i++ {
		sc.Scan()
		s1 := sc.Text()
		sc.Scan()
		s2 := sc.Text()
		out.WriteString(fmt.Sprintf("%d\n", lcs(s1, s2)))
	}
	return strings.TrimRight(out.String(), "\n")
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int lcs(string x, string y) {
    x = ' ' + x;
    y = ' ' + y;
    int m = x.size();
    int n = y.size();
    vector<vector<int>> c(m, vector<int>(n, 0));
    rep(i, m) {
        rep(j, n) {
            if (i == 0 || j == 0) continue;
            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]);
        }
    }
    return c[m-1][n-1];
}

int main() {
    int n;
    cin >> n;
    rep(i, n) {
        string s1, s2;
        cin >> s1;
        cin >> s2;
        cout << lcs(s1, s2) << endl;
    }
}

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

string X;
string Y;

int calc(int x, int y, vector<vector<int> >& Memo){
    if (X[x] == '\0' || Y[y] == '\0') return 0;
    if (Memo[x][y] != -1) return Memo[x][y];

    int unuse = calc(x + 1, y, Memo);
    int use = 0;
    int y_target = Y.find(X[x], y);
    if (y_target == string::npos) {
        use = 0;
    } else {
        use = calc(x + 1, y_target + 1, Memo) + 1;
    }
    return Memo[x][y] = max(unuse, use);
}

int main(){
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> X;
        cin >> Y;
        vector<int> tmp(Y.size(), -1);
        vector<vector<int> > Memo(X.size(), tmp);
        cout << calc(0, 0, Memo) << endl;
    }

    return 0;
}

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