Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
)

func max(x, y int) int {
	if x < y {
		return y
	} else {
		return x
	}
}

/*********************************************************************
長さmの列X_(m)と長さnの列Y_(n)の最長共通部分列LCSは、
	(1)X[m] == Y[n]ならば、X_(m-1)とY_(n-1)のLCSにX[m]を繋げたもの
	(2)X[m] != Y[n]ならば、X_(m-1)とY_(n)のLCSもしくはX_(m)とY_(n-1)のLCS
	のいずれかのうち長い方

c[m+1][n+1]：c[i][j]にX_(i)とY_(j)のLCSの長さを格納する二次元配列
*********************************************************************/

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		s1 := scanner.Text()
		scanner.Scan()
		s2 := scanner.Text()
		s1, s2 = s1 + " ", s2 + " "
		c := make([][]int, len(s1))
		for i := range c {
			c[i] = make([]int, len(s2))
		}

		LCS := 0

		for i := 1; i < len(s1); i++ {
			for j := 1; j < len(s2); j++ {
				if s1[i] == s2[j] {
					c[i][j] = c[i-1][j-1] + 1
				} else {
					c[i][j] = max(c[i-1][j], c[i][j-1])
				}
				LCS = max(LCS, c[i][j])
			}
		}
		fmt.Println(LCS)
	}
}

C++: #define DEBUG 1
#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using tll = tuple<ll, ll, ll>;
#define all(v) (v).begin(), (v).end()
#define for1(i, n) for (ll i = 0; i < (n); i++)
#define for2(i, m, n) for (ll i = (m); i < (n); i++)
#define for3(i, m, n, d) for (ll i = (m); i < (n); i += (d))
#define rfor1(i, n) for (ll i = 0; i > (n); i--)
#define rfor2(i, m, n) for (ll i = (m); i > (n); i--)
#define rfor3(i, m, n, d) for (ll i = (m); i > (n); i += (d))
#define INF (1LL << 60)
#define MOD 1000000007LL  // 10**9 + 7
#if DEBUG
#define dump(v1) dump_1(#v1, v1)
#define dump2(v1, v2) dump_2(#v1, v1, #v2, v2)
#define dump3(v1, v2, v3) dump_3(#v1, v1, #v2, v2, #v3, v3)
#define dump4(v1, v2, v3, v4) dump_4(#v1, v1, #v2, v2, #v3, v3, #v4, v4)
#else
#define dump(v1)
#define dump2(v1, v2)
#define dump3(v1, v2, v3)
#define dump4(v1, v2, v3, v4)
#endif
#if DEBUG
template <typename T>
void dump_1(const string& n1, const T& v1)
{
    cerr << n1 << ": " << v1 << endl;
}
template <typename T1, typename T2>
void dump_2(const string& n1, const T1& v1, const string& n2, const T2& v2)
{
    cerr << n1 << ": " << v1 << "  " << n2 << ": " << v2 << endl;
}
template <typename T1, typename T2, typename T3>
void dump_3(const string& n1, const T1& v1, const string& n2, const T2& v2,
            const string& n3, const T3& v3)
{
    cerr << n1 << ": " << v1 << "  " << n2 << ": " << v2 << "  " << n3 << ": "
         << v3 << endl;
}
template <typename T1, typename T2, typename T3, typename T4>
void dump_4(const string& n1, const T1& v1, const string& n2, const T2& v2,
            const string& n3, const T3& v3, const string& n4, const T4& v4)
{
    cerr << n1 << ": " << v1 << "  " << n2 << ": " << v2 << "  " << n3 << ": "
         << v3 << "  " << n4 << ": " << v4 << endl;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& v)
{
    os << "(" << v.first << ", " << v.second << ")";
    return os;
}
template <typename T1, typename T2, typename T3>
ostream& operator<<(ostream& os, const tuple<T1, T2, T3>& v)
{
    os << "(" << get<0>(v) << ", " << get<1>(v) << ", " << get<2>(v) << ")";
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    for (auto it = v.begin(); it != v.end(); it++) {
        if (it != v.begin()) {
            os << " ";
        }
        os << *it;
    }
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    for (auto it = v.begin(); it != v.end(); it++) {
        if (it != v.begin()) {
            os << " ";
        }
        os << *it;
    }
    return os;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const map<T1, T2>& v)
{
    os << "{";
    for (auto it = v.begin(); it != v.end(); it++) {
        if (it != v.begin()) {
            os << ", ";
        }
        os << it->first << ":" << it->second;
    }
    os << "}";
    return os;
}
#endif  // DEBUG
void Yes(void) { cout << "Yes" << endl; }
void No(void) { cout << "No" << endl; }
void YES(void) { cout << "YES" << endl; }
void NO(void) { cout << "NO" << endl; }
template <typename T>
void chmax(T& a, const T& b)
{
    if (a < b) {
        a = b;
    }
}
template <typename T>
void chmin(T& a, const T& b)
{
    if (a > b) {
        a = b;
    }
}
template <typename T>
void vin(vector<T>& v, ll len)
{
    for1 (i, len) {
        cin >> v[i];
    }
}
template <typename T>
void print(const T& v)
{
    cout << v << endl;
}
//---------------------------------------------------------
ll dp[1001][1001];
void solve()
{
    ll Q;
    cin >> Q;
    vll ans;
    for1 (q, Q) {
        string X, Y;
        cin >> X >> Y;
        ll lenX = X.length();
        ll lenY = Y.length();
        for1 (x, lenX + 1) {
            for1 (y, lenY + 1) {
                dp[x][y] = 0;
                if (x > 0 && y > 0) {
                    if (X[x - 1] == Y[y - 1]) {
                        chmax(dp[x][y], dp[x - 1][y - 1] + 1);
                    }
                }
                if (x > 0) {
                    chmax(dp[x][y], dp[x - 1][y]);
                }
                if (y > 0) {
                    chmax(dp[x][y], dp[x][y - 1]);
                }
            }
        }
        ans.push_back(dp[lenX][lenY]);
    }
    for (ll a : ans) {
        print(a);
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    solve();
}

C++: #include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>

using namespace std;

string s1,s2;
int l[1010][1010];
bool flag[1010][1010];

int L(int i,int j){
	//cout<<i<<" "<<j<<endl;
	if(i<0||j<0){
		return 0;
	}
	else if(s1.at(i)==s2.at(j)){
		if(!flag[i][j]){
			l[i][j]=1+L(i-1,j-1);
			flag[i][j]=true;
		}
		return l[i][j];
	}
	else{
		if(!flag[i][j]){
			int a=L(i,j-1);
			int b=L(i-1,j);
			l[i][j]=a>b?a:b;
			flag[i][j]=true;
		}
		return l[i][j];
	}
}

int main(){
	int q;
	cin>>q;
	for(int i=0; i<q; i++){
		cin>>s1>>s2;
		for(int j=0; j<1010; j++){
			for(int k=0; k<1010; k++){
				flag[j][k]=false;
			}
		}

		cout<<L(s1.size()-1,s2.size()-1)<<endl;
	}
}

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

int q;
int **len;
int common(string a,string b,int m,int n){
    if(m==0||n==0){
        return 0;
    }
    if(a[m-1]==b[n-1]){
        if(m>1&&n>1){
            if(len[m-2][n-2]!=-1)len[m-1][n-1]=len[m-2][n-2]+1;
            else len[m-1][n-1]=common(a.substr(0,m-1),b.substr(0,n-1),m-1,n-1)+1;
        }else{
            len[m-1][n-1]=1;
        }
    }
    else{
        int l1,l2;
        //?±?l1
        if(n>1){
            if(len[m-1][n-2]!=-1)l1=len[m-1][n-2];
            else l1=len[m-1][n-2]=common(a,b.substr(0,n-1),m,n-1);
        }else l1=-1;
        if(m>1){
            if(len[m-2][n-1]!=-1)l2=len[m-2][n-1];
            else l2=len[m-2][n-1]=common(a.substr(0,m-1),b,m-1,n);
        }else l2=-1;

        len[m-1][n-1]=max(l1,l2);
    }
    return len[m-1][n-1];
}

int main()
{
    cin>>q;
    int i;
    for(i=0;i<q;i++){
        string a,b;
        cin>>a>>b;
        int m=a.length();
        int n=b.length();
        len=new int*[m];
        int j;
        for(j=0;j<m;j++){
            len[j]=new int[n];
            memset(len[j],-1,n*sizeof(int));
        }
        cout<<common(a,b,m,n)<<endl;
    }
    return 0;
}

//int main(){
//    int **p=new int*[5];
//    p[1]=new int[5];
//    memset(p[1],-1,5*sizeof(int));
//    cout<<p[1][3]<<" "<<p[0][1];
//}