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++: #include <iostream>
#include <string>

using namespace std;

int solve(string s1, string s2){
    int L[s1.size()+1][s2.size()+1];
    for(int i=0;i<=s1.size();i++){
        for(int j=0;j<=s2.size();j++){
            if(i<=0 || j<=0){
                L[i][j]=0;
            }
            else if(s1[i-1] == s2[j-1]){
                L[i][j] = 1 + L[i-1][j-1];
            }
            else{
                L[i][j] = max(L[i][j-1],L[i-1][j]);
            }
        }
    }
    return L[s1.size()][s2.size()];
}

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

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

void fnInput(vector<string>& rvsStrX, vector<string>& rvsStrY)
{
  int nMaxSiz;
  cin >> nMaxSiz;
  rvsStrX.resize(nMaxSiz);
  rvsStrY.resize(nMaxSiz);

  for (int i = 0; i < nMaxSiz; ++i)
  {
    cin >> rvsStrX[i] >> rvsStrY[i];
    rvsStrX[i] = ' ' + rvsStrX[i];
    rvsStrY[i] = ' ' + rvsStrY[i];
  }
}

int fnLngCmnSubseq(const vector<string>& cnrvsStrX, const vector<string>& cnrvsStrY)
{
  vector<vector<int>> vvnLcsDP;
  for (int i = 0; i < cnrvsStrX.size(); ++i)
  {
    int nMAXNX = cnrvsStrX[i].size();
    int nMAXNY = cnrvsStrY[i].size();
    vvnLcsDP.resize(nMAXNX, vector<int>(nMAXNY, 0));

    for (int nx = 1; nx < nMAXNX; ++nx)
      for (int ny = 1; ny < nMAXNY; ++ny)
        if (cnrvsStrX[i][nx] == cnrvsStrY[i][ny])
          vvnLcsDP[nx][ny] = vvnLcsDP[nx - 1][ny - 1] + 1;
        else
          vvnLcsDP[nx][ny] = max(vvnLcsDP[nx - 1][ny], vvnLcsDP[nx][ny - 1]);

    cout << vvnLcsDP[nMAXNX - 1][nMAXNY - 1] << endl;
  }
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<string> vsStrX, vsStrY;

  fnInput(vsStrX, vsStrY);
  fnLngCmnSubseq(vsStrX, vsStrY);
  return 0;
}

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

int lcs(string X, string Y) {
  int max = 0;
  int n = X.size();
  int m = Y.size();
  int dp[1000+1][1000+1];

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

  for(int i = 1;i <= n; i++)
    dp[i][0] = 0;

  for(int i = 1; i <= m; i++)
    dp[0][i] = 0;

  for(int i = 1; i <= n; i++) {
    for(int j = 1; j <= m; j++) {
      if(X[i] == Y[j])
	dp[i][j] = dp[i-1][j-1]+1;
      else if(dp[i-1][j] > dp[i][j-1])
	dp[i][j] = dp[i-1][j];
      else
	dp[i][j] = dp[i][j-1];

      if(max < dp[i][j])
	max = dp[i][j];
    }
  }

  return max;
}

int main() {
  int N;
  cin >> N;

  string X, Y;
  for (int i = 0; i < N; i++) {
    cin >> X >> Y;

    cout << lcs(X, Y) << endl;
  }
  return 0;
}

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

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    for(int cnt=0;cnt<n;++cnt){
        int lngst_cmn[1001][1001]={0};
        string x,y;
        cin>>x>>y;

        for(size_t i=1;i<=x.size();++i){
            for(size_t j=1;j<=y.size();++j){
                string subx=x.substr(0,i),suby=y.substr(0,j);
                if(subx[i-1]==suby[j-1]){
                    lngst_cmn[i][j]=1+lngst_cmn[i-1][j-1];
                }
                else {
                    lngst_cmn[i][j]=(lngst_cmn[i-1][j]>lngst_cmn[i][j-1] ? lngst_cmn[i-1][j]:lngst_cmn[i][j-1]);
                }
            }
        }

        cout<<lngst_cmn[x.size()][y.size()]<<endl;
    }

}

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

#define FOR(i,a,b) for(int i=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define ZERO(a) memset(a,0,sizeof(a))

int dp[1020][1020];

int lcs(string x, string y) {
    int n=x.size(), m=y.size();
    rep(i,n) rep(j,m) {
        if(x[i] == y[j])
            dp[i+1][j+1] = dp[i][j] + 1;
        else
            dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
    }
    return dp[n][m];
}

int main() {
    int q; cin >> q;
    rep(data,q) {
        ZERO(dp);
        string x, y;
        cin >> x >> y;
        int ans = lcs(x,y);
        cout << ans << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,memo[1111][1111];
  string x,y;
  memset(memo,0,sizeof(memo));
  cin>>n;
  for(int count=0;count<n;count++){
    cin>>x>>y;
    int h=x.size(),w=y.size();
    for(int i=0;i<=w;i++){
      for(int j=0;j<=h;j++){
	if(i==0||j==0) continue;
	if(x[j-1]==y[i-1]){
	  memo[i][j]=max(memo[i-1][j-1]+1,memo[i][j-1]);
	}else if(x[j-1]!=y[i-1]){
	  memo[i][j]=max(memo[i-1][j],memo[i][j-1]);
	}
      }
    }
    cout<<memo[w][h]<<endl;
  }
  return(0);
}

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 = 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() {
  string s1, s2;
  int q;
  cin >> q;

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

  return 0;
}

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


void f () {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> dp(s.size(), vector<int>(t.size()));
  tuple<int, int, int> tup(0, 0, 0);

  rep (i, t.size()) if(s[0] == t[i]) dp[0][i] = 1;
  rep (i, s.size()) if(t[0] == s[i]) dp[i][0] = 1;
  for (int i=1; i<s.size(); ++i) for (int j=1; j<t.size(); ++j) {
    if (s[i] == t[j]) dp[i][j] = 1;
    if (get<0>(tup) < dp[i][j]) tup = tie(dp[i][j], i, j);
  }

  for (int i=1; i<dp[0].size(); ++i) if (dp[0][i-1] == 1) dp[0][i] = 1;
  for (int i=1; i<dp.size(); ++i) if (dp[i-1][0] == 1) dp[i][0] = 1;
  for (int i=1; i<dp.size(); ++i) for (int j=1; j<dp[i].size(); ++j) {
    if (dp[i][j] == 0) dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
    else dp[i][j] = dp[i-1][j-1] + 1;
  }

  cout << dp[s.size() - 1][t.size() - 1] << endl;
}


int main() {
  int q;  cin >> q;
  
  rep (i, q) f();
}