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++: /*
 * ALDS1_10_C.cpp
 *
 *  Created on: May 5, 2018
 *      Author: 13743
 */

#include<cstdio>
#include<cstring>
#include<algorithm>

int dp[1001][1001];
bool dpR[1001][1001];

int lcm(char* X, char* Y, int nx, int ny) {
	dp[1][1] = (X[0] == Y[0]);
	for(int i=1; i<=nx; i++) {
		for(int j=1; j<=ny; j++) {
			dp[i][j] = (X[i-1]==Y[j-1]) + dp[i-1][j-1];
			dp[i][j] = std::max(dp[i-1][j], dp[i][j]);
			dp[i][j] = std::max(dp[i][j-1], dp[i][j]);
		}
	}
	return dp[nx][ny];
}

int lcmRec(char* X, char* Y, int nx, int ny) {
	if(nx <=0 || ny <= 0) return 0;
	if(!dpR[nx][ny]) {
		dpR[nx][ny] = 1;
		dp[nx][ny] = lcmRec(X, Y, nx-1, ny-1) + (X[nx-1]==Y[ny-1]);
		dp[nx][ny] = std::max(lcmRec(X, Y, nx-1, ny), dp[nx][ny]);
		dp[nx][ny] = std::max(lcmRec(X, Y, nx, ny-1), dp[nx][ny]);
	}
	return dp[nx][ny];
}

int main() {
	int q;
	scanf("%d", &q);
	char X[1001];
	char Y[1001];
	while(q--) {
		for(int i=0; i<1001; i++) {
			for(int j=0; j<1001; j++) {
				dpR[i][j] = dp[i][j] = 0;
			}
		}
		scanf("%s", X);
		scanf("%s", Y);
		int nx = (int)strlen(X);
		int ny = (int)strlen(Y);
		//printf("%d\n", lcm(X, Y, nx, ny));
		printf("%d\n", lcmRec(X, Y, nx, ny));
	}
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <sstream>
#include <utility>
#define ll long long int
#define rep(i,x,y) for(int i=x;i<y;i++)
#define rel(i,x,y) for(int i=x-1;i>=y;i--)
#define all(x) x.begin(),x.end()
#define mod 1000000007
using namespace std;

int main(){
	int q;
	cin >> q;
	string X[q],Y[q];
	rep(i,0,q){
		cin >> X[i];
		cin >> Y[i];
	}

	rep(i,0,q){
		int m = X[i].length();
		int n = Y[i].length();
		int dp[m+1][n+1] = {};
		int maxl=0;
		rep(j,0,m){
			rep(k,0,n){
				if(X[i][j] == Y[i][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[m][n] << 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 i = 1; i <= n; i++) c[0][i] = 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 n;
	string s1, s2;
	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<algorithm>
using namespace std;
static const int N = 1001;

int lcs(string X, string Y){
    int c[N][N];
    int m = X.size();
    int n = Y.size();
    int maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;
    for(int i = 0; i <= m; i++) c[i][0] = 0;
    for(int j = 0; 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;
    cin >> q;
    while(q--){
        string a,b; cin >> a >> b;
        cout << lcs(a,b) << endl;
    }
}

C++: #include <iostream>

using namespace std;

typedef long long signed int ll;

constexpr ll NIL = -20000000000;

string s[2];
int dp[1001][1001];

int lcs(int x,int y) {
    if(x < 0 || y < 0) return 0;
    if(x >= s[0].size() || y >= s[1].size()) return 0;
    if(dp[x][y] == -1) {
        dp[x][y] = max(lcs(x+1,y),dp[x][y]);
        dp[x][y] = max(lcs(x,y+1),dp[x][y]);
        if(s[0][x] == s[1][y]) dp[x][y] = max(lcs(x+1,y+1) + 1,dp[x][y]);
    }
    return dp[x][y];
}

int main() {
    int n;
    cin >> n;
    for(int i = 0;i < n;i++) {
        cin >> s[0] >> s[1];
        int res = -1;
        for(int j = 0;j < 1001 * 1001;j++)(reinterpret_cast<int*>(dp))[j] = -1;
        for(int j = 0;j < 1001 * 1001;j++)res = max(res,(reinterpret_cast<int*>(dp))[j]);
        cout << lcs(0,0) << endl;
    }
}

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

#define MAX 1000

int DP[MAX][MAX];

int main(){
  int n;
  
  std::cin >> n;
  
  for(int t = 0; t < n; t++){
    std::string str1, str2;

    std::cin >> str1;
    std::cin >> str2;
    
    const int one_len = str1.length();
    const int two_len = str2.length();
    
    for(int i = 0; i < one_len; i++){
      for(int j = 0; j < two_len; j++){
	DP[i][j] = 0;
      }
    }

    for(int i = 0; i < one_len; i++){
      for(int j = 0; j < two_len; j++){
	if(i == 0 && j == 0){
	  DP[i][j] = str1[i] == str2[j] ? 1 : 0;
	}else if(i == 0){	
	  DP[i][j] = std::max(DP[i][j-1], str1[i] == str2[j] ? 1 : 0);
	}else if(j == 0){
	  DP[i][j] = std::max(DP[i-1][j], str1[i] == str2[j] ? 1 : 0);
	}else{
	  DP[i][j] = std::max(DP[i-1][j-1] + (str1[i] == str2[j] ? 1 : 0), std::max(DP[i-1][j], DP[i][j-1]));
	}
      }
    }
    
    std::cout << DP[one_len - 1][two_len - 1] << std::endl;
  }
}

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; // X[0]に空白を挿入
    Y = ' ' + Y;
    for (int i=0; i<=m; i++) C[i][0] = 0;
    for (int j=0; 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][j-1], C[i-1][j]);
            }
            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 <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin >> n;
    for(int q=0;q<n;q++){
        string x,y;
        cin >>x >> y;
        int dp[1002][1002];
        memset(dp,-1, sizeof(dp));
        dp[0][0] = 0;
        for(int i=0;i<x.size()+1;i++){
            for(int j=0;j<y.size()+1;j++){
                if (i < x.size() && j < y.size() && x[i] == y[j]){
                dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + 1);
                }
                dp[i][j+1] = max(dp[i][j+1],dp[i][j]);
                dp[i+1][j] = max(dp[i+1][j],dp[i][j]);
            }
        }
        cout << dp[x.size()][y.size()] << endl;
    }
}