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++: import java.io.*;
import static java.lang.Math.max;
import static java.lang.Integer.parseInt;

class Main {
   final static PrintWriter out = new PrintWriter( System.out );

   final static BufferedReader in =
         new BufferedReader( new InputStreamReader( System.in ) );

   final static StringBuilder sb = new StringBuilder("");

   static int lcs(char[] a, char[] b){
      final int alen = a.length;
      final int blen = b.length;
      int dp[][] = new int[alen+1][blen+1];

      for(int i=0; i<alen; ++i){
         for(int j=0; j<blen; ++j){
            if(a[i] == b[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[alen][blen] );
   }

   public static void main(String[] args) throws IOException {
      final int N = parseInt( in.readLine() );
      for(int i=0; i<N; ++i){
         char[] a = in.readLine().toCharArray();
         char[] b = in.readLine().toCharArray();
         sb.append( lcs(a,b) );
         sb.append('\n');
      }
      System.out.print(sb);
   }
}

C++: #include <iostream>
#include <string>
using namespace std;
int main(void){
    int n;
    cin >> n;
    for(int i = 0;i < n;i++){
        string X,Y;
        cin >> X >> Y;
        int L[X.length()+10][Y.length()+10];
        for(int j = 0;j < X.length()+10;j++){
            for(int k = 0;k < X.length()+10;k++){
                L[j][k] = 0;
            }
        }
        for(int j = 1;j<=X.length();j++){
            for(int k = 1;k<=Y.length();k++){
                if(Y[k] == X[j]){
                    L[j][k] = 1+L[j-1][k-1];
                }
                else{
                    L[j][k] = max(L[j-1][k],L[j][k-1]);
                }
            }
        }
        cout << L[X.length()][Y.length()] << endl;
    }
return 0;
}

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

int dp[1001][1001];

int lcs(string x, string y)
{
	fill(dp[0], dp[0] + x.length() * y.length(), 0);
	for (int i = 0; i < x.length(); ++i)
	{
		for (int j = 0; j < y.length(); ++j)
		{
			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[x.length()][y.length()];
}

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++: #define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<functional>
#include<algorithm>
#include<stack>
#include<queue>
#include<iomanip>
#include<set>
#include<map>
#include<utility>
#include<string>
#include<vector>

using namespace std;

#define LL long long

string X,Y;
int q,Z;
int dp[1001][1001]={};

int main() {
	cin>>q;
	for(int ds=0;ds<q;ds++){
		cin>>X>>Y;
		int xs=X.size(),ys=Y.size();
		for(int i=0;i<xs;i++){
			for(int j=0;j<ys;j++){
				int tmp=0;
				if(X[i]==Y[j]){
					if(i*j==0)dp[i][j]=1;
					else dp[i][j]=dp[i-1][j-1]+1;
				}
				else {
					if(i+j==0)dp[0][0]=0;
					else if(i==0)dp[0][j]=dp[0][j-1];
					else if(j==0)dp[i][0]=dp[i-1][0];
					else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
				}
			}
		}
		cout<<dp[xs-1][ys-1]<<endl;
	}
	return 0;
}

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

int lcs(string X, string Y)
{
	static int Z[1001][1001];
	int m = X.size();
	int n = Y.size();
	int i, j;

	for (i = 1; i <= m; i++) {
		for (j = 1; j <= n; j++) {
			if (X[i - 1] == Y[j - 1])
				Z[i][j] = Z[i - 1][j - 1] + 1;
			else
				Z[i][j] = max(Z[i - 1][j], Z[i][j - 1]);
		}
	}
	return Z[m][n];
}

int main()
{
	string X, Y;
	int q;
	scanf("%d", &q);
	for (int i = 0; i < q; i++) {
		cin >> X >> Y;
		cout << lcs(X, Y) << endl;
	}
	return 0;
}

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

static const int N = 1000;
int c[N+1][N+1];

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

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

    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]= std::max(c[i-1][j], c[i][j-1]);
            }
        }
    }
    return c[m][n];
}

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

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

void solve(string, string);
int main(){
  int q; cin >> q;

  for(int i=0; i<q; i++){
    string s, t;  cin >> s >> t;
    solve(s, t);
  }

  return 0;
}

void solve(string s, string t){
  int m=s.length(), n=t.length();
  s = " " + s; t = " " + t;

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

  for(int i=1; i<=m; i++){
    for(int j=1; j<=n; j++){
      if(s[i]==t[j]){
        dp[i][j] = dp[i-1][j-1]+1;
      }
      else{
        dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
      }
    }
  }

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

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 n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> s1 >> s2;
		cout << lcs(s1, s2) << endl;
	}

	return 0;
}

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

#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = (n)-1; i >= 0; i--)
#define rep1(i,n) for(int i = 1; i <= (n); i++)
#define rrep1(i,n) for(int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

#define MAXLEN 1001
int dp[MAXLEN][MAXLEN];

int main(){
  int n;cin>>n;
  rep(i, n){
    rep(i, MAXLEN)rep(j, MAXLEN)dp[i][j]=0;

    string l,r;cin>>l>>r;
    rep(x, l.size())rep(y, r.size()){
      if(l[x]==r[y]) dp[x+1][y+1] = dp[x][y]+1;
      else dp[x+1][y+1] = max(dp[x+1][y], dp[x][y+1]);

    // cout<<r[x]<<' '<<l[y]<<endl;
    // rep(x, l.size()+1){
    //   rep(y, r.size()+1)cout<<dp[x][y]<<' ';cout<<endl;
    // }
    // cout<<"#######"<<endl;

    }

    cout<<dp[l.size()][r.size()]<<endl;

  }
  return 0;
}