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;

vector<vector<int> > D(2000, vector<int>(2000, -1));

// Xのi番目以前とYのj番目以前の最長共通部分列の大きさを返す
int lcs(string &X, string &Y, int i, int j)
{
  // メモされているとき
  if (D.at(i).at(j) > -1)
  {
    return D.at(i).at(j);
  }

  // ベースケース
  if (i == 0)
  {
    for (int tmp = 0; tmp <= j; tmp++)
    {
      if (X.at(i) == Y.at(tmp))
      {
        D.at(i).at(j) = 1;
        return 1;
      }
    }
    D.at(i).at(j) = 0;
    return 0;
  }
  if (j == 0)
  {
    for (int tmp = 0; tmp <= i; tmp++)
    {
      if (X.at(tmp) == Y.at(j))
      {
        D.at(i).at(j) = 1;
        return 1;
      }
    }
    D.at(i).at(j) = 0;
    return 0;
  }
  // 再帰ステップ
  int M = 0;
  if (i > 0)
  {
    M = max(M, lcs(X, Y, i - 1, j));
  }
  if (j > 0)
  {
    M = max(M, lcs(X, Y, i, j - 1));
  }
  if ((i > 0 && j > 0) && (X.at(i)) == Y.at(j))
  {
    M = max(M, lcs(X, Y, i - 1, j - 1) + 1);
  }
  D.at(i).at(j) = M;
  return M;
}

int main()
{
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    string X, Y;
    cin >> X;
    cin >> Y;
    cout << lcs(X, Y, X.size() - 1, Y.size() - 1) << endl;
    vector<vector<int> > E(2000, vector<int>(2000, -1));
    D = E;
  }
}

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

using namespace std;

int q;
char X[1000], Y[1000];
int dp[1001][1001];

int main(void) {

    scanf("%d", &q);

    for (int i = 0; i < q; ++i) {
        scanf("%s %s", X, Y);
        int xlen = strlen(X), ylen = strlen(Y);
        for (int x = 0; x < xlen; ++x) {
            for (int y = 0; y < ylen; ++y) {
                if (X[x] == Y[y]) {
                    dp[x + 1][y + 1] = dp[x][y] + 1;
                }
                else {
                    if (dp[x + 1][y] > dp[x][y + 1]) {
                        dp[x + 1][y + 1] = dp[x + 1][y];
                    }
                    else {
                        dp[x + 1][y + 1] = dp[x][y + 1];
                    }
                }
            }
        }
        printf("%d\n", dp[xlen][ylen]);
    }

    return 0;
}

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

void solve(){
	string a,b;
	cin >> a >> b;
	int dp[1001][1001];
	memset(dp,0,sizeof(dp));
	for(int i=0;i<=a.size();i++){
		for(int j=0;j<=b.size();j++){
			if(i > 0)
				dp[i][j] = max(dp[i][j],dp[i-1][j]);
			if(j > 0)
				dp[i][j] = max(dp[i][j],dp[i][j-1]);
			if(i > 0 && j > 0)
				dp[i][j] = max(dp[i][j],dp[i-1][j-1] + (a[i-1] == b[j-1] ? 1 : 0));
		}
	}
	cout << dp[a.size()][b.size()] << endl;
}
int main(void) {
	int q;
	cin >> q;
	for(int i=0;i<q;i++){
		solve();
	}
	return 0;
}

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

int LCS(const string &a, const string &b) {
	vector<vector<int>> dp(a.size() + 1, vector<int>(b.size() + 1, 0));
	for (int i = 0; i <= a.size(); i++) {
		for (int j = 0; j <= b.size(); j++) {
			if (i > 0 && j > 0) {
				if (a[i - 1] == b[j - 1]) {
					dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
				}
				else {
					dp[i][j] = max(dp[i][j], dp[i - 1][j - 1]);
				}
				dp[i][j] = max(dp[i][j], dp[i][j - 1]);
				dp[i][j] = max(dp[i][j], dp[i - 1][j]);
			}
			else if (i > 0) {
				dp[i][j] = max(dp[i][j], dp[i - 1][j]);
			}
			else if (j > 0) {
				dp[i][j] = max(dp[i][j], dp[i][j - 1]);
			}
		}
	}
	return dp[a.size()][b.size()];
}

int main()
{
	int n;
	string X, Y;
	cin >> n;
	while (n--) {
		cin >> X >> Y;
		cout << LCS(X, Y) << endl;
	}
	return 0;
}

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

#define MAX 1000

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

    for (i = 1 ; i <= m ; i++ ) c[i][0] = 0;
    for (j = 1 ; j <= n ; j++ ) c[0][j] = 0;

    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][j-1], c[i-1][j]);
            }
            maxlen = max(maxlen, c[i][j]);
        }
    }
    return maxlen;
}

int main (void) {
    int N, i;
    string x, y;

    cin >> N;

    for(i = 0 ; i < N ; i++) {
        cin >> x;
        cin >> y;
        cout << lcs(x, y) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;

int main() {
    int q;
    cin >> q;

    int ans[q];

    rep(k, q) {
        string x, y;
        cin >> x >> y;

        int m = x.size();
        int n = y.size();

        int dp[m+1][n+1] = {};  // dp[i][j] : xのi文字目までとyのj文字目までのLCSの長さ(i, jは0-index)

        rep(i, m) {
            rep(j, n) {
                if(x[i] == y[j]) {
                    dp[i+1][j+1] = dp[i][j] + 1;
                }
                else {
                    dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
                }
            }
        }

        ans[k] = dp[m][n];
    }

    rep(i, q) {
        cout << ans[i] << "\n";
    }

    return 0;
}