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 <string>

using namespace std;

int solve(string s1, string s2){
    int L[s1.size()+1][s2.size()+1];
    for(int i=0;i<s1.size();i++){
        for(int j=0;j<s2.size();j++){
            if(i==0 || j==0){
                L[i][j]=0;
            }
            else if(s1[i] == s2[j]){
                L[i][j] = 1 + L[i-1][j-1];
            }
            else{
                L[i][j] = max(L[i][j-1],L[i-1][j]);
            }
        }
    }
    return L[s1.size()][s2.size()];
}

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

C++: #include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[1001][1001];

int main(){
	int n; cin >> n;
	while (n--){
		string X, Y; cin >> X >> Y;
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= X.size(); ++i){
			for (int j = 1; j <= Y.size(); ++j){
				if (X[i] == Y[j]) dp[i][j] = max(dp[i][j-1],max(dp[i-1][j],dp[i-1][j-1]+1));
				else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
			}
		}
		cout << dp[(int)X.size()][(int)Y.size()] << '\n';
	}
}

C++: #include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
typedef pair<ll, ll> LLP;

const int INF = (1 << 30) - 1;
// const ll LL_INF = (1LL << 62) - 1;
// const ll MOD = 1e9 + 7;
// const double PI = M_PI;

int q;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> q;
  rep(i, q) {
    string x, y;
    cin >> x >> y;
    int dp[y.size() + 1][x.size() + 1];
    rep(j, y.size() + 1) {
      fill(dp[j], dp[j] + x.size() + 1, INF);
      dp[j][0] = 0;
    }
    fill(dp[0], dp[0] + x.size() + 1, 0);

    for (int j = 1; j <= y.size(); j++) {
      for (int k = 1; k <= x.size(); k++) {
        if (y[j - 1] == x[k - 1]) {
          dp[j][k] = dp[j - 1][k - 1] + 1;
        } else {
          dp[j][k] = max(dp[j][k - 1], dp[j - 1][k]);
        }
      }
    }

    cout << dp[y.size()][x.size()] << endl;
  }

  return 0;
}

C++: #include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;

int dat[1024][1024];
int getLcbLen(char* x, int xe, char* y, int ye){
	cout << xe << ":"<<ye<<endl;
	if(xe <= 0 || ye <= 0) return 0;
	if(dat[xe][ye] != -1) return dat[xe][ye];
	return dat[xe][ye] = (x[xe-1] == y[ye-1] ? 1 + getLcbLen(x, xe-1, y, ye-1) : max(getLcbLen(x, xe-1, y, ye), getLcbLen(x, xe, y, ye-1)));
}

int main() {
	int set;
	scanf("%d", &set);
	
	while(set--){
		char x[1024], y[1024];
		scanf("%s %s", x, y);
		
		for(int i = 0; i < 1024; i++){
			for(int k = 0; i < 1024; k++){
				dat[i][k] = -1;
			}
		}
		printf("%d\n", getLcbLen(x, (int)strlen(x), y, (int)strlen(y)));
	}
	return 0;
}

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

using namespace std;

int main(){
  int n;
  int k;
  int count;
  cin>>n;
  char X[1001];
  char Y[1001];
  for(int i=0;i<n;i++){
    cin>>X;
    cin>>Y;
    k=0;
    count=0;
    for(int i=0;X[i]!='\0';i++){
      for(int j=k;Y[j]!='\0';j++){
	if(X[i]==Y[j]){
	  count++;
	  k=j;
	  break;
	}
      }
    }
    cout<<count<<endl;
  }
  return 0;
}

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

string X;
string Y;

int calc(int x, int y){
    if (X[x] == '\0') return 0;
    int tmp1 = calc(x + 1, y);
    int y_next = Y.find(X[x], y);
    if (y_next == string::npos) return 0;
    int tmp2 = calc(x + 1, y_next + 1) + 1;
    return max(tmp1, tmp2);
}

int main(){
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> X;
        cin >> Y;
        cout << calc(0, 0) << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#define MAX 1001
using namespace std;
int lcs(string A,string B){
    int X = A.size();
    int Y = B.size();
    int count[MAX][MAX] = {0};
    for(int i=1;i<Y+1;i++){
        for(int j=1;j<X+1;j++){
            if(B[i-1]==A[j-1]){
                count[i][j] = count[i-1][j-1] + 1;
            }else{
                count[i][j] = max(count[i][j-1],count[i-1][j]);
            }
            //cout << count[i][j];
        }
        //cout << endl;
    }
    return count[Y][X];
}
int main(void){
    int n;
    cin >> n;
    for(int i=0;i<n;i++){
        string A,B;
        cin >> A >> B;
        cout << lcs(A,B) << endl;
    }
    return 0;
}