Go: package main

/*
最長共通部分列
*/

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

func larger(l, r int) int {
	if l > r {
		return l
	}
	return r
}

var memo [1001][1001]int

func countLCS(x, y string) int {
	x = " " + x
	y = " " + y
	max := 0
	for xi := 1; xi < len(x); xi++ {
		for yi := 1; yi < len(y); yi++ {
			if x[xi] == y[yi] {
				memo[xi][yi] = memo[xi-1][yi-1] + 1
			} else {
				memo[xi][yi] = larger(memo[xi-1][yi], memo[xi][yi-1])
			}
			max = larger(max, memo[xi][yi])
		}
	}
	return max
}

func main() {
	scanner := makeScanner(1024)
	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		x := eGetLine(scanner)
		y := eGetLine(scanner)
		fmt.Println(countLCS(x, y))
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

type ints []int

func (s ints) String() string {
	var buf bytes.Buffer
	buf.WriteString("[")
	sep := ""
	for _, v := range s {
		buf.WriteString(fmt.Sprintf("%s%d", sep, v))
		sep = ", "
	}
	buf.WriteString("]")
	return buf.String()
}

func reverse(a ints) ints {
	r := make(ints, len(a))
	for i := 0; i < len(a); i++ {
		r[i] = a[len(a)-1-i]
	}
	return r
}
func max(a ints) (int, bool) {
	if len(a) == 0 {
		return 0, false
	}
	m := a[0]
	for _, e := range a {
		if e > m {
			m = e
		}
	}
	return m, true
}
func min(a []int) int {
	m := a[0]
	for _, v := range a {
		if v < m {
			m = v
		}
	}
	return m
}

func sum(a ints) int {
	if len(a) == 0 {
		return 0
	}
	sum := 0
	for _, e := range a {
		sum += e
	}
	return sum
}
func find(n int, a ints) int {
	for i, v := range a {
		if n == v {
			return i
		}
	}
	return -1
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <stack>
#include <vector>
#include <algorithm>
#include <cmath>
#include <climits>
#include <cstring>
#include <queue>
#include <string>

using namespace std;

const double PI = 3.14159265358979;

int dp[1001][1001];

void lcs(string x, string y) {

	for (int i = 0; i < x.length(); i++) {
		dp[i][0] = 0;
	}
	for (int i = 0; i < y.length(); i++) {
		dp[0][i] = 0;
	}

	for (int i = 1; i <= x.length(); i++) {
		for (int j = 1; j <= y.length(); j++) {

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


int main() {

	int n; cin >> n;

	for (int i = 0; i < n; i++)	{
		string big, small;
		cin >> big >> small;
		
		lcs(big, small);
		int res = dp[big.length()][small.length()];
		cout << res << endl;
	}
	

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <queue>
#include <map>
#include <memory.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define INF ((int)1e9)
#define INFLL ((ll)1e18)
#define MOD (1000000007LL)


int main(int argc, const char * argv[]) {
    int Q;
    string X, Y;
    cin >> Q;
    for (int q = 0; q < Q; ++q) {
        cin >> X >> Y;
        int DP[1001][1001];
        memset(DP, 0, sizeof(DP));
        for (int sx = 1; sx <= X.size(); ++sx) {
            for (int sy = 1; sy <= Y.size(); ++sy) {
                DP[sx][sy] = max(DP[sx-1][sy], DP[sx][sy-1]);
                if (X[sx-1] == Y[sy-1]) {
                    DP[sx][sy] = max(DP[sx][sy], DP[sx-1][sy-1]+1);
                }
            }
        }
        cout << DP[X.size()][Y.size()] << endl;
    }
    return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;

#define N_MAX 1000

char X[N_MAX];
char Y[N_MAX];
char LCS[N_MAX][N_MAX];


int longest(int x, int y)
{
    for (int i = 0; i < x+1; i++) {
        for (int j = 0; j < y+1; j++) {
            if (i == 0 || j == 0) {
                LCS[i][j] = 0;
//                printf("0 LCS[%d][%d] = %d\n", i, j, LCS[i][j]);
            }
            else if (X[i] == Y[j]) {
                LCS[i][j] = LCS[i-1][j-1] + 1;
//                printf("1 LCS[%d][%d] = %d\n", i, j, LCS[i][j]);
            }
            else {
                LCS[i][j] = (LCS[i-1][j] > LCS[i][j-1]) ? LCS[i-1][j] : LCS[i][j-1]; 
//                printf("LCS[%d][%d] = %d\n", i, j, LCS[i][j]);
            }
        }
    }

    return LCS[x][y]; 
}

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

    cin >> q;

    for (int i = 0; i < q; i++) {
        scanf("%s", X);
        x = strlen(X);
        scanf("%s", Y);
        y = strlen(Y);
        cout << longest(x, y) << endl;
    }
        
    return 0;
}

C++: // 最長共通部分裂問題

#include <bits/stdc++.h>

#define rep(i, a, n)  for(ll i=a; i<n; i++)
#define per(i, a, n)  for(ll i=n-1; i>=a; i--)
#define fill0(n)  setfill('0') << right << setw(n)
#define all(x)  (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define LONGMAX 1e18
#define INTMAX 1000000000

using namespace std;
typedef long long ll;
typedef pair<ll ,ll> P;
const ll mod = 1e9+7;
const long double EPS = 0.0000000001;


int n;
int main(){
	cin >> n;
	for(int a = 0; a < n; a++){
		string x,y;
		cin >> x;
		cin >> y;

		// dp start
		int W = x.size(); int H = y.size();
		int dp[H+1][W+1] = {0};
		for(int i = 0; i < H+1; i++){
			dp[i][0] = 0;
		}
		for(int i = 0; i < W+1; i++){
			dp[0][i] = 0;
		}
		for(int i = 1; i < H+1; i++){
			for(int j = 1; j < W+1; j++){
				if(x[j-1] == y[i-1]){
					dp[i][j] = max(max(dp[i-1][j-1] + 1, dp[i-1][j]), dp[i][j-1]);
				}else{
					dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
				}
			}
		}
		// cout << endl;
		// for(int i = 0; i < H+1; i++){
		// 	for(int j = 0; j < W+1; j++){
		// 		cout << dp[i][j] << " ";
		// 	}
		// 	cout << endl;
		// }

		cout << dp[H][W] << endl;
	}
}

C++: #include <iostream>
#include <vector>

using ll = long long;
using namespace std;

/* initialize */
int lcs(string A, string B){
    int m = A.length(), n = B.length();
    int c[m+1][n+1];
    int maxl = 0;
    A = ' ' + A;
    B = ' ' + B;
    for (int i = 0; i <= m; ++i) c[i][0]=0;
    for (int i = 1; i <= n; ++i) c[0][i]=0;
    for (int i = 1; i <= m; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (A[i]==B[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;
};
/* main */
int main() {
    int n;
    cin >> n;
    string s1, s2;
    for (int i = 0; i < n; ++i) {
        cin >> s1 >> s2;
        int s = lcs(s1, s2);
        cout << s << "\n";
    }
    return 0;
}