Go: package main

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

const max int = 1000 + 1
var A [][]int

func lcs(x, y string) int {
	x = " " + x
	y = " " + y
	for i:=0;i<len(x); i++ {
		for j:=0;j<len(y); j++ {
			A[i][j] = 0
		}
	}
	for i:=1; i<len(x); i++ {
		for j:=1; j<len(y); j++ {
			if x[i] == y[j] {
				A[i][j] = A[i-1][j-1] + 1
			} else if A[i-1][j] > A[i][j-1] {
				A[i][j] = A[i-1][j]
			} else {
				A[i][j] = A[i][j-1]
			}
		}
	}
	return A[len(x)-1][len(y)-1]
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num 
}

func nextStr(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}


func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	A = make([][]int, max)
	for i := range A {
		A[i] = make([]int, max)
	}
	n := nextInt(scanner)
	for i:=0; i<n; i++ {
		x := nextStr(scanner)
		y := nextStr(scanner)
		fmt.Printf("%d\n", lcs(x, y))
	}
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

const int INF = 1 << 30;
const int MOD = 1000000007;
const int MAX = 1000;
const int N = 150;

int dp[MAX+1][MAX+1];

int lcs(string X, string Y) {
	rep(i, X.size()+1) { rep(j, Y.size()+1) {
		if (i == 0 || j == 0)
			dp[i][j] = 0;
		else if (X[i-1] == Y[j-1])
			dp[i][j] = dp[i-1][j-1] + 1;
		else
			dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
	} }
	return dp[X.size()][Y.size()];
}

int main() {
	int q; cin >> q;
	string X, Y;
	rep(i, q) {
		cin >> X >> Y;
		cout << lcs(X, Y) << endl;
	}
}

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++) {
		for (j = 0; j < len_y; j++) {
			lcs[i][j] = '\0';
		}
		//cout << endl;
	}
	i = 0;
	while (x[i] != y[0]) {
		lcs[i][0] = 0;
		i++;
		if (i == len_x) break;

	}
	for (j = i; j < len_x; j++) {
		lcs[j][0] = 1;
	}
	while (y[i] != x[0]) {
		lcs[0][i] = 0;
		i++;
		if (i == len_y) break;
	}
	for (j = i; j < len_y; j++) {
		lcs[0][j] = 1;
	}

	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[i-1][j-1];
}

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

using namespace std;

static const int N = 1000;

int lcs(string X, string Y) {
  int c[N + 1][N + 1];
  int m = X.size();
  int n = Y.size();

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

int main() {
  string s1, s2;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1 >> s2;
    cout << lcs(s1, s2) << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <utility>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <cmath>
#include <queue>
using namespace std;
typedef long long ll;
const int INF = 1<<30; 
const int MOD = 1e9 + 7;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
    int n;
    cin >> n;
    string s, t;
    for(int i = 0; i < n; i++)
    {
        cin >> s >> t;
        int dp[1100][1100] = {};
        for(int i = 0; i < s.size(); i++)
        {
            for(int j = 0; j < t.size(); j++)
            {
                if(s[i] == t[j])
                {
                    dp[i + 1][j + 1] = dp[i][j] + 1;
                }
                else 
                {
                    dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
                }
            }
        }
        cout << dp[s.size()][t.size()] << endl;
    }
	return 0;
}

C++: #include <bits/stdc++.h>
#include <cmath>
#include <numeric>
using namespace std;
#define rep(i,a,b) for(int64_t i=(a); i<(b); ++i) // a ≦ i < b 
#define Rrep(i,a,b) for(int64_t i=(a);i>=(b);--i) // reverse repeat. a から b まで減少．
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend() //逆イテレータ
#define RANGE(a,b,c) (a).begin()+b,(a).begin()+c // コンテナ a の 要素 b から c へのイテレータ
#define MOD 1000000007
#define INF 1000000000
typedef pair<int64_t, int64_t> PII;
typedef vector<int64_t> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef vector<PII> VP;

int main() {
cin.tie(0);
ios::sync_with_stdio(false);

int q; cin >> q;
rep(i, 0, q){
    string X, Y; cin >> X >> Y;
    // lsc[x][y] = X の x文字目 とYの y 文字目まで を見た時のlcs
    VVI lcs(X.size()+1, VI(Y.size() + 1, 0));
    rep(x, 1, X.size()+1){
        rep(y, 1, Y.size()+1){
            if (X[x-1] == Y[y-1])
                lcs[x][y] = lcs[x-1][y-1] + 1;
            else
                lcs[x][y] = max(lcs[x][y -1], lcs[x-1][y]);
        }
    }

    cout << lcs[X.size()][Y.size()] << "\n";
}

}

// 境界，出力文字列 チェック
// 可読性優先．高速化次点．
// まずは全探索，分割統治，次にDP
// 制限を見る
// 偶奇，逆から，ソート，出現回数，出現位置，DP， 余事象，包除
// データ構造． 問題の特徴量
// 存在判定：構成方法，入力の特徴
// gcd, lcm ,素因数分解．
// 例外を十分に含む一般化．想像力の限界
// 小さい系から例示
// 始めは過剰に例示・場合分けしてもいい．各場合を確実に対処．
// 自明な例から処理，除外．
// 小数のときは，精度の設定する．doubel 変数に数値を入力するときは 123. とする．
// テストケース作成は数表あり

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

int lengthOfLcs_r(const char x[], const char y[], int ix, int iy, int xsize, int ysize, int **lengthTable) {
	if (ix == xsize || iy == ysize) {
		return 0;
	}
	if (lengthTable[ix][iy] != -1) {
		return lengthTable[ix][iy];
	}
	if (x[ix] == y[iy]) {
		return lengthTable[ix][iy] = lengthOfLcs_r(x, y, ix + 1, iy + 1, xsize, ysize, lengthTable) + 1;
	}
	return lengthTable[ix][iy] = max(lengthOfLcs_r(x, y, ix, iy + 1, xsize, ysize, lengthTable), lengthOfLcs_r(x, y, ix + 1, iy, xsize, ysize, lengthTable));
}

int lengthOfLcs(string x, string y) {
	int xsize = x.size(), ysize = y.size();

	int **lengthTable = new int*[xsize];
	for (int i = 0; i < xsize; ++i) {
		lengthTable[i] = new int[ysize];
		fill_n(lengthTable[i], ysize, -1);
	}

	int ret = lengthOfLcs_r(x.c_str(), y.c_str(), 0, 0, xsize, ysize, lengthTable);
	
	for (int i = 0; i < xsize; i++) {
		delete[] lengthTable[i];
	}
	delete[] lengthTable;
	
	return ret;
}

int main() {
	int q;
	string x, y;

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