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 <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

int dp[1001][1001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  vector<string> xs(q), ys(q);
  for (int i = 0; i < q; i++) {
    cin >> xs[i] >> ys[i];
  }

  for (int i = 0; i < q; i++) {
    fill(dp[0], dp[1001], 0);
    string x = xs[i], y = ys[i];
    for (int j = 0; j < x.size(); j++) {
      for (int k = 0; k < y.size(); k++) {
        if (x[j] == y[k]) {
          dp[j + 1][k + 1] = dp[j][k] + 1;
        } else {
          dp[j + 1][k + 1] = max(dp[j][k + 1], dp[j + 1][k]);
        }
      }
    }

    cout << dp[x.size()][y.size()] << endl;
    ;
  }
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <queue>
#include <cmath>
#include <set>
#include <iomanip>

using namespace std;

typedef long long ll;

int get_LCS(string x, string y){
  int nx = x.size(), ny = y.size();
  int dp[nx+1][ny+1];
  for(int i=0;i<=ny;i++) dp[0][i] = 0;
  for(int i=0;i<=nx;i++) dp[i][0] = 0;
  for(int i=1;i<=nx;i++){
    for(int j=1;j<=ny;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]);
    }
  }
  return dp[nx][ny];
}

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int q; cin >> q;
  for(int i=0;i<q;i++){
    string x, y; cin >> x >> y;
    cout << get_LCS(x, y) << endl;
  }
}

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

int lcs(string s1,string s2)
{
	int m = s1.size();
	int n = s2.size();
	int c[1005][1005];
	for (int i = 1; i <= m; i++)c[i][0] = 0;
	for (int j = 1; j <= n; j++)c[0][j] = 0;
	int maxz = 0;
	s1 = " " + s1;
	s2 = " " + s2;
	for(int i=1;i<=m;i++)
		for (int j = 1; j <= n; 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]);
			}
			maxz = max(maxz, c[i][j]);
		}
	return maxz;
}
int main()
{
	int N;
	scanf("%d", &N);
	while (N--) {
		string s1, s2;
		cin >> s1>>s2;
		cout << lcs(s1, s2) << endl;
	}
	return 0;
}

C++: /*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C&lang=jp
*/
#include<iostream>
#include<string>
using namespace std;

//最長共通部分列
int LCS(string x , string y){
  int x_len=x.size();
  int y_len=y.size();
  int dp[x_len+1][y_len+1];
  //初期化
  for(int i=0;i<=x_len;i++){
    for(int j=0;j<=y_len;j++){
      dp[i][j]=0;
    }
  }
  for(int i=0;i<x_len;i++){
    for(int j=0;j<y_len;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]);
      }
    }
  }
  return dp[x_len][y_len];
}
int main(){
  int q;
  cin>>q;
  for(int i=0;i<q;i++){
    string str1,str2;
    cin>>str1>>str2;
    cout<<LCS(str1,str2)<<"\n";
  }
}

C++: # include<iostream>
# include<string>
# include<algorithm>
using namespace std;
# define N 300
# define M 1000
# define L 1001

int main(){
    int q;
    cin >> q;
    string iranai; iranai = cin.get(); //改行文字を排除

    string A[N][M];
    int cnt[N];
    for(int i=0; i<(q*2); i++){
        int j=0;
        while((A[i][j] = cin.get()) != "\n"){
            j++;
            cnt[i]++;
        }
    }

    for(int k=0; k<(q*2); k+=2){
        int B[L][L];
        for(int i=0; i<=cnt[k]; i++){
            string str;
            if(i!=0) str = A[k][i-1];
            for(int j=0; j<=cnt[k+1]; j++){
                if(i==0 || j==0) B[i][j] = 0;
                else if(A[k+1][j-1] != str) B[i][j] = max(B[i-1][j], B[i][j-1]);
                else B[i][j] = B[i][j] = B[i-1][j-1] + 1;
            }
        }
        cout << B[cnt[k]][cnt[k+1]] << "\n";
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

typedef long long ll;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const int INF = 1e9;
const long long INFL = 1LL<<60;

int dp[10005][10005];

int main() {
  int n;
  cin >> n;
  rep(k, n) {
    string a, b;
    cin >> a >> b;
    int m = a.size();
    int n = b.size();
    rep(i, 10005) {
      dp[0][i] = 0;
      dp[i][0] = 0;
    }
    for (int i = 1; i < n + 1; i++) {
      for (int j = 1; j < m + 1; j++) {
        if (b[i-1] == a[j-1]) {
          dp[i][j] = max(max(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1] + 1);
        } else {
          dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
      }
    }
    cout << dp[n][m] << endl;
  }

  return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<iostream>
#define MAX(a,b) (a>b?a:b)
using namespace std;
const int MAXN=1010;
int dp[MAXN][MAXN];
char a[MAXN],b[MAXN];
int main()
{
    int n;
    cin>>n;
    for(int k=0; k<n; k++)
    {
        while(scanf("%s%s",a+1,b+1))
        {
            memset(dp,0,sizeof(dp));
            int i,j;
            for( i=1; a[i]; i++)
            {
                for(j=1; b[j]; j++)
                {
                    if(a[i]==b[j])dp[i][j]=dp[i-1][j-1]+1;
                    else dp[i][j]=MAX(dp[i][j-1],dp[i-1][j]);
                }
            }
            printf("%d\n",dp[i-1][j-1]);
        }
    }
    return 0;
}