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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define SENTINEL (-1)
#define M (1001)

// ia, ib以降のindexに対するLCSを返す
// (ia, ib)に対するLCSがわかっているとき, (ia-1, ib), (ia, ib-1)
// に対するLCSは, a[ia-1] == a[ib],...であれば+1される....
// と考えたくなるが, iaとibでカウントされている場合, ダブルカウントになってしまうから,
// 必ずしもそうとは言えない.
// X: abcbdab
// Y: bdcaba
// 1-originで考えると,
// LCS(7,6,X,Y) = 0
// LCS(6,6,X,Y) = 1
// LCS(5,5,X,Y) = 1
// LCS(4,5,X,Y) = 2
// ... ????
// 部分問題に分解したい
// X: abc
// Y: ac
// -> 2
// X[3],Y[2] -> 1
// X[2],Y[2] -> 
// lcs(i,j)
// : if a[i] == b[j] => lcs(i,j) = lcs(i-1,j-1) + 1
// : else lcs(i,j) = max ( lcs(i-1,j), lcs(i, j-1) )
// lcs(0,0) := 0
// lcs(

int lcs(int i, int j, string &a, string &b, int memo[][M]) {
	if (i==0 || j==0) {
		memo[i][j] = 0;
		return 0;
	}
	if (memo[i][j] != SENTINEL) {
		return memo[i][j];
	}
	// i, j as 1-origin index
	if (a[i-1] == b[j-1]) {
		memo[i][j] = lcs(i-1, j-1, a, b, memo) + 1;
	} else {
		memo[i][j] = max(lcs(i-1,j,a,b,memo), lcs(i,j-1,a,b,memo));
	}
	return memo[i][j];
}

// この多次元配列の書き方は汎用性がなくて嫌
void fill(int a[][M], int size1, int size2, int v) {
	for (int i=0;i<size1;++i) {
		for (int j=0;j<size2;++j) {
			a[i][j] = v;
		}
	}
}

int main () {
	int q;
	cin >> q;
	int LCS[1001][M];
	fill(LCS, 1001, 1001, SENTINEL);// -1 is sentinel
	vector<int> res;
	for (int i=0; i<q; ++i) {
		string a, b;
		cin >> a >> b;
		res.push_back(lcs(a.size(),b.size(),a,b,LCS));
		fill(LCS, M, M, SENTINEL);
	}
	for (auto i : res) {
		cout << i << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
static const int N=1000;

int lcs(string X,string Z)
{
  int c[N+1][N+1];
  int m=X.size();
  int n=Z.size();
  int maxl=0;
  X=' '+X;
  Y=' '+Z;
  for(int i=1;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 i=1;j<=n;j++)
	{
	  if(X[i]==Z[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()
{
  stirng 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 <utility>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <math.h>
#include <string.h>
#include <iomanip>
#include <numeric>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <functional>
#include <limits>

using namespace std;

using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define Rep(i, n) for (long long i = 0; i< (long long)(n); i++)
#define RRep(i,n) for (long long i = ((long long)(n)-1); i>=0; i--)
#define all(v) (v).begin(),(v).end()

struct Edge{
    int to;
    int weight;
    Edge(int t, int w) : to(t), weight(w){ }
};

struct edge{
    int from;
    int to;
    int weight;
    edge(int f,int t,int w) : from(f), to(t), weight(w){ }
};

using Graph = vector<vector<Edge>>;
using graph = vector<vector<int>>;
using edges = vector<edge>;

template <class T>
inline bool chmin(T &a, T b){
    if (a > b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
inline bool chmax(T &a, T b){
    if (a < b){
        a = b;
        return true;
    }
    return false;
}

template <class T>
T gcd(T a, T b){
    if( b==0 ) return a;
    return gcd(b, a%b);
}

template <class T>
T lcm(T a, T b){
    return (a*b)/gcd(a,b);
}

int INF = 1e9+7;

vector<int> dx = {-1,0,1,0};
vector<int> dy = {0,-1,0,1};

void solve(){
    string s,t;
    cin >> s >> t;
    int n = s.size();
    int m = t.size();
    vector<vector<int>> dp(n+1,vector<int>(m+1,0));
    rep(i,n) rep(j,m){
        if(s[i]==t[j]){
            dp[i+1][j+1] = max(dp[i][j]+1,max(dp[i+1][j],dp[i][j+1]));
        }
        else{
            dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
        }
    }
    cout << dp[n][m] << endl;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    rep(_,t) solve();
}

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

string lcs_f;
string lcs_s;
int maxnum = 0;

void lcs() {
    maxnum = 0;
	int flen = lcs_f.size() + 1;
	int slen = lcs_s.size() + 1;
	int a[1001][1001] ={};
	for (int i = 0; i < flen; i++) {
		for (int j = 0; j < slen; j++) {
			if (i == 0 || j == 0) {
				a[i][j] = 0;
			}
			else if (lcs_f[i-1] == lcs_s[j-1]) {
				a[i][j] = a[i - 1][j - 1] + 1;
				if (a[i][j] > maxnum) {
				    maxnum = a[i][j];
				}
			}
			else {
				if (a[i][j - 1] < a[i - 1][j]) {
					a[i][j] = a[i - 1][j];
				}
				else {
					a[i][j] = a[i][j-1];
				}
				 
			}
		}
	}
	
	
}

int main() {
	int n;
	
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> lcs_f >> lcs_s;
		lcs();
		cout << maxnum << endl;
	}
}

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

using namespace std;

int main() {
		
	long n;
	cin >> n ;

	for (long i = 0; i < n; i++) {
		string x, y;
		cin >> x >> y;
		long xl, yl;
		xl = x.size();
		yl = y.size();
		vector<vector<long>> dp(xl+1, vector<long> (yl+1,0));
		for (long j = 0; j < xl; j++) {
			for (long k = 0; k < yl; k++) {

				if (x[j] == y[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[xl][yl] << endl;
	}

	return 0;

}