Go: package main

import (
	. "fmt"
    "math"
)

// func init() { Println("----------------------------------------") }

func main() {
    const L_MAX = 1003

	var q int
	Scanf("%d", &q)
	X := make([]string, 0, 150)
	Y := make([]string, 0, 150)
	for i := 0; i < 2 * q; i++ {
	    var tmp string
	    Scanf("%s", &tmp)

	    if i % 2 == 0 {
	        X = append(X, tmp)
	    } else {
	        Y = append(Y, tmp)
	    }
	}

    for i := 0; i < q; i++ {
        dp := make([][]int, L_MAX)
        for k, _ := range dp {
            dp[k] = make([]int, L_MAX)
        }

        x, y := X[i], Y[i]

        // init
        isXFound := false
        for c := 0; c < len(x); c++ {
            if isXFound || y[0] == x[c] {
                dp[0][c] = 1
                isXFound = true
            }
        }
        isYFound := false
        for r := 0; r < len(y); r++ {
            if isYFound || x[0] == y[r] {
                dp[r][0] = 1
                isYFound = true
            }
        }

        for r := 1; r < len(y); r++ {
            for c := 1; c < len(x); c++ {
                // dp[r][c]
                if x[c] == y[r] {
                    dp[r][c] = int(math.Max(float64(dp[r - 1][c - 1] + 1), float64(dp[r][c])))
                }

                dp[r][c] = int(math.Max(float64(dp[r][c]), math.Max(float64(dp[r - 1][c]), float64(dp[r][c - 1]))))
            }
        }
        Println(dp[len(y) - 1][len(x) - 1])

        // LCSも求めておく
        // ans := ""
        // r, c := len(y) - 1, len(x) - 1
        // for r >= 0 && c >= 0 {
        //     if y[r] == x[c] {
        //         ans = string(x[c]) + ans
        //         r--
        //         c--
        //     } else {
        //         if dp[r - 1][c] > dp[r][c - 1] {
        //             r--
        //         } else {
        //             c--
        //         }
        //     }
        // }
        // Println(ans)
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

constexpr int max_len = 1024;

int lcs(const string &s,const string &t){
    const int m=s.size(),n=t.size();
    vector<vector<int>> dp(m+1,vector<int>(n+1,0));

    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            if(s[i]==t[j])  dp[i+1][j+1] = dp[i][j] + 1;
            else            dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1]);
        }
    }

    return dp[m][n];
}

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

C++: #include <iostream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
using namespace std;
int main() {
    int query;
    cin >> query;
    for(int q=0;q<query;q++){
        string x,y;
        cin >> x >> y;
        vector<vector<int> > dp(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]){
                    dp[i+1][j+1] = dp[i][j] +1;
                }else{
                    dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1]);
                }
            }
        }
        cout << dp[x.length()][y.length()] << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v
#define NUM(x,v) (POSU(x,v)-POSL(x,v))
const ll INF=1e9+7;
const ll N = 1002;
const ll M = 21;
ll q,dp[N][N];
string x,y;
vector <ll> ans;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>q;
	FOR(k,0,q){
		cin>>x>>y;
		FOR(i,0,x.size()+1){
			FOR(j,0,y.size()+1){
				dp[i][j]=0;
			}
		}
		FOR(i,0,x.size()+1){
			FOR(j,0,y.size()+1){
				if(i==0||j==0)dp[i][j]=0;
				else{
					if(x[i-1]==y[j-1])dp[i][j] = max({dp[i-1][j-1]+1,dp[i][j-1],dp[i-1][j]});
					else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
				} 
			}
		}
		ans.push_back(dp[x.size()][y.size()]);
	}
	FOR(i,0,ans.size()){
		cout<<ans[i]<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;

int lcs(string x, string y) {
    x = ' ' + x;
    y = ' ' + y;
    int m = x.length();
    int n = y.length();
    int memo[m][n];
    rep(i, m) memo[i][0] = 0;
    rep(i, n) memo[0][i] = 0;
    for(int i = 1; i < m; i++) {
        for(int j = 1; j < n; j++) {
            if(x[i] == y[j]) {
                memo[i][j] = memo[i - 1][j - 1] + 1;
            } else {
                memo[i][j] = max(memo[i - 1][j], memo[i][j - 1]);
            }
        }
    }

    return memo[m-1][n-1];
}

int main() {
    // 制約見て！！！デカかったらlong longにすること！
    int q;
    string x, y;
    cin >> q;
    rep(i, q) {
        cin >> x >> y;
        cout << lcs(x, y) << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;

#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define N 1050

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

    X = ' ' + X;
    Y = ' ' + Y;

    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]);
            }
            maxV = max(maxV, c[i][j]);
        }
    }
    return maxV;
}

int main() {
    // ios::sync_with_stdio(false);
    // cin.tie(0);
    // cout << fixed << setprecision(5);

    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 <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <bitset>
#include <iterator>
#include <iomanip>
#include <sstream>
#define INF 2000000000
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
#define sym cout<<"---------"<<endl;
#define ll long long
#define PL pair<long long, long long>
#define P pair<int,int>
#define mk make_pair
#define en endl
#define WHITE 0
#define BLACK 2
#define GRAY 1
#define RE return 0
#define int ll
//
//vector<int> G[20];
//
//// 巡回セールスマン問題
//
//signed main(){
//  int v,e;
//  cin>>v>>e;
//
//  for(int i=0; i<e; i++){
//    int a,b;
//    cin>>a>>b;
//    G[a].push_back(b);
//  }
//}























////  最長共通部分列

int dp[1005][1005];

signed main(){
  int n;
  cin>>n;
  for(int i=0; i<n; i++){
    string s1,s2;
    cin>>s1>>s2;
    s1=" "+s1;
    s2=" "+s2;
    int maxl=0;
    for(int i=0; i<=s1.size()-1; i++) for(int j=0; j<=s2.size(); j++) dp[i][j]=0;
    for(int i=1; i<=s1.size()-1; i++){
      for(int j=1; j<=s2.size(); j++){
        if(s1[i]==s2[j]) dp[i][j]=dp[i-1][j-1]+1;
        else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
        maxl=max(maxl,dp[i][j]);
      }
    }
    cout<<maxl<<en;
  }
}