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 <bits/stdc++.h>
using namespace std;
#define MAX 1001
int dp[MAX][MAX];
int lcs(string& s,string& t,int si,int ti){
	int ret = 0;
	if(si < 0 || ti < 0)return 0;
	if(dp[si][ti] != -1)return dp[si][ti];
	if( s[si] == t[ti] ){
		ret = max(ret, lcs(s,t,si-1,ti-1) + 1);
	}else{
		if(si > 0)ret = max(ret, lcs(s,t,si-1,ti));
		if(ti > 0)ret = max(ret, lcs(s,t,si,ti-1));
	}
	return dp[si][ti] = ret;
}

int main(void){
	int T;
	cin >> T;
	for(int k=0;k<T;k++){
		string s,t;
		cin >> s >> t;
		// memset(dp,0,sizeof(dp));
		for(int i=0;i<MAX;i++)for(int j=0;j<MAX;j++)
			dp[i][j] = -1;
		cout << lcs(s,t,s.size()-1,t.size()-1) << endl;;
	}
	return 0;
}

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

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

const long long MOD = 1000000007;

int main(){
  int q;
  cin >> q;
  rep(x,q){
      string s,t;
      cin >> s >> t;

     int a=s.size(),b=t.size();

     vector<vector<int>> dp(a+1,vector<int>(b+1,0));
     
     rep(i,a){
         rep(j,b){
             if(s.at(i)==t.at(j)) dp.at(i+1).at(j+1)=dp.at(i).at(j)+1;
             else dp.at(i+1).at(j+1) = max(dp.at(i+1).at(j),dp.at(i).at(j+1)); 
         }
     }
     cout << dp[a][b] << endl;
  }
}

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

using namespace std;

int main(){
  string X,Y;
  int n;
  cin>>n;
  int ans[n];
  int l=0;
 int point;

  for(int i=0;i<n;i++){
    ans[i]=0;
  }

  for(int m=0;m<n;m++){
  cin>>X>>Y;
 int N[X.size()+1][Y.size()+1];

  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      N[i][j]=0;
}
  }

 
 

  for(int i=1;i<=X.size();i++){
    for(int j=1;j<=Y.size();j++){
      if(X[i-1]==Y[j-1]){
	point=1;
	
      }else{
	point=0;
      }
       N[i][j]=max({N[i-1][j],N[i][j-1],N[i-1][j-1]+point});
       point=0;
}
   
  }
  // ans[l]=N[X.size()][Y.size()];

  cout<<N[X.size()][Y.size()]<<endl;
  // l++;

  }
  //  cout<<ans[0]<<endl;
  // cout<<ans[1]<<endl;
  //cout<<ans[2]<<endl;

 return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>

class TLcsSolver
{
    int FTable[1000][1000];
    const char* FArray1;
    const char* FArray2;
    const int Fn1;
    const int Fn2;

    int Count(int i, int j)
    {
        if(i == 0 || j == 0) return 0;
        if(FArray1[i-1] == FArray2[j-1]){
            return FTable[i-1][j-1] + 1;
        }
        return std::max(FTable[i-1][j], FTable[i][j-1]);
    }
    void Solve()
    {
        for(std::size_t i = 1; i < Fn1; ++i){
            for(std::size_t j = 1; j < Fn2; ++j){
                FTable[i][j] = Count(i, j);
            }
        }
    }
public:
    TLcsSolver(const char* a, const char* b)
     : FArray1(a), FArray2(b)
     , Fn1(std::strlen(a)), Fn2(std::strlen(b))
    {
        Solve();
    }
    int Answer()const
    {
        return FTable[FArray1.size()][FArray2.size()];
    }
};

int main()
{
    int n;
    std::scanf("%d", &n);
    char a[1000];
    char b[1000];
    for(int i = 0; i < n; ++i){
        std::fgets(a, 1000, stdin);
        std::fgets(b, 1000, stdin);
        std::printf("%d", TLcsSolver(a, b).Answer());
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF=1e18;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)


int main(){
    int q;
    cin >> q;
    rep(i, q){
        string A, B;
        cin >> A >> B;
        int la = A.length(), lb = B.length();
        vector<vector<int> > dp(la+1, vector<int>(lb+1, 0));
        rep(i, la){
            rep(j , lb){
                if(A.at(i) == B.at(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[la][lb] << endl;
    }
    
}

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

using namespace std;
typedef long long lint;

int dp[1111][1111];

void chmax(int& x, const int y) {
    x = max(x, y);
}

int main() {
    int Q;
    cin >> Q;
    for (int q = 0; q < Q; q++) {
        string X, Y;
        cin >> X >> Y;
        const int N = X.size(), M = Y.size();
        for (int i = 0; i <= N; i++) {
            for (int j = 0; j <= M; j++) {
                dp[i][j] = 0;
            }
        }
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                // X を進める
                chmax(dp[i+1][j], dp[i][j]);
                // Y を進める
                chmax(dp[i][j+1], dp[i][j]);
                // 同じ文字の場合はどちらも進めて +1
                if (X[i] == Y[j]) {
                    chmax(dp[i+1][j+1], dp[i][j]+1);
                }
            }
        }
        cout << max({dp[N][M], dp[N-1][M], dp[N][M-1]}) << endl;
    }
    return 0;
}