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 <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 1005;
char inputa[MAXN], inputb[MAXN];
int f[MAXN][MAXN];
int main(void)
{
    int t, lengtha, lengthb;
    scanf("%d", &t);

    while(t--)
    {
        scanf("%s%s", inputa, inputb);
        memset(f, 0, sizeof(f));

        lengtha = strlen(inputa);
        lengthb = strlen(inputb);

        for(int i = 0; i <= lengtha; i++)
        {
            for(int j = 0; j <= lengthb; j++)
            {
                if(i == 0 || j == 0)
                    f[i][j] = 0;
                else if(inputa[i - 1] == inputb[j - 1])
                    f[i][j] = f[i - 1][j - 1] + 1;
                else
                    f[i][j] = max(f[i - 1][j], f[i][j - 1]);
            }
        }

        printf("%d\n", f[lengtha][lengthb]);
    }

    return 0;
}

C++: // C++ 14
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>

#define ll long long
#define loop(x, start, end) for(int x = start; x < end; x++)
#define loopdown(x, start, end) for(int x = start; x > end; x--)

#define MAX 1000

using namespace std;



int lcs(string &s1, string &s2) {
    int dp[MAX+1][MAX+1];
    int l1 = s1.length();
    int l2 = s2.length();
    int maxl = 0;
    
    s1 = ' ' + s1;
    s2 = ' ' + s2;
    
    loop(i, 0, l1+1) {
        dp[i][0] = 0;
    }
    loop(j, 0, l2+1) {
        dp[0][j] = 0;
    }
    
    loop (i, 1, l1+1) {
        loop (j, 1, l2+1) {
            if (s1[i] == s2[j]) dp[i][j] = 1 + dp[i-1][j-1];
            else dp[i][j] = max(
                dp[i-1][j],
                dp[i][j-1]
            );
            
            maxl = max(maxl, dp[i][j]);
        }
        
    }
    
    return maxl;
}

int main(void) {
    int n;
    string s1, s2;
    
    cin >> n;
    loop(i, 0, n) {
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,j) for (int (i)=0;(i)<(int)(j);++(i))
#define put(i) cout << i << endl
using namespace std;
const int MAX = 1000;

int n;
string s1, s2;
int dp[MAX+1][MAX+1];

int rec(int x, int y) {
    if (dp[y][x] >= 0) {
        return dp[y][x];
    }
    int sz1 = s1.size(), sz2 = s2.size();
    int ret;
    if (x == sz1 || y == sz2) {
        ret = 0;
    }
    else if (s1[x] == s2[y]) {
        ret = rec(x+1, y+1)+1;
    }
    else {
        ret = max(rec(x+1, y), rec(x, y+1));
    }
    return dp[y][x] = ret;
}

void solve() {
    while (n--) {
        memset(dp, -1, sizeof(dp));
        cin >> s1 >> s2;
        cout << rec(0, 0) << endl;
    }
}

int main() {
    cin >> n;
    solve();
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <vector>
#include <map>
#include <set>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <climits>
 
using namespace std;
 
int f(map<string,int>& dmap,const string& dstr1,string dstr2);
 
int main()
{
    int N;
    cin>>N;
    for(int i=0;i<N;i++){
        string data[2];
        map<string,int> dmap;
        cin>>data[0]>>data[1];
        if(data[0].length()<data[1].length()){
            cout<<f(dmap,data[1],data[0])<<endl;
        }else{
            cout<<f(dmap,data[0],data[1])<<endl;
        }
    }
}
int f(map<string,int>& dmap,const string& dstr1,string dstr2)
{
    auto dit=dmap.find(dstr2);
    if(dit!=dmap.end()){
        return (*dit).second;
    }
    int j=0;
    for(int i=0;i<dstr1.length()&&j<dstr2.length();i++,j++){
        while(i<dstr1.length()&&dstr1[i]!=dstr2[j]){
            i++;
        }
    }
    if(j==dstr2.length()){
        dmap.insert(pair<string,int>(dstr2,(int)dstr2.length()));
        return dstr2.length();
    }
    if(dstr2.length()==1){
        dmap.insert(pair<string,int>(dstr2,0));
        return 0;
    }
    f(dmap,dstr1,dstr2.substr(1,dstr2.length()-1));
    f(dmap,dstr1,dstr2.substr(0,dstr2.length()-1));
    j=0;
    for(int i=2;i<dstr2.length();i++){
        int c=f(dmap,dstr1,dstr2.substr(0,i-1)+dstr2.substr(i,dstr2.length()-i));
        if(j<c){
            j=c;
        }
    }
    dmap.insert(pair<string,int>(dstr2,j));
    return j;
}

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

int main(){
    int q;
    cin >> q;
    for(int qnum = 0; qnum < q; qnum++){
        string x, y;
        cin >> x;
        cin >> y;
        vector<vector<int>> memo(x.length()+1, vector<int>(y.length()+1, 0));
        for(int i = 0; i < x.length(); i++){
            for(int j = 0; j < y.length(); j++){
                if(x[i] == y[j]){
                    memo[i+1][j+1] = memo[i][j]+1;
                }else{
                    memo[i+1][j+1] = max(memo[i][j+1], memo[i+1][j]);
                }
            }
        }
        cout << memo[x.length()][y.length()] << endl;
    }
    return 0;
}