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 <algorithm>
#include <set>
#include <vector>
#include <math.h>
using namespace std;
typedef long long ll;

int linersearch(int a[], int n, int x);
void insetrtionsort(int a[], int n);
void printarray(int n, int a[]);
void babblesort(int n, int a[]);
void selectionsort(int n, int a[]);
void countingsort(int n, int a[]);
void minimumcostsort(int n, int a[]);

int linersearch(int a[], int n, int x)
{
    int ret = 0;
    for (int i = 1; i < n; i++)
    {
        if (a[i] == x)
        {
            ret = 1;
            break;
        }
    }
    return ret;
}

void insetrtionsort(int a[], int n)
{
    int j, v;
    for (int i = 1; i < n; i++)
    {
        v = a[i];
        j = i - 1;
        while (j >= 0 && a[j] > v)
        {
            a[j + 1] = a[j];
            j--;
        }
        a[j + 1] = v;
        for (int i = 0; i < n; i++)
        {
            cout << a[i] << " ";
        }
        cout << endl;
    }
}

void babblesort(int n, int a[])
{
    int flag = 1;
    int counter = 0;
    int tmp;
    while (flag)
    {
        flag = 0;
        for (int j = n - 1; j > 0; j--)
        {
            if (a[j] < a[j - 1])
            {
                swap(a[j], a[j - 1]);
                counter++;
                flag = 1;
            }
        }
    }
    printarray(n, a);
    cout << counter << endl;
}

void selectionsort(int n, int a[])
{
    int minj;
    int counter = 0;
    for (int i = 0; i < n; i++)
    {
        minj = i;
        for (int j = i; j < n; j++)
        {
            if (a[j] < a[minj])
                minj = j;
        }
        if (i != minj)
        {
            swap(a[minj], a[i]);
            counter++;
        }
    }
    printarray(n, a);
    cout << counter << endl;
}

void countingsort(int n, int a[])
{
    int b[n] = {};
    int k = 0;
    for (int i = 0; i < n; i++)
        k = max(a[i], k);
    int c[k + 1] = {};
    for (int i = 0; i < n; i++)
        c[a[i]]++;
    for (int i = 1; i < k + 1; i++)
        c[i] += c[i - 1];
    for (int i = n - 1; i > -1; i--)
    {

        b[c[a[i]] - 1] = a[i];
        c[a[i]]--;
    }
    printarray(n, b);
}

void minimumcostsort(int n, int a[])
{
    int maxa, maxindex, cost;
    cost = 0;
    for (int i = n - 1; i > 0; i--)
    {
        maxa = a[0];
        maxindex = 0;
        for (int j = 1; j <= i; j++)
        {
            if (a[j] > maxa)
            {
                maxa = a[j];
                maxindex = j;
            }
        }
        if (maxindex != i)
        {
            cost += maxa + a[i];
            swap(a[maxindex], a[i]);
        }
    }
    cout << cost << endl;
}

void printarray(int n, int a[])
{
    for (int i = 0; i < n; i++)
    {
        cout << a[i];
        if (i != n - 1)
            cout << " ";
    }
    cout << endl;
}

int gcd(int a, int b)
{
    if (a < b)
    {
        int t = a;
        a = b;
        b = t;
    }
    cout << a << " " << b << endl;
    if (b == 0)
        return a;
    else
        return gcd(b, b % a);
}

ll fib(int x){
    if (x == 0 || x == 1)
        return 1;
    else;
        return fib(x - 1) + fib(x - 2);
}

int lcs(string s, string t)
{
    int la = s.size();
    int lb = t.size();
    int lcs[la + 1][lb + 1] = {};
    for (int i = 0; i < la; i++)
    {
        for (int j = 0; j < lb; j++)
        {
            if (s[i] == t[j])
            {
                lcs[i + 1][j + 1] = lcs[i][j] + 1;
            }
            else
            {
                lcs[i + 1][j + 1] = max(lcs[i][j + 1], lcs[i + 1][j]);
            }
        }
    }

    return lcs[la][lb];
}

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

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

class TLcsSolver
{
    typedef std::vector<std::vector<int> > TTable;
    TTable FTable;
    std::string FArray1;
    std::string FArray2;

    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 < FTable.size(); ++i){
            for(std::size_t j = 1; j < FTable[i].size(); ++j){
                FTable[i][j] = Count(i, j);
            }
        }
    }
public:
    TLcsSolver(const std::string& a, const std::string& b)
     : FArray1(a), FArray2(b)
     , FTable(a.size()+1, std::vector<int>(b.size()+1, 0))
    {
        Solve();
        // Print();
    }
    int Answer()const
    {
        return FTable[FArray1.size()][FArray2.size()];
    }
    void Print()const
    {
        for(std::size_t i = 0; i < FTable.size(); ++i){
            for(std::size_t j = 0; j < FTable[i].size(); ++j){
                std::cout << FTable[i][j] << '\t';
            }
            std::cout << '\n';
        }
    } 
};

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