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

int main(){
    int n;cin>>n;
    for(int i=0;i<n;i++){
        string X,Y;cin>>X>>Y;
        int M[X.size()][Y.size()]={};
        //まずは端を埋める
        int flg=0;
        for(int i=0;i<Y.size();i++){//Xの一文字目を固定
            if(X[0]==Y[i])flg=1;
            if(flg)M[0][i]=1;
        }
        flg=0;
        for(int i=0;i<X.size();i++){//Yの一文字目を固定
            if(Y[0]==X[i])flg=1;
            if(flg)M[i][0]=1;
        }
        for(int i=1;i<X.size();i++){
            for(int j=1;j<Y.size();j++){
                if(X[i]==Y[j]){
                    M[i][j]=M[i-1][j-1]+1;
                }else{
                    M[i][j]=max(M[i-1][j],M[i][j-1]);
                }
            }
        }

        cout<<M[X.size()-1][Y.size()-1]<<endl;
    }
    return 0;
}

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

static const int N = 1000;

int longestCommonSubsequence(string X, string Y)  {
  int c[N + 1][N + 1];
  int m = X.size();
  int n = Y.size();
  int max1 = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  for (int i = 0; i <= m; i++) 
    c[i][0] = 0;
  for (int j = 1; j <= n; j++)
    s[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]);
      }
      max1 = max(max1, c[i][j]);
    }
  }
  return max1;
}
 
int main()  {
  string s1, s2;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)  {
    cin >> s1 >> s2;
  cout << longestCommonSubsequence(s1, s2) << endl;
  }
  return ;
}

C++: #include <bits/stdc++.h>
 
#define ALL(l)  (l).begin(),(l).end()
#define rep(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;
typedef long long ll;
//const ll mod =  1000000007;
const ll maxn = 1000000000;

//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // intの1次元の型に vi という別名をつける
using vvll = vector<vll>; // intの2次元の型に vvi という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll = pair<ll, ll>; // これ以降 pii という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------

int main(){
  int n;
  cin>>n;
  rep(i,n){
      string s,t;
      cin>>s>>t;
      
      vvll dp(s.size()+1,vll(t.size()+1));
      rep(j,s.size()+1)dp.at(j).at(0)=0;
      rep(j,t.size()+1)dp.at(0).at(j)=0;
      
      rep(j,s.size()){
          rep(k,t.size()){
              if(s.at(j)==t.at(k))dp.at(j+1).at(k+1)=dp.at(j).at(k)+1;
              else dp.at(j+1).at(k+1)=max(dp.at(j+1).at(k),dp.at(j).at(k+1));
          }
      }
      cout<<dp.at(s.size()).at(t.size())<<endl;
  }
}

C++: def lcs(x, y)
  for i in range(1, len(x)):
    for j in range(1, lem(y)):
      if x[i] = y[j]:
        c[i][j] = c[i-1][j-1] + 1
      elif c[i-1][j]>c[i][j-1]:
        c[i][j] = c[i-1][j]
      else:
        c[i][j] = c[i][j-1]






q  = input()
x = [' ' for row in range(n+1)]
y = [' ' for row in range(n+1)]
c = [[' ' for col in range(1000)] for row in range(1000)]
for i in range(q):
  x = input()
  y = input()
  print lcs(x, y)

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}

typedef long long ll;

int main()
{
	int n, dp[1024][1024];
	string a, b;
	cin >> n;
	
	while(n)
	{
		n--;
		memset(dp, 0, sizeof(dp));

		cin >> a;
		cin >> b;

		for(int i = 1; i <= a.size(); i++)
		{
			for(int j = 1; j <= b.size(); j++)
			{
				if(a[i+1] == b[j+1])
				{
					dp[i+1][j+1] = max(dp[i][j] + 1, max(dp[i + 1][j], dp[i][j + 1]));
				}
				else
				{
					dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
				}
			}
		}

		cout << dp[a.size()+1][b.size()+1] << endl;
	}
}

C++: #include<iostream>
#include<string>
int main()
{
  int n;
  int an[1000];
  int l=0;
  std::cin>>n;
  std::string x;
  std::string y;
  int ans[1001][1001];
  int j,k;
  for(int i=0;i<n;i++){
    std::cin>>x;
    std::cin>>y;
    for(int j=0;j<=1000;j++){
      for(int k=0;k<=1000;k++){
	ans[j][k]=0;
      }
    }
    for(int j=0;j<x.size();j++){
      for(int k=0;k<y.size();k++){
	if(x[j]==y[k]){
	  ans[j][k]=ans[j-1][k-1]+1;
	}else{
	  if(ans[j][k-1]>ans[j-1][k]){
	    ans[j][k]=ans[j][k-1];
	  }else{
	    ans[j][k]=ans[j-1][k];
	  }
	}
      }
    }
    std::cout<<ans[x.size()-1][y.size()-1]<<'\n';  
  }
  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <list>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

const int INF = 1 << 30;
const int MOD = 1000000007;
const int MAX = 1000;
const int N = 150;

int dp[MAX+1][MAX+1];

int lcs(string X, string Y) {
	rep(i, X.size()+1) { rep(j, Y.size()+1) {
		if (i == 0 || j == 0)
			dp[i][j] = 0;
		else if (X[i-1] == Y[j-1])
			dp[i][j] = dp[i-1][j-1] + 1;
		else
			dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
	} }
	return dp[X.size()][Y.size()];
}

int main() {
	int q; cin >> q;
	string X, Y;
	rep(i, q) {
		cin >> X >> Y;
		cout << lcs(X, Y) << endl;
	}
}