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 <cstring>
using namespace std;

int q;
int **len;
int common(string a,string b,int m,int n){
    if(m==0||n==0){
        return 0;
    }
    if(a[m-1]==b[n-1]){
        if(m>1&&n>1){
            if(len[m-2][n-2]!=-1)len[m-1][n-1]=len[m-2][n-2]+1;
            else len[m-1][n-1]=common(a.substr(0,m-1),b.substr(0,n-1),m-1,n-1)+1;
        }else{
            len[m-1][n-1]=1;
        }
    }
    else{
        int l1,l2;
        //?±?l1
        if(n>1){
            if(len[m-1][n-2]!=-1)l1=len[m-1][n-2];
            else l1=len[m-1][n-2]=common(a,b.substr(0,n-1),m,n-1);
        }else l1=-1;
        if(m>1){
            if(len[m-2][n-1]!=-1)l2=len[m-2][n-1];
            else l2=len[m-2][n-1]=common(a.substr(0,m-1),b,m-1,n);
        }else l2=-1;

        len[m-1][n-1]=max(l1,l2);
    }
    return len[m-1][n-1];
}

int main()
{
    cin>>q;
    int i;
    for(i=0;i<q;i++){
        string a,b;
        cin>>a>>b;
        int m=a.length();
        int n=b.length();
        len=new int*[m];
        int j;
        for(j=0;j<m;j++){
            len[j]=new int[n];
            memset(len[j],-1,n*sizeof(int));
        }
        cout<<common(a,b,m,n)<<endl;
    }
    return 0;
}

//int main(){
//    int **p=new int*[5];
//    p[1]=new int[5];
//    memset(p[1],-1,5*sizeof(int));
//    cout<<p[1][3]<<" "<<p[0][1];
//}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int MAX = 1000;
int c[MAX + 1][MAX + 1];
int lcs(string x, string y)
{
	int m = x.length();
	int n = y.length();
	int maxLen = 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]);
			maxLen = max(maxLen, c[i][j]);
		}
	return maxLen;
}
int main()
{
	int n;
	(cin >> n).get();
	for (int i = 0; i < n; i++)
	{
		string x, y;
		getline(cin, x);
		getline(cin, y);
		cout << lcs(x, y) << endl;
	}
}

C++: #include<stdio.h>

int strlen(char s[]) {
  int w = 0;
  while (1) {
    if (s[w] == '\0')
      break;
    w++;
  }
  return w;
}

int max(int a, int b, int c){
  int m = a;
  if (m < b)
    m = b;
  if (m < c)
    m = c;
  return m;
}

int main(void){
  int q, xl = 0, yl = 0, h = 0, i = 0, j = 0;
  int d, ans = 0;
  int DP[1001][1001];
  char X[1001], Y[1001];

  for (i = 0; i < 1001; i++) {
    DP[i][0] = 0;
  }
  for (i = 0; i < 1001; i++) {
    DP[0][i] = 0;
  }

  scanf("%d", &q);
  for (h = 0; h < q; h++) {

    scanf("%s", X);
    scanf("%s", Y);
    xl = strlen(X);
    yl = strlen(Y);
    //printf("xl:%d, yl:%d\n", xl, yl);

    for (i = 1; i <= xl; i++) {
       for (j = 1; j <= yl; j++) {
        if (X[i-1] == Y[j-1])
          d = 1;
        else
          d = 0;
        DP[i][j] = max(DP[i-1][j-1], DP[i-1][j], DP[i-1][j]) + d;
        if(DP[i][j] > ans)
          ans = DP[i][j];
      }
    }
    printf("%d\n", ans);
    ans = 0;
  }
  return 0;
}

C++: #include<cstdio>
#include<stdlib.h>
#include<iostream>
using namespace std;

const int maxn = 1005;

int T[maxn][maxn];

int calculateCommon(string s1,string s2){
	for(int i = 0;i <= s1.length();i++){
		for(int j = 0;j <= s2.length();j++){
			if(i == 0 || j == 0){
				T[i][j] = 0;
			}else{
				if(s1[i - 1] == s2[j - 1]){
					T[i][j] = T[i - 1][j - 1] + 1;
				}else{
					T[i][j] = (T[i][j - 1] < T[i - 1][j]) ? T[i - 1][j]:T[i][j - 1];
				}
			}
	//		cout << "T[" << i << "][" << j << "] = " << T[i][j] << endl;
		}
	}
	return T[s1.length()][s2.length()];
}

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

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

using namespace std;

#if 0
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2065462#1
#endif

int n;
string a, b;
string x[10000], y[10000];




int main(){
  cin >> n;
  while(n != 0){
    n = n -1;
    int longest[1000][1000];
    cin >> a;
    cin >> b;
    a = ' ' + a;
    b = ' ' + b;
    int mm = 0;
    /*
    for(int i = 0; i < a.size(); ++i){
      for(int j = 0; j < b.size(); ++j){
        
        x[i] = a;
        y[j] = b;
      }
    }
    */
   //longest[0][0] =0;
   for(int i = 1; i <= a.size(); ++i){
     longest[i][0] = 0;
    }
   for(int j = 1; j <= b.size(); ++j){
     longest[0][j] = 0;
   }
    for(int i = 1; i <= a.size(); ++i){
      for(int j = 1; j <= b.size(); ++j){
        
        /*
        if(i <= 0 || j <=0){
          longest[i][j] = 0;
        }
        */
        if(a[i] == b[j]){
          longest[i][j] = 1 + longest[i-1][j - 1];
        }
        else{
          longest[i][j] = max(longest[i][j-1], longest[i-1][j]);
        }
        mm = max(mm, longest[i-1][j-1]);
      }
    }
    //cout << a[1] << endl;
    cout << mm << endl;
  }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
    using namespace std;
int lcs(string x,string y){
    int c[1010][1010];
    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(){
    int q;
    string x;
    string y;
    scanf("%d",&q);
    for (int i=0; i<q; i++) {
        cin>>x>>y;
        cout<<lcs(x, y)<<endl;
    }
    return 0;
}