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 ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#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 srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

const int MAX_SIZE = 52;

int LCS(const char* a, const char* b){
    int n = 0, m = 0;
    vector<int> x,y;
    for(;a[n] != '\0';n++){
        int s = a[n]-'a';
        if(s < 0){
            s = a[n]-'A'+26;
        }
        x.pb(s);
    }
    for(;b[m] != '\0';m++){
        int s = b[m]-'a';
        if(s < 0){
            s = b[m]-'A'+26;
        }
        y.pb(s);
    }
    vector<vector<int> > vec(MAX_SIZE);
    for(int j = m-1; j >= 0; --j){
        vec[y[j]].push_back(j);
    }
    vector<int> xs(n+1, INF);
    xs[0] = -INF;
    vector<P> nx(n+1,P(-INF,-INF));
    rep(i,n){
        vector<int>& ys = vec[x[i]];
        rep(j,(int)ys.size()){
            int k = lower_bound(xs.begin(),xs.end(),ys[j]) - xs.begin();
            xs[k] = ys[j];
            nx[k] = P(y[ys[j]], k-1);
        }
    }
    return lower_bound(xs.begin(),xs.end(),INF-1) - xs.begin() - 1;
}


int main()
{
    int q;
    scanf("%d",&q);
    rep(i,q){
        char x[1000], y[1000];
        scanf("%s%s",x,y);
        cout << LCS(x,y) << "\n";
    }
    return 0;
}

C++: #include <algorithm>
#include<iostream>
#include<vector>
#include<deque>
#include<queue>
#include<list>
#include<stack>
#include<map>
#include<set>
#include<string>
#include <sstream>
#include<bitset>

#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<limits.h>

using namespace std;
const int INF = 0x7fffffff;

int gcd(int x,int y){
  int mi,ma;
  ma = max(x,y);
  mi = min(x,y);
  if(ma % mi == 0){
    return mi;
  }
  else{
    return gcd(mi,ma % mi);
  }
}
int dp[1001][1001];

int lcs(string x,string y){

  memset(dp,0,sizeof(dp));
  int m,n,ii,jj,maxl;

  m = x.size();
  n = y.size();
  maxl = 0;
  x = ' ' + x;
  y = ' ' + y;
  for(ii=1;ii<=m;ii++){
    for(jj=1;jj<=n;jj++){

      if( x[ii] == y[jj]){
        //cout << "hoge" << endl;

        dp[ii][jj] = dp[ii - 1][jj -1] + 1;

      }
      
      else{

        dp[ii][jj] = max(dp[ii-1][jj],dp[ii][jj-1]);

      }
      maxl = max(maxl,dp[ii][jj]);      
    }
  }
  
  return maxl;

}

int main(){
  
  long ii,jj,kk;
  vector<int> a;

  int q;
  cin >> q;

  string x,y;
  int len;
  
  for(ii=0;ii<q;ii++){
    
    cin >> x >> y;
    len = lcs(x,y);

    cout << len << endl;
    
  }
  
  return 0;
}

C++: #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#define MAX 150
void solve(void);
int q,count[MAX][MAX]={},xlength,ylength;
char X[MAX],Y[MAX];

void solve(void){
  int i,j;

  xlength=strlen(X);
  ylength=strlen(Y);
  
  for(i=1;i<=strlen(X);i++){
    for(j=1;j<=strlen(Y);j++){
      if(X[i-1]==Y[j-1]) count[i][j]=count[i-1][j-1]+1;
      else if(count[i-1][j]>=count[i][j-1]) count[i][j]=count[i-1][j];
      else count[i][j]=count[i][j-1];
    }
  }

}
    

int main()
{
  int i;
  
  scanf("%d",&q);

  for(i=1;i<=q;i++){
    scanf("%s",X);
    scanf("%s",Y);
    solve();
    printf("%d\n",count[xlength][ylength]);
  }

  /*for(i=0;i<q;i++){
    printf("%d\n",count[][]);
    }*/

  return 0;
}

C++: #include<iostream>
#include<cstring>
using namespace std;
int q;
int main(){
  cin>>q;
  for(int z=0;z<q;z++){
    string a,b;
    cin>>a>>b;
    int n=a.size(),m=b.size();
    int dp[n+1][m+1];
    memset(dp,0,sizeof(dp));
    for(int i=0;i<n;i++){
      for(int j=0;j<m;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]);
        }
      }
    }
    cout<<dp[n][m]<<endl;
  }
}

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

int main(){
    int N;
    cin>>N;
    int ans[151];
    if(N>20){
        for(int i=0;i<N;i++){
            string inp1,inp2;
            cin>>inp1;
            cin>>inp2;
            int dp[101][101]={};
            for(int j=1;j<=inp1.size();j++){
                for(int k=1;k<=inp2.size();k++){
                    if(inp1[j]==inp2[k]){
                        dp[j][k]=dp[j-1][k-1]+1;
                    }else{
                        dp[j][k]=max(dp[j-1][k],dp[j][k-1]);
                    }
                }
            }
            ans[i]=dp[inp1.size()][inp2.size()];
        }
    }else{
        for(int i=0;i<N;i++){
            string inp1,inp2;
            cin>>inp1;
            cin>>inp2;
            int dp[1001][1001]={};
            for(int j=1;j<=inp1.size();j++){
                for(int k=1;k<=inp2.size();k++){
                    if(inp1[j]==inp2[k]){
                        dp[j][k]=dp[j-1][k-1]+1;
                    }else{
                        dp[j][k]=max(dp[j-1][k],dp[j][k-1]);
                    }
                }
            }
            ans[i]=dp[inp1.size()][inp2.size()];
        }
    }
    for(int i=0;i<N;i++){
        cout<<ans[i]<<endl;
    }
    return 0;
}