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>
#define REP0(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define REPB0(i,n) for(int i=n-1;i>=0;i--)
#define REPB1(i,n) for(int i=n;i>0;i--)
#define llong long long
#define INF 2000000000
#define NIL -1
#define N 1000
using namespace std;
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;
  REP1(i,m) C[i][0]=0;
  REP1(i,n) C[0][i]=0;
  REP1(i,m){
    REP1(j,n){
      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 n;
  scanf("%d",&n);
  REP0(i,n){
    cin>>s1>>s2;
    printf("%d\n",LCS(s1,s2));
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
	int dp[1005][1005];
int LCS(string A,string B){
	memset(dp,0,sizeof dp);
	int n=A.size();
	int m=B.size();
	A=' '+A;
	B=' '+B; 
	int max1=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(A[i]==B[j])	dp[i][j]=dp[i-1][j-1]+1;
			else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			max1=max(max1,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 <iostream>
#include <string>

int LCS(std::string X, std::string Y)
{
    int m = X.size();
    int n = Y.size();
    X = ' ' + X;
    Y = ' ' + Y;
    int c[m+1][n+1];
    for (int i = 0; i <= m; i++) c[i][0] = 0;
    for (int j = 0; 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 if (c[i-1][j] >= c[i][j-1]) {
                c[i][j] = c[i-1][j];
            } else {
                c[i][j] = c[i][j-1];
            }
        }
    }

    return c[m][n];
}

int main(int argc, char *argv[])
{
    int n;
    std::cin >> n;
    for (int i = 0; i < n; i++) {
        std::string X, Y;
        std::cin >> X;
        std::cin >> Y;
        std::cout << LCS(X, Y) << std::endl;
    }
}

C++: #pragma region template
#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD = 1e9 + 7, MAX = 1e18, larg = 1e5, INF = -1e9;
long long A, B, C, D, E, F, G, H, I, J, K, L, N, M, O, P, Q, R, S, T, U, V, W, x, y, z;
long long max_value = INF, max_index = -1;
long long min_value = MAX, min_index = -1;
typedef long long ll;
typedef pair<ll, ll> Pa;

ll dp[1001][1001];
ll ans[150];
int main() {
	cin >> N;
	rep(n, N){
		string X, Y;
		cin >> X;
		cin >> Y;
		rep(i, 1001){
			rep(j, 1001){
				dp[i][j] = 0;
			}
		}
		ll maxv = 0;
		rep(i, X.size()){
			rep(j, Y.size()){
				if(X[i] == Y[j]) dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j]+1);
				dp[i+1][j] = max(dp[i+1][j], dp[i][j]);
				dp[i][j+1] = max(dp[i][j+1], dp[i][j]); 
			}
		}
		rep(i, X.size()+1){
			rep(j, Y.size()+1){
				maxv = max(maxv, dp[i][j]);
			}
		}
		ans[n] = maxv;
	}
	rep(i, N){
		cout << ans[i] << endl;
	}
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> pp;
const int INF = 1e9;
const int MOD = 1000000007;


int main(){
        int q;
        cin >>q;
        rep(i,q){
                string x,y;
                cin >> x >>y;
                vector<vector<int>> dp(x.size()+1,vector<int>(y.size()+1,0));
                
                for(int i=1;i<=x.size();i++){
                        for(int j=1;j<=y.size();j++){
                                if(x.at(i-1) == y.at(j-1)) dp.at(i).at(j) = dp.at(i-1).at(j-1) + 1;
                                else dp.at(i).at(j) = max(dp.at(i-1).at(j),dp.at(i).at(j-1));
                        }
                }
                
                cout << dp.at(x.size()).at(y.size()) << 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; // X[0] に空白を挿入
    Y = ' ' + Y; // Y[0] に空白を挿入
     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 n; cin >> n;
                                               for ( int i = 0; i < n; i++ ) {
                                                   cin >> s1 >> s2;
                                                    cout << lcs(s1, s2) << endl;
                                                   }
                                                return 0;
                                                }

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

int lcs(string str1, string str2) {
	int n1 = str1.size();
	int n2 = str2.size();
	int A[n1 + 1][n2 + 1];
	for (int i = 0; i <= n1; i++) A[i][0] = 0;
	for (int j = 1; j <= n1; j++) A[0][j] = 0;

	for (int i = 1; i <= n1; i++) {
		for (int j = 1; j <= n2; j++) { 
			if (str1[i - 1] == str2[j - 1]) {
				A[i][j] = A[i - 1][j - 1] + 1; 
			} else {
				A[i][j] = max(A[i - 1][j], A[i][j - 1]);
			}
		}
	}
	return A[n1][n2];
}

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

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
  
static const int N = 1000;
  
int las(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 n; cin >> n;
for ( int i = 0; i < n; i++ ) {
cin >> s1 >> s2;
cout << lcs(s1, s2) << endl;
}
return 0;
}