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++: //
// Created by 李青坪 on 2018/10/29.
//

#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream>

#define MAXN 150
#define MAXLEN 1000
using namespace std;

int c[MAXLEN+1][MAXLEN+1];

int getSubsequence(string x, string y){
    int i = 0, j = 0;
    while (i <= x.length()){
        while (j <= y.length()){
            if (i == 0 || j ==0){
                c[i][j] = 0;
            } else if (x[i-1] == y[j-1]){
                c[i][j] = c[i-1][j-1] + 1;
            } else if (x[i-1] != y[j-1]){
                c[i][j] = max(c[i-1][j], c[i][j-1]);
            }
            ++j;
        }
        ++i;
        j = 0;
    }
    return c[x.length()][y.length()];
}

int main(){
    int n;
    scanf("%d", &n);
    string x, y;
    for (int i = 0; i < n; ++i) {
        cin >> x >> y;
        printf("%d\n", getSubsequence(x, y));
    }

}
/*
3
abcbdab
bdcaba
abc
abc
abc
bc
 */

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

string lcs(string &s, string &t) {
    auto chmax = [](int& a, int b) { if (a < b) { a = b; return 1; } return 0; };
    int n = s.size(), m = t.size();
    vector<vector<int>> dp(n + 1, vector<int>(m + 1));
    vector<vector<int>> prv(n + 1, vector<int>(m + 1)); // ↓: 0, →: 1, ↘︎: 2
    for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
        if (s[i] != t[j]) {
            if (chmax(dp[i + 1][j + 1], dp[i][j + 1])) prv[i + 1][j + 1] = 0;
            if (chmax(dp[i + 1][j + 1], dp[i + 1][j])) prv[i + 1][j + 1] = 1;
        } else {
            if (chmax(dp[i + 1][j + 1], dp[i][j] + 1)) prv[i + 1][j + 1] = 2;
        }
    }
    string res = "";
    while (n > 0 && m > 0) {
        if (prv[n][m] == 0) n--;
        if (prv[n][m] == 1) m--;
        if (prv[n][m] == 2) n--, m--, res += s[n];
    }
    reverse(res.begin(), res.end());
    return res;
}

int main() {
    int T; cin >> T;
    while (T--) {
        string s, t; cin >> s >> t;
        cout << lcs(s, t).size() << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstring>

using namespace std;

/**
 *  lcs(m,n) = 
 *  if x[m] == y[n] lcs[m-1,n-1]+1
 *  max(lcs[m-1,n],lcs[m,n-1])
 *
 */

string x, y;

const int N = 1000;

int c[N][N];

int lcs(int m, int n)
{
	if (m == -1 || n== -1) return 0;
	if (x[m] == y[n]) return lcs(m-1,n-1) + 1;
	int & cc = c[m][n];
	if (cc != -1) return cc;
	int xx = lcs(m-1,n);
	int yy = lcs(m,n-1);
	cc = xx > yy ? xx : yy;
	return cc;
}

int main()
{
	int n = 0;
	x.reserve(N);
	y.reserve(N);
	cin >> n; 
	for (int i=0;i<n;i++)
	{
		cin >> x;
		cin >> y;
		cout << lcs(x.size()-1, y.size()-1) << endl;
		x.clear();
		y.clear();
		memset(c, -1, sizeof(c));
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int dp[1001][1001];
int lcs(string x,string y) {
    int m = x.size(), n = y.size(),la = 0;
    x = " " + x;
    y = " " + y;
    for (int i = 0; i <= m;i++) dp[i][0] = 0;
    for (int i = 1; i <= n;i++) dp[0][i] = 0;
    for (int i = 1;i <= m;i++) {
        for (int j = 1;j <= n;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]);
            la = max(la, dp[i][j]);
        }
    }
    return la;
}
int main() {
    int q;string x, y;
    cin >> q;
    for (int i = 0; i < q;i++) {
        cin >> x >> y;
        cout << lcs(x, y) << endl;
    }

}

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

int main() {
  std::ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int N;
  cin>>N;
  string S1,S2;
  for (int k=0; k<N; k++) {
    cin>>S1>>S2;
    int m=S1.length();
    int n=S2.length();
    S1=' '+S1;
    S2=' '+S2;
    int lcs[1001][1001];
    for (int i=1; i<=m; i++) {
      lcs[i][0]=0;
    }
    for (int j=1; j<=n; j++) {
      lcs[0][j]=0;
    }
    int maxl=0;
    for (int i=1; i<=m; i++) {
      for (int j=1; j<=n; j++) {
        if (S1[i]==S2[j]) {
          lcs[i][j]=lcs[i-1][j-1]+1;
        }
        else {
          lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1]);
        }
        maxl=max(maxl,lcs[i][j]);
      }
    }
    cout<<maxl<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int N;


void solve(string s,string t){
  vector<vector<int >> dp(1001, vector<int>(1001));
  int S=s.size();
  int T=t.size();
  for(int i=0;i<S;i++){
    for(int j=0;j<T;j++){
      if(s[i]==t[j]) dp[i+1][j+1]=dp[i][j]+1;
      else dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
    }
  }
  cout<<dp[S][T]<<endl;
}

int main() {

cin>>N;

for(int i=0;i<N;i++){
  string s,t;
  cin>>s>>t;
  solve(s,t);
}

}

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

using namespace std;

// typedef
typedef long long ll;

// container util
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

// repetition
#define FOR(i,m,n) for(ll i = ((ll) m); i < ((ll) n); ++i)
#define REP(i,n) FOR(i,0,n)

int solve(const string& X, const string& Y) {
    int xlen = X.length()+1, ylen = Y.length()+1;
    int* dp = new int[xlen*ylen];
    for(int i = 0; i < xlen; i++) {
        for(int j = 0; j < ylen; j++) {
            if(i == 0 || j == 0) {
                dp[i*ylen+j] = 0;
                continue;
            }
            dp[i*ylen+j] = max(
                dp[(i-1)*ylen+(j-1)] + (X[i-1] == Y[j-1]? 1 : 0),
                max(dp[(i-1)*ylen+j], dp[i*ylen+(j-1)])
            );
        }
    }
    int ret = dp[xlen*ylen - 1];
    delete[] dp;
    return ret;
}

int main() {
    int q; cin >> q;
    REP(i, q) {
        string X, Y;
        cin >> X >> Y;
        cout << solve(X, Y) << endl;
    }
    return 0;
}

/*
Longest Common Subsequence
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C&lang=jp

dp[i][j] = X1...XiとY1...Yjの最長共通部分列
dp[i][j] = 
dp[i-1][j-1] + 1            if Xi == Yj
max(dp[i][j-1], dp[i-1][j], dp[i-1][j-1]) otherwise
*/