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 <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int q;
char X[1000], Y[1000];
int dp[1001][1001];

int main(void) {

    scanf("%d", &q);

    for (int i = 0; i < q; ++i) {
//        scanf("%s %s", X, Y);
        scanf("%s", X);
        scanf("%s", Y);
        int xlen = strlen(X), ylen = strlen(Y);
        for (int x = 0; x < xlen; ++x) {
            for (int y = 0; y < ylen; ++y) {
                if (X[x] == Y[y]) {
                    dp[x + 1][y + 1] = dp[x][y] + 1;
                }
                else {
                    if (dp[x + 1][y] > dp[x][y + 1]) {
                        dp[x + 1][y + 1] = dp[x + 1][y];
                    }
                    else {
                        dp[x + 1][y + 1] = dp[x][y + 1];
                    }
                }
            }
        }
        printf("%d\n", dp[xlen][ylen]);
    }

    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxx = 1010;
int c[maxx][maxx];

int lcs(char s1[],char s2[]) {
    int m = strlen(s1), n = strlen(s2);
    for (int i = 0; i <= m; i++)
    c[i][0] = 0;
    for (int i = 0; i <= n; i++)
    c[0][i] = 0;
    
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (s1[i] == s2[j])
            c[i+1][j+1] = c[i][j] + 1;
            else
            c[i+1][j+1] = max(c[i][j+1], c[i + 1][j]);
        }
    }
    return c[m][n];
}
int main()
{
    char s1[maxx], s2[maxx];
    int t;
    scanf("%d", &t);
    for (int i = 0; i < t; i++) {
        scanf("%s %s", s1, s2);
        printf("%d\n",lcs(s1,s2));
    }
    
    return 0;
}

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

using namespace std;

string str1,str2;


int LCS(int i,int j){
  
  if(i==-1||j==-1){
    return 0;
  }else if(str1[i]==str2[j]){
    return LCS(i-1,j-1)+1;
  }else{
    return max(LCS(i-1,j),LCS(i,j-1));
  }
  
}

int main(){
  int N;
  
  cin >> N;
  for(int i=0;i<N;i++){
    cin >> str1 >> str2;
    cout << LCS(str1.size()-1,str2.size()-1) << 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;
  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 <iostream>
#include <string>
#include <utility>
using namespace std;
#define MAXQ 150
#define MAXL 1000

int main(){
    int n; cin >> n;
    pair<string, string> pairs[MAXQ];
    for(int i=0; i<n; ++i){
        cin >> pairs[i].first >> pairs[i].second;
    }
    
    for(int i=0; i<n; ++i){
        string str1, str2;
        str1 = pairs[i].first;
        str2 = pairs[i].second;
        int dp[MAXL][MAXL];
        for(int r=0; r<=str1.size(); ++r){
            dp[r][0] = 0;
        }
        for(int c=0; c<=str2.size(); ++c){
            dp[0][c] = 0;
        }
        for(int r=1; r<=str1.size(); ++r){
            for(int c=1; c<=str2.size(); ++c){
                int d = 0;
                if (str1[r-1] == str2[c-1]) d++;
                dp[r][c] = max(dp[r-1][c-1]+d, max(dp[r-1][c], dp[r][c-1]));
            }
        }
        cout << dp[str1.size()][str2.size()] << endl;
    }
}

C++: #include<iostream>
#include<string>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
int main(){
    int n,dp[1001][1001];
    cin>>n;
    string x,y;
    rep(i,n){
        cin>>x>>y;
        rep(j,x.size()+1) rep(k,y.size()+1) dp[j][k]=0;
        rep(j,x.size()) rep(k,y.size()){
            if(x[j]==y[k]) dp[j+1][k+1]=max(max(dp[j][k]+1,dp[j+1][k]),dp[j][k+1]);
            else dp[j+1][k+1]=max(dp[j+1][k],dp[j][k+1]);
        }
        cout<<dp[x.size()][y.size()]<<endl;
    }
}

C++: //	created : 16/03/15
// 	author   : Rp7rf
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define vi vector<int>
#define mp make_pair
#define pb push_back
#define MOD 1000000007
#define INF 1e9


int main(void){
	int n;
	string s1,s2;
	cin>>n;
	int dp[1010][1010];
	rep(i,n){
		int ret = 0;
		cin>>s1>>s2;
		rep(i,s1.size()+1)dp[i][0] = 0;
		rep(j,s2.size()+1)dp[0][j] = 0;

		loop(i,1,s1.size()+1){
			loop(j,1,s2.size()+1){
				if(s1[i] == s2[j]){
					dp[i][j] = dp[i-1][j-1] + 1;
				}else{
					dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
				}
				ret = max(ret,dp[i][j]);
			}
		}
		cout<<ret<<endl;
	}
}