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<vector>
#include<string>
#include<algorithm>

using namespace std;

string s, t;

vector<vector<int>> memo(2000, vector<int>(2000,-1));

//s???i??????????????§???t???j??????????????§?????£?????¨??????LCS
int solve(int i, int j) {
	if (memo[i][j] != -1)return memo[i][j];

	if (i == 0 && j == 0)return (s[0] == t[0]);
	if (i == 0)return memo[i][j] = (bool)(solve(i, j - 1) + (s[i] == t[j]));
	if (j == 0)return memo[i][j] = (bool)(solve(i - 1, j) + (s[i] == t[j]));
	if (s[i] == t[j]) return solve(i - 1, j - 1) + 1;
	return memo[i][j] = max(solve(i - 1, j), solve(i, j - 1));
}

int main() {
	int n;
	cin >> n;
	while (n--) {
		cin >> s >> t;
		memo = vector<vector<int>>(2000, vector<int>(2000,-1));
		cout << solve(s.size() - 1, t.size() - 1) << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <stack>
#include <string>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <map>
#define INFTY (1<<30)
#define MAX 100000000

using namespace std;

int lcs(string x, string y) {
	int m = x.size(), n = y.size();
	int c[1000+1][1000+1];
	x = ' ' + x; y = ' ' + y;
	for( int i = 0; i <= m; i++) c[i][0] = 0;
	for( int j = 0; j <= n; j++) c[0][j] = 0;
	for( int i = 1; i <= m; i++) {
		for( int j = 0; j < n; j++) {
			if(x[i] == y[j]) c[i][j] = c[i-1][j-1];
			else if(c[i-1][j] >= c[i][j-1]) c[i][j] = c[i-1][j];
			else c[i][j] = c[i][j-1];
		}
	}
	return c[m][n];
}

int main(){
	int q;
	for( int i = 0; i < q; i++) {
		string s, t;
		cin >> s >> t;
		cout << lcs(s, t) <<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 1010;
char x[N],y[N];
int c[N][N];
int lcs(char x[],char y[]){

    int lenx = strlen(x+1);
    int leny = strlen(y+1);

    for (int i=0; i<=lenx; i++) {
        c[i][0] = 0;
    }
    for(int j=1;j<=leny;j++){
        c[0][j] = 0;
        
    }

    for(int i=1;i<lenx;i++){
        for(int j=1;j<leny;j++){
            if(x[i]==y[j]){
                c[i][j] = c[i-1][j-1] + 1;
            }
            else c[i][j] = max(c[i-1][j], c[i][j-1]);
        }
    }
    return c[lenx][leny];
}
int main(){
    int q;
    scanf("%d",&q);
    for (int i=0; i<q; i++) {
        gets(x+1);
        gets(y+1);
        cout<<lcs(x, y)<<endl;
    }
    return 0;
}

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

int main(){
  int dp[1000][1000]={0};
  int n,m=0;
  int ans[150];
  std::string str1,str2;
  std::cin>>n;
  for(int k=0;k<n;k++){
    std::cin>>str1>>str2;
    for(int i=1;i<=str1.size();i++){
      for(int j=1;j<=str2.size();j++){
	if(str1[i-1]==str2[j-1]){
	  dp[i][j]=dp[i-1][j-1]+1;
	}else{
	  dp[i][j]=std::max(dp[i-1][j],dp[i][j-1]);
	}
      }
    }
    ans[m]=dp[str1.size()][str2.size()];
    m++;
  }
  for(int i=0;i<m;i++){
    std::cout<<ans[i]<<"\n";
  }
  return 0;
}

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

class TLcsSolver
{
    int FTable[1001][1001];
    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);
            }
        }
    }
    void Print()const
    {
        for(std::size_t i = 0; i < Fn1; ++i){
            for(std::size_t j = 0; j < Fn2; ++j){
                printf("%d\t", FTable[i][j]);
            }
            printf("\n");
        }
    } 
public:
    TLcsSolver(const char* a, const char* b)
     : FArray1(a), FArray2(b)
     , Fn1(std::strlen(a)), Fn2(std::strlen(b))
    {
        Solve();
        // Print();
    }
    int Answer()const
    {
        return FTable[Fn1-1][Fn2-1];
    }
};

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