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<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(n,a) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265


void f(){
  string x,y;
  cin>>x>>y;
  int dp[1100][1100]={};
  
  rep(i,x.length()+1){
    rep(j,y.length()+1){
      if(i==0||j==0) continue;
      dp[i][j]=max(dp[i-1][j-1],dp[i][j-1]);
      if(x[i-1]==y[j-1]){
        FOR(k,j,y.length()){
          dp[i][k]=max(dp[i][k],dp[i][j]+1);
        }
      }
    }
  }

  cout<<dp[(int)x.length()][(int)y.length()]<<endl;
}


int main(){
  int q;
  cin>>q;
  while(q--){
    f();
  }
}

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

static const int N = 1000;

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

    X = ' ' + X;
    Y = ' ' + Y;
    
    int maxl = 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 n;
    string X, Y;
    cin >> n;

    for(int i = 0; i < n; i++) {
        cin >> X >> Y;
        cout << lcs(X, Y) << 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;
    int tmp1 = calc(x + 1, y);
    int tmp2 = 0;
    int y_next = Y.find(X[x], y);
    if (y_next == string::npos) {
        tmp2 = 0;
    } else {
        tmp2 = calc(x + 1, y_next + 1) + 1;
    }
    return max(tmp1, tmp2);
}

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<bits/stdc++.h>
using namespace std;

int mazide(string a,string b){
  int c[1001][1001];
  int m=a.size(),n=b.size();
  int mx=0;
  a=' '+a;
  b=' '+b;
  for(int i=1;i<=m;i++){
    for(int j=1;j<=n;j++){
      if(a[i]==b[j]){
	c[i][j]=c[i-1][j-1]+1;
      }else{
	c[i][j]=max(c[i-1][j],c[i][j-1]);
      }
      mx=max(mx,c[i][j]);
    }
  }
  return mx;
}


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

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

using namespace std;

int main(int argc, char const* argv[])
{
	int q;
	cin >> q;
	for(int i = 0; i < q; i++){
			string a, b;
			cin >> a;
			cin >> b;
			int **dp = new int*[a.size() + 1];
			for(int j = 0; j < a.size() + 1; j++){
					dp[j] = new int[b.size() + 1];
			}
			for(int j = 0; j <= a.size(); j++)dp[j][0] = 0;
			for(int j = 0; j <= b.size(); j++)dp[0][j] = 0;
			for(int j = 0; j < a.size(); j++){
					for(int k = 0; k < b.size(); k++){
							if(a[j] == b[k])dp[j + 1][k + 1] = dp[j][k] + 1;
							else{
									dp[j + 1][k + 1] = max(dp[j][k], dp[j+1][k]);
									dp[j + 1][k + 1] = max(dp[j + 1][k + 1], dp[j][k+1]);
							}
					}
			}
			cout << dp[a.size()][b.size()] << endl;
	}
	return 0;
}

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

vector<vector<int>> init_c(MAX, vector<int>(MAX, -1));
vector<vector<int>> c(MAX, vector<int>(MAX, -1));
string a, b;

int lcs(int i, int j){
    if(i == 0 || j == 0){
        c.at(i).at(j) = 0;

    }else if(c.at(i).at(j) != -1){
        return c.at(i).at(j);

    }else if(a.at(i - 1) == b.at(j - 1)){
        c.at(i).at(j) = lcs(i-1, j-1) + 1;

    }else{
        c.at(i).at(j) = max(lcs(i, j - 1), lcs(i - 1, j));
    }
    // cout << i << " " << j << " " << c.at(i).at(j) << endl;
    
    return c.at(i).at(j);
}

int main(){
    int q; cin >> q;
    rep(i, q){
        c = init_c;
        cin >> a; cin >> b;
        cout << lcs(a.size(), b.size()) << endl;
    }

}

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

string a, b;
int N, M;
vector<vector<int> > dp;

int dfs(int n, int m) {
  if ( n == N || m == M ) return 0;
  if ( dp[n][m] >= 0 ) return dp[n][m];
  
  int ret = 0;
  //cout << n << " " << m << " " << cnt << endl;
  if ( a[n] == b[m] ) ret = dfs(n+1, m+1)+1;
  else ret = max(ret, max(dfs(n+1, m), dfs(n, m+1)));

  return dp[n][m] = ret;
}

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

  while ( q-- ) {
    cin >> a >> b;
    N = a.size();
    M = b.size();
    dp = vector<vector<int> >(N+1, vector<int>(M+1, -1));    
    cout << dfs(0, 0) << endl;
  }
  
  return 0;
}

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

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int q;
    cin >> q;
    vector<int> res(q);

    rep(_, q){
        string x, y;
        cin >> x >> y;
        if(y.size() > x.size()) swap(x, y);

        int n = y.size();
        int m = x.size();

        vector<vector<int>> dp(n, vector<int>(m, 0));
        if(x[0] == y[0]) dp[0][0] = 1;
        else dp[0][0] = 0;

        rep1(i, m){
            if(y[0] == x[i]) dp[0][i] = 1;
            else dp[0][i] = dp[0][i-1];
        }

        rep1(i, n){
            dp[i][0] = dp[i-1][0];
            if(y[i] == x[0]) dp[i][0] = 1;

            rep1(j, m){
                if(y[i] == x[j]) dp[i][j] = dp[i-1][j-1] + 1;
                else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
            }
        }

        // rep(i, n){
        //     rep(j, m) cout << dp[i][j] << " ";
        //     cout << endl;
        // }

        cout << dp[n-1][m-1] << endl;
        
    }

    return 0;
}