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;
#define rep(i, n) for (int i = 0; i < n; i++)

int lcs(string x, string y) {
    x = ' ' + x;
    y = ' ' + y;
    int m = x.size();
    int n = y.size();
    vector<vector<int>> c(m, vector<int>(n, 0));
    rep(i, m) {
        rep(j, n) {
            if (i == 0 || j == 0) continue;
            if (x[i] == y[j]) c[i][j] = c[i-1][j-1] + 1;
            else c[i][j] = max(c[i][j-1], c[i-1][j]);
        }
    }
    return c[m-1][n-1];
}

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

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

string X;
string Y;

int calc(int x, int y, vector<vector<int> >& Memo){
    if (X[x] == '\0' || Y[y] == '\0') return 0;
    if (Memo[x][y] != -1) return Memo[x][y];

    int unuse = calc(x + 1, y, Memo);
    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, Memo) + 1;
    }
    return Memo[x][y] = max(unuse, use);
}

int main(){
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> X;
        cin >> Y;
        vector<int> tmp(Y.size(), -1);
        vector<vector<int> > Memo(X.size(), tmp);
        cout << calc(0, 0, Memo) << endl;
    }

    return 0;
}

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

int lcs(string X, string Y) {
    int m = X.size();
    int n = Y.size();
    int c[m + 1][n + 1];
    c[0][0] = 0;
    for (int i = 1; i <= m; i++) c[i][0] = 0;
    for (int j = 1; j <= n; j++) c[0][j] = 0;

    int max_len = 0;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (X[i - 1] == Y[j - 1]) {
                c[i][j] = c[i - 1][j - 1] + 1;
            } else {
                c[i][j] = max(c[i - 1][j], c[i][j - 1]);
            }
            max_len = max(max_len, c[i][j]);
        }
    }

    return max_len;
}

int main() {
    int n;
    cin >> n;
    string s1, s2;
    int max_len[n];
    for (int i = 0; i < n; i++) {
        cin >> s1 >> s2;
        max_len[i] = lcs(s1, s2);
    }

    for (int i = 0; i < n; i++) {
        cout << max_len[i] << endl;
    }

    return 0;
}

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


int LCS(const string& a, const string& b) {
    int ans = 0;
    const int len_a = a.length(), len_b = b.length();

    int dp[len_a + 1][len_b + 1] = {};
    for(int i=1; i<=len_a; i++) {
	for(int j=1; j<=len_b; j++) {
	    dp[i][j] = max(dp[i][j], dp[i-1][j] );
	    dp[i][j] = max(dp[i][j], dp[i][j-1] );
	    dp[i][j] = max(dp[i][j], dp[i-1][j-1] + (a[i-1] == b[j-1] ? 1 : 0) );
	}
    }
    ans = dp[len_a][len_b];
    return ans;
}

int main() {

    int N;
    cin >> N;

    for(int i=0; i<N; i++) {
	string a,b;
	cin >> a >> b;
	cout << LCS(a,b) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#define N 1000
using namespace std;
 
int lcs(string S1,string S2){
    int c[N+1][N+1];
    int S1_size = S1.size();
    int S2_size = S2.size();
     
    for(int i=1;i<S1_size;i++) c[i][0] = 0;
    for(int i=1;i<S2_size;i++) c[0][i] = 0;
     
    S1 = ' '+S1;
    S2 = ' '+S2;
     
    int max_lcs=0;
    for(int j=1;j<=S1_size;j++){
        for(int k=1;k<=S2_size;k++){
            if(S1[j]==S2[k]){
                c[j][k] = c[j-1][k-1] + 1;
            }else{
                c[j][k] = max(c[j-1][k],c[j][k-1]);
            }
            max_lcs = max(max_lcs,c[j][k]);
        }
    }
    return max_lcs;
}
 
int main() {
    // your code goes here
    int q;
    string str1,str2;
    cin>>q;
    for(int i=0;i<q;i++){
        cin>>str1>>str2;
        cout<<lcs(str1,str2)<<endl;
    }
    return 0;
}

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

int Max(int &a,int b){return a=max(a,b);}

int LCS(string &a,string &b){
  int n = a.size();
  int m = b.size();
  vector <vector<int> > dp(n+1,vector<int>(m+1));

  for(int i=0;i<=n;i++)
    for(int j=0;j<=m;j++){
      if(i<n && j<m && a[i] == b[j]) Max(dp[i+1][j+1],dp[i][j]+1);
      if(i<n)Max(dp[i+1][j],dp[i][j]);
      if(j<m)Max(dp[i][j+1],dp[i][j]);

    }
  return dp[n][m];
}

int main(){
  int q;
  cin>>q;
  while(q--){
    string a,b;
    cin>>a>>b;
    cout<<LCS(a,b)<<endl;
  }
  return 0;
}

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

// ???????????§???????????§?????§??????
int DP[1024][1024] = {};

int main(){
    int N;
    cin >> N;
    for(int c=0; c<N; ++c){
        string X,Y;
        cin >> X >> Y;
        int xlen = (int)X.size();
        int ylen = (int)Y.size();

        for(int i=0;i<=xlen;++i){
          DP[i][0] = 0;
        }
        for(int i=0;i<=ylen;++i){
          DP[0][i] = 0;
        }

        for(int i=1;i<=xlen;++i){
            for(int j=1;j<=ylen;++j){
                if(X[i-1] == Y[j-1]){ // X???i???????????¨Y???j?????????????????????????????????index?????????????????¨?????¨??????
                    DP[i][j] = DP[i-1][j-1] + 1;
                } else {
                    DP[i][j] = max(DP[i-1][j], DP[i][j-1]);
                }
            }
        }
        cout << DP[xlen][ylen] << endl;
    }
}