Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	for i := 0; i < n; i++ {
		x := getNextString(scanner)
		y := getNextString(scanner)

		fmt.Fprintln(writer, lcs(x, y))
	}

	writer.Flush()
}

func lcs(x, y string) int {
	lx := len(x)
	ly := len(y)
	dp := make([][]int, lx+1)
	for i := 0; i <= lx; i++ {
		dp[i] = make([]int, ly+1)
	}
	for i := 0; i < lx; i++ {
		for ii := 0; ii < ly; ii++ {
			if x[i] == y[ii] {
				dp[i+1][ii+1] = dp[i][ii] + 1
				continue
			}
			dp[i+1][ii+1] = dp[i][ii+1]
			if dp[i+1][ii] > dp[i+1][ii+1] {
				dp[i+1][ii+1] = dp[i+1][ii]
			}
		}
	}
	return dp[lx][ly]
}

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


constexpr int MAXLENGTH = 1000;
int C[MAXLENGTH + 1][MAXLENGTH + 1];
int lcs(std::string lhs, std::string rhs) {
    int lnum = lhs.length();
    int rnum = rhs.length();
    for (int i = 0; i <= lnum; i++) {
        C[i][0] = 0;
    }
    for (int i = 0; i <= rnum; i++) {
        C[0][i] = 0;
    }
    for (int il = 1; il <= lnum; il++) {
        for (int ir = 1; ir <= rnum; ir++) {
            if (lhs[il-1] == rhs[ir-1]) {
                C[il][ir] = C[il - 1][ir - 1] + 1;
            }else if(C[il - 1][ir] >= C[il][ir - 1]) {
                C[il][ir] = C[il-1][ir];
            }else{
                C[il][ir] = C[il][ir - 1];
            }
        }
    }
    return C[lnum][rnum];
}

int main() {
    int n;
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::string l, r;
        std::cin >> l;
        std::cin >> r;
        std::cout << lcs(l, r) << std::endl;
    }
}

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

static const int N = 1000;

int lcs(string X, string Y){
    int c[N+1][N+1];
    int m = X.size();
    int n = Y.size();
    int maxl =0;

    X = ' ' + X; // X[0]に空白を挿入
    Y = ' ' + Y; // Y[0]に空白を挿入
    for(int i=0; i<=m; i++) c[i][0] = 0;
    for(int j=1; j<=n; j++) c[0][j] = 0;

    for(int i=1; i<=m; i++){// 全通りの組み合わせを試している？
        for(int j=1; j<=n; 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]);
            }
            maxl = max(maxl, c[i][j]);
        }
    }
    return maxl;
}

int main(int argc, char const *argv[])
{
    string s1, s2;
    int n;
    cin >> n;
    for(int i=0; i<n; i++){
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <sstream>
#include <utility>
#define ll long long int
#define rep(i,x,y) for(int i=x;i<y;i++)
#define rel(i,x,y) for(int i=x-1;i>=y;i--)
#define all(x) x.begin(),x.end()
#define mod 1000000007
using namespace std;

int main(){
	int q;
	cin >> q;
	string X[q],Y[q];
	rep(i,0,q){
		cin >> X[i];
		cin >> Y[i];
	}

	rep(i,0,q){
		int m = X[i].length();
		int n = Y[i].length();
		int dp[m+1][n+1] = {};
		int maxl=0;
		rep(j,0,m){
			rep(k,0,n){
				if(X[i][j] == Y[i][k]) dp[j+1][k+1] = dp[j][k]+1;
				else dp[j+1][k+1] = max(dp[j][k+1],dp[j+1][k]);
			}
		}
		cout << dp[m][n] << endl;
	}
	return 0;
}

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

int z[1001][1001];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    string x,y;
    int n;
    cin>>n;
    while(n--) {
        cin>>x>>y;
        int lx=x.size(), ly=y.size();
        for(int i=0; i<=lx; i++) z[i][0]=0;
        for(int i=0; i<=ly; i++) z[0][i]=0;
        for(int i=1; i<=lx; i++) {
            for(int j=1; j<=ly; j++) {
                if(x[i-1]==y[j-1]) z[i][j]=z[i-1][j-1]+1;
                else z[i][j]=max(z[i][j-1],z[i-1][j]);
            }
        }
        cout<<z[lx][ly]<<'\n';
    }
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
using ll = long long;

void init();
void solve();

#define rep(i, a) for (int i = 0; i < (int) (a); i++)
#define reps(i, a, b) for (int i = (int) (a); i < (int) (b); i++)
#define rrep(i, a) for (int i = (int) a-1; i >= 0; i--)
#define rreps(i, a, b) for (int i = (int) (a)-1; i >= (int) (b); i--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PRINT(f) if((f)){cout << (TRUE__) << endl;}else{cout << FALSE__ << endl;}
#define TRUE__  "Yes"
#define FALSE__ "No"
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v) sort(all(v));(v).erase(unique(all(v)), v.end())
#define CIN(type, x) type x;cin >> x

#ifdef LOCAL
#define lcout(a) cout << a
#define lcoutln(a) cout << a << endl
#define lcerr(a) cerr << a
#define lcerrln(a) cerr << a << endl
#else
#define lcout(a) 
#define lcoutln(a) 
#define lcerr(a) 
#define lcerrln(a) 
#endif
#define MOD 1000000007

signed main()
{
	init();
	solve();
}

int q, xl, yl;
string x, y;

void init()
{
	cin >> q;
}

int dp[1010][1010];

void reset()
{
	rep(i, xl+2) rep(j, yl+2) dp[i][j] = 0;
}


void solve()
{
	rep(cnt, q) {
		cin >> x >> y;
		xl = x.length();
		yl = y.length();
		reps(i, 1, xl+1) reps(j, 1, yl+1) {
			if (x[i-1] == y[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
			else dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
		}
		cout << dp[xl][yl] << endl;
		reset();
	}
}

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

static const int N = 1000;

int lcs(string X,string Y){
    int c[N+1][N+1];
    int m = X.size();
    int n = Y.size();
    int maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;
    for(int i=1; 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=1; j<=n; 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]);
            }
            maxl = max(maxl, c[i][j]);
        }
    }
    
    return maxl;
}

int main(){
    string s1,s2;
    int n; cin >> n;
    
    for(int i=0; i<n; i++){
        cin >> s1 >> s2;
        cout << lcs(s1,s2) << endl;
    }
    
    return 0;
}

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

string X;
string Y;

int calc(int x, int y){
    if (X[x] == '\0') return 0;
    if (y == string::npos) return 0;

    int unuse = calc(x + 1, y);
    int use = 0;
    int y_target = Y.find(X[x], y);
    if (y_target == string::npos) {
        use = 0;
    } else {
        use = calc(x + 1, y_target + 1) + 1;
    }
    return max(unuse, use);
}

int main(){
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> X;
        cin >> Y;
        cout << calc(0, 0) << endl;
    }

    return 0;
}

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

int N,len1,len2;
string seq1,seq2;
int dp[1000+10][1000+10];

int main() {
	cin >> N;
	for (int M=0; M<N; M++){
		cin >> seq1 >> seq2;
		len1 = seq1.length();
		len2 = seq2.length();
		for (int i=1; i<=len1; i++){
			for (int j=1; j<=len2; j++){
				if (seq1[i-1] == seq2[j-1]){
					dp[i][j] = dp[i-1][j-1] + 1;
				}else if (dp[i][j-1] > dp[i-1][j]){
					dp[i][j] = dp[i][j-1];
				}else{
					dp[i][j] = dp[i-1][j];
				}
			}
		}
		cout << dp[len1][len2] << endl;
	}
	
	return 0;
}