Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Position struct {
	X int
	Y int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}
	for i := 0; i < n; i++ {
		strX := nextText(sc)
		strY := nextText(sc)
		fmt.Println(calcLcs(strX, strY))
	}
}

func calcLcs(strX, strY string) int {
	return lcs(strX, strY, Position{0, 0}, make(map[Position]int))
}

func lcs(strX string, strY string, position Position, memo map[Position]int) int {
	if position.X >= len(strX) || position.Y >= len(strY) {
		return 0
	}
	if v, ok := memo[position]; ok {
		return v
	}
	if strX[position.X] == strY[position.Y] {
		result := lcs(strX, strY, Position{position.X + 1, position.Y + 1}, memo)
		memo[position] = result + 1
		return memo[position]
	} else {
		result1 := lcs(strX, strY, Position{position.X + 1, position.Y}, memo)
		result2 := lcs(strX, strY, Position{position.X, position.Y + 1}, memo)
		var result int
		if result1 > result2 {
			result = result1
		} else {
			result = result2
		}
		memo[position] = result
		return result
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

#define MIN(X,Y) ((X)<(Y)?(X):(Y))
#define MAX(X,Y) ((X)>(Y)?(X):(Y))

int dp[1001][1001];

int main(void){
  char x[1001], y[1001];
  int xln, yln;
  int dataNum;
  
  cin >> dataNum;
  for (int did=0; did<dataNum; did++){
    cin >> &(x[1]) >> &(y[1]);
    xln = strlen(x+1); yln = strlen(y+1);
    // dp tableの初期化
    for (int i=0; i<=xln; i++) dp[i][0] = 0;
    for (int j=0; j<=yln; j++) dp[0][j] = 0;
    // dp
    for (int i=1; i<=xln; i++){
      for (int j=1; j<=yln; j++){
        if (x[i]==y[j]){
          dp[i][j] = dp[i-1][j-1]+1;
        } else {
          dp[i][j] = MAX(dp[i][j-1], dp[i-1][j]);
        }
      }
    }
/*    for (int i=0; i<=yln; i++){
      for (int j=0; j<=xln;j++){
        cout << dp[j][i] << " ";
      }
      cout << endl;
    }*/
    cout << dp[xln][yln] << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int dp[1002][1002];

int main(){
	int n;cin >> n;
	string x, y;
	while(n--){
		fill((int*)dp, (int*)(dp+1002), 0);
		cin >> x >> y;
		for(int i = 1;i <= x.size();i++){
			for(int j = 1;j <= y.size();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[x.size()][y.size()] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <climits>
#include <cstring>
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))

using namespace std;

const int N_MAX = 1000000;
string a[200];
string b[200];
int dp[10000][10000];

int solve(string a, string b){
  int N = a.length();
  int M = b.length();
  int n = max(N, M);
  rep(i, 0, n+1){
    rep(j, 0, n+1){
      dp[i][j] = 0;
    }
  }

  /*
    ...X
    .....X
    dp[i][j] = dp[i-1][j-1] + 1

    ...X
    .....Y
    dp[i][j] = max(dp[i-1][j], dp[i][j-1])
   */
  
  rep(row, 1, N+1){
    rep(col, 1, M+1){
      if(a[row-1] == b[col-1]){
	dp[row][col] = dp[row-1][col-1] + 1;
      }
      else{
	dp[row][col] = max(dp[row-1][col], dp[row][col-1]);
      }
    }
  }
  /*
  rep(row, 0, n+1){
    rep(col, 0, n+1){
      cout << dp[row][col] << " ";
    }
    cout << endl;
    }*/
  return dp[a.length()][b.length()];
}

int main(){
  int n;
  cin >> n;

  rep(i, 0, n){
    cin >> a[i];
    cin >> b[i];
  }
  
  rep(i, 0, n){
    cout << solve(a[i], b[i]) << endl;
  }
  
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include<string.h>
using namespace std;
int LCS(char *x,char *y);
char x[1001], y[1001];
int lcs[1001][1001];
int main()
{
	int q,i,j;

	cin >> q;
	for (i = 0; i < q; i++) {
		cin >> x;
		cin >> y;
		cout << LCS(x, y) << endl;
	}
}

int LCS(char *x, char *y)
{
	int i, j;
	int len_x, len_y;
	len_x = strlen(x);
	len_y = strlen(y);
	for (i = 0; i < len_x; i++) {
		for (j = 0; j < len_y; j++) {
			lcs[i][j] = '\0';
		}
		//cout << endl;
	}
	i = 0;
	while (x[i] != y[0]) {
		lcs[i][0] = 0;
		i++;
		if (i == len_x) break;

	}
	for (j = i; j < len_x; j++) {
		lcs[j][0] = 1;
	}
	while (y[i] != x[0]) {
		lcs[0][i] = 0;
		i++;
		if (i == len_y) break;
	}
	for (j = i; j < len_y; j++) {
		lcs[0][j] = 1;
	}

	for (i = 1; i <= len_x; i++) {
		for (j = 1; j <= len_y; j++) {
			if (x[i] == y[j]) {
				lcs[i][j] = lcs[i - 1][j - 1] + 1;
				//printf("lcs[%d][%d]=%d\n", i, j, lcs[i - 1][j - 1] + 1);
			}
			if (x[i] != y[j]) {
				lcs[i][j] = max(lcs[i - 1][j], lcs[i][j - 1]);
				//printf("lcs[%d][%d]=%d\n", i, j, max(lcs[i - 1][j], lcs[i][j - 1]));
			}
		}
	//cout << endl;

	}
	for (i = 0; i < len_x; i++) {
		for (j = 0; j < len_y; j++) {
			//printf("%d ", lcs[i][j]);
		}
		//cout << endl;
	}
	//cout << "len_x=" << len_x << endl << "len_y=" << len_y << endl;
			
	return lcs[i-1][j-1];
}

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

string x;
string y;

int lcs(string x, string y){

  int c[1024][1024] = {{}};

  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 - 1][j - 1] + 1;
      else  c[i][j] = max(c[i - 1][j],c[i][j - 1]);

/*
      for(int k = 1;k <= x.size();k++){
        for(int l = 1;l <= y.size();l++){
          cout << c[i][j] << ' ';
        }
        cout << endl;
      }

      */
    }
  }

  return c[x.size()][y.size()];
}



int main(){
  int q;
  cin >> q;

  for(int i = 0;i < q;i++){
    cin >> x;
    cin >> y;
    cout << lcs(x,y) << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int dp[1010][1010];
int main(){
  int q;
  int ans[100]={};
  cin>>q;
  for(int cn=0;cn<q;cn++){
    string x,y;
    cin>>x>>y;
    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]=max(dp[i+1][j+1],dp[i][j]+1);
	dp[i+1][j]=max(dp[i+1][j],dp[i][j]);
	dp[i][j+1]=max(dp[i][j+1],dp[i][j]);
      }
    }
    ans[cn]=dp[x.size()][y.size()];
    //cout<<dp[x.size()][y.size()]<<endl;
  }
  for(int i=0;i<q;i++)cout<<ans[i]<<endl;
  return 0;
}

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

using namespace std;

class Dataset{
private:
 string X;
 string Y;
public:
 Dataset(){
  string tmp = "";
  cin >> tmp; X = tmp; X = " " + X;
  cin >> tmp; Y = tmp; Y = " " + Y;
 };

 unsigned long getLongestCommonLength(){
  unsigned long Xsize = X.size();
  unsigned long Ysize = Y.size();
 
  vector< vector<unsigned long> > LC(Xsize, vector<unsigned long>(Ysize, 0));

  for(unsigned long X_i = 1; X_i < Xsize; ++X_i){
   for(unsigned long Y_i = 1; Y_i < Ysize; ++Y_i){
    unsigned long tmp = LC[X_i-1][Y_i-1];
    if(X[X_i] == Y[Y_i]) ++tmp;
    if(tmp < LC[X_i-1][Y_i]) tmp = LC[X_i-1][Y_i];
    if(tmp < LC[X_i][Y_i-1]) tmp = LC[X_i][Y_i-1];
    LC[X_i][Y_i] = tmp;
//cout << LC[X_i][Y_i] << " ";
   }
//cout << endl;
  }

  return LC[Xsize-1][Ysize-1];
 };


protected:
};

int main(){
 long datasetNum = 0; cin >> datasetNum;
 for(unsigned long dataset_i = 0; dataset_i < datasetNum; ++dataset_i){
  Dataset data;
  cout << data.getLongestCommonLength() << endl;
 }

 return 0;
};