Go: package main

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

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

/*********************************************************************
長さmの列X_(m)と長さnの列Y_(n)の最長共通部分列LCSは、
	(1)X[m] == Y[n]ならば、X_(m-1)とY_(n-1)のLCSにX[m]を繋げたもの
	(2)X[m] != Y[n]ならば、X_(m-1)とY_(n)のLCSもしくはX_(m)とY_(n-1)のLCS
	のいずれかのうち長い方

c[m+1][n+1]：c[i][j]にX_(i)とY_(j)のLCSの長さを格納する二次元配列
*********************************************************************/

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		s1 := scanner.Text()
		scanner.Scan()
		s2 := scanner.Text()
		s1, s2 = s1 + " ", s2 + " "
		c := make([][]int, len(s1))
		for i := range c {
			c[i] = make([]int, len(s2))
		}

		LCS := 0

		for i := 1; i < len(s1); i++ {
			for j := 1; j < len(s2); 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])
				}
				LCS = max(LCS, c[i][j])
			}
		}
		fmt.Println(LCS)
	}
}

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