Go: package main

import (
	. "fmt"
    "math"
)

// func init() { Println("----------------------------------------") }

func main() {
    const L_MAX = 1003

	var q int
	Scanf("%d", &q)
	X := make([]string, 0, 150)
	Y := make([]string, 0, 150)
	for i := 0; i < 2 * q; i++ {
	    var tmp string
	    Scanf("%s", &tmp)

	    if i % 2 == 0 {
	        X = append(X, tmp)
	    } else {
	        Y = append(Y, tmp)
	    }
	}

    for i := 0; i < q; i++ {
        dp := make([][]int, L_MAX)
        for k, _ := range dp {
            dp[k] = make([]int, L_MAX)
        }

        x, y := X[i], Y[i]

        // init
        isXFound := false
        for c := 0; c < len(x); c++ {
            if isXFound || y[0] == x[c] {
                dp[0][c] = 1
                isXFound = true
            }
        }
        isYFound := false
        for r := 0; r < len(y); r++ {
            if isYFound || x[0] == y[r] {
                dp[r][0] = 1
                isYFound = true
            }
        }

        for r := 1; r < len(y); r++ {
            for c := 1; c < len(x); c++ {
                // dp[r][c]
                if x[c] == y[r] {
                    dp[r][c] = int(math.Max(float64(dp[r - 1][c - 1] + 1), float64(dp[r][c])))
                }

                dp[r][c] = int(math.Max(float64(dp[r][c]), math.Max(float64(dp[r - 1][c]), float64(dp[r][c - 1]))))
            }
        }
        Println(dp[len(y) - 1][len(x) - 1])

        // LCSも求めておく
        // ans := ""
        // r, c := len(y) - 1, len(x) - 1
        // for r >= 0 && c >= 0 {
        //     if y[r] == x[c] {
        //         ans = string(x[c]) + ans
        //         r--
        //         c--
        //     } else {
        //         if dp[r - 1][c] > dp[r][c - 1] {
        //             r--
        //         } else {
        //             c--
        //         }
        //     }
        // }
        // Println(ans)
    }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
 
static const long N = 1000;
 
long lcs(string X, string Y){
    long c[N + 1][N + 1];
    long m = X.size();
    long n = Y.size();
    long maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;
    for (long i = 1; i <= m; i++) c[i][0] = 0;
    for (long j = 1; j <= n; j++) c[0][j] = 0;
 
    for (long i = 1; i <= m; i++){
        for (long 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 = c[i][j];
        }
    }
 
    return maxl;
}
 
int main(){
    string s1, s2;
    long n;
    cin >> n;
    for (long i = 0; i < n; i++){
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
    return 0;
}

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

int Longest(string x, string y) {
  int L[1001][1001];
  int maxl = -1;
  int xlen = x.size();
  int ylen = y.size();

  x = ' ' + x;
  y = ' ' + y;

  for (int i = 1; i <= xlen; i++) L[i][0] = 0;
  for (int j = 1; j <= ylen; j++) L[0][j] = 0;

  for (int i = 1; i <= xlen; i++) {
    for (int j = 1; j <= ylen; j++) {
      if (x[i] == y[j])
        L[i][j] = L[i-1][j-1] + 1;
      else
        L[i][j] = max(L[i][j-1], L[i-1][j]);

      maxl = max(maxl, L[i][j]);
    }
  }

  return maxl;
}

int main() {
  string X, Y;
  int n;
  int result;

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> X;
    cin >> Y;
    result =  Longest(X, Y);
    cout << result << endl;
  }

  


  return 0;
}

C++: #include <iostream> 
#include <cstring> 
using namespace std;
int c[1001][1001];

int lcs(string x, string y)
{
    for (int i = 0; i < x.size(); i++)
    {
        c[i][0] = 0;
    } 
    for (int i = 0; i < y.size(); i++)
    {
        c[0][i] = 0; 
    }
    for (int i = 1; i <= x.size(); i++)
    {
        for (int j = 1; j <= y.size(); j++)
        {
            if (x[i]==y[j])
            { 
                c[i][j] = c[i][j-1] + 1; 
            }else
            {
                c[i][j] = max(c[i][j-1], c[i-1][j]);
            }
        } 
    } 
    return c[x.size()][y.size()];
}

int main()
{
    string x,y;
    int q;
    cin>>q;
    while(q--)
    {
        cin>>x>>y;
        int ans = lcs(x, y);
        cout<<ans<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
//BEGIN CUT HERE
struct LongestCommonSubstring{
  struct node{
    int value;
    node *next;
    node(int value,node *next):value(value),next(next){}
  };
  const int INF=1LL<<55LL;
  string x,y;
  LongestCommonSubstring(){}
  LongestCommonSubstring(string x,string y):x(x),y(y){}
  string lcs(){
    int n=x.size(),m=y.size();
    map<char,vector<int> > M;
    for(int j=m-1;j>=0;j--) M[y[j]].push_back(j);
    vector<int> xs(n+1,INF);
    xs[0]=-INF;
    vector<node* > link(n+1);
    for(int i=0;i<n;i++){
      if(M.count(x[i])){
	vector<int> ys=M[x[i]];
	for(int j=0;j<(int)ys.size();j++){
	  int k=distance(xs.begin(),lower_bound(xs.begin(),xs.end(),ys[j]));
	  xs[k]=ys[j];
	  link[k]=new node(y[ys[j]],link[k-1]);
	}
      }
    }
    string res;
    int l=distance(xs.begin(),lower_bound(xs.begin(),xs.end(),INF-1))-1;
    for(node *p=link[l];p!=NULL;p=p->next)
      res.push_back(p->value);
    return res;
  }
};
//END CUT HERE
signed main(){
  int q;
  cin>>q;
  while(q--){
    string x,y;
    cin>>x>>y;
    LongestCommonSubstring lcs(x,y);
    cout<<lcs.lcs().size()<<endl;
  }
  return 0;
}
/*
verified on 2017/06/29
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C
*/

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

signed main(){

  int q; cin >> q;
  while(q-- > 0){
    string x,y; cin >> x >> y;
    int dp[x.size()+1][y.size()+1];
    fill(dp[0],dp[x.size()+1],0);
    for(int i = 0; i < x.size(); i++){
      for(int j = 0; j < y.size(); j++){
        if(x[i] == y[j]) dp[i+1][j+1] = dp[i][j] + 1;
        else dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1]);
      }
    }
    cout << dp[x.size()][y.size()] << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
#define INF 1e9
#define all(v) v.begin(), v.end()
#define rep(i, n) for(int i = 0; i < n; i++)
#define pb push_back
#define eb emplace_back
#define mp make_pair
using namespace std;

int main(){
    int dp[1001][1001];
    string x, y;
    int q, n, xl, yl, ans;
    cin>>q;
    for(int k = 0; k < q; k++){
        cin>>x>>y;
        for(int i = 0; i <= 1000; i++){
            for(int j = 0; j <= 1000; j++){
	            dp[i][j]=0;
            }
            xl = x.size();
            yl = y.size();
            ans = 0;
        }
        for(int i = 1; i <= xl; i++){
            for(int j = 1; j <= yl; j++){
	            if(x[i-1] == y[j-1]){
	                dp[i][j] = dp[i-1][j-1]+1;
            	}else{
            	    dp[i][j]=max(dp[i-1][j], dp[i][j-1]);
            	}
            }
        }
    cout<<dp[xl][yl]<<endl;
    }
  return 0;
}

C++: #include<iostream>

using namespace std;

int dp[1001][1001];

int main(){
    int q, m, n;
    cin >> q;

    while(q-- > 0){
        string x, y;
        cin >> x >> y;
        x = ' ' + x;
        y = ' ' + y;
        m = x.length();
        n = y.length();
        for(int i = 1; i <= m; i++) dp[i][0];
        for(int i = 1; i <= n; i++) dp[0][i];

        int res = 0;
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(x[i] == y[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]-1 << endl;
    }

    return 0;
}