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 <string>
#include <vector>
#include <queue>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <chrono>
#define lint long long int
#define FOR(x, to) for(int x=0; x<(int)(to); x++) // for簡略表記
#define DUMP(ar) for(int loop=0; loop<(int)ar.size(); loop++) cout << "[" << loop << "]:" << ar[loop] << endl // 配列一覧表示
#define DUMPL(ar) for(int loop=0; loop<(int)ar.size(); loop++) { cout << setw(2) << ar[loop]; if(loop<(int)ar.size()-1) cout << ' '; } cout << endl; // 配列一行一覧表示
#define COUT(x) cout << x << endl // 単要素出力
#define ALL(ar) ar.begin(), ar.end() // コンテナの全要素
#define LOOPD(i) cout << "ループ[" << i << "]回目---------------" << endl // ループ回数デバグ
using namespace std;
// cout << fixed << setprecision(4) で小数点以下4桁の桁そろえ
// cout << setw(5) << ... で表示形式を5桁に設定 5桁未満なら頭に空白がつく 5桁越なら先頭から5桁まで
// cout << setw(5) << setfill('0') << ... で空白部分の埋め文字に5を使う
// getline(cin, [変数]); で空白など(ホワイトスペース)を含む入力
// cin >> ws; でそこまでに入力ストリームに残されたホワイトスペースを取り除く(=以降のgetlineの有効化)

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

    vector<int> ans(q);
    for(int i=0; i<q; i++){
        int s1_size = s1[i].size();
        int s2_size = s2[i].size();
        
        vector<vector<int>> dp(s1_size, vector<int>(s2_size, 0));
        // 初期化
        bool flag_same = false;
        for(int j=0; j<s1_size; j++){
            if(s1[i][j] == s2[i][0] && !flag_same){
                flag_same = true;
            }
            if(flag_same) dp[j][0] = 1;
            else dp[j][0] = 0;
        }
        flag_same = false;
        for(int j=0; j<s2_size; j++){
            if(s2[i][j] == s1[i][0] && !flag_same){
                flag_same = true;
            }
            if(flag_same) dp[0][j] = 1;
            else dp[0][j] = 0;
        }

        // 計算
        for(int j=1; j<s1_size; j++){
            for(int k=1; k<s2_size; k++){
                if(s1[i][j] == s2[i][k]){
                    dp[j][k] = min(dp[j-1][k-1], min(dp[j-1][k], dp[j][k-1])) + 1;
                }else{
                    dp[j][k] = max(dp[j-1][k], dp[j][k-1]);
                }
            }
        }
        // LOOPD(i);
        // for(int j=0; j<s1_size; j++){
        //     DUMPL(dp[j]);
        // }
        // cout << "   ";
        // for(int j=0; j<s2_size; j++){
        //     cout << setw(2) << s2[i][j] << ' ';
        // }
        // cout << endl;

        // for(int j=0; j<s1_size; j++){
        //     cout << setw(2) << s1[i][j] << ' ';
        //     for(int k=0; k<s2_size; k++){
        //         cout << setw(2) << dp[j][k] << ' ';
        //     }
        //     cout << endl;
        // }

        ans[i] = dp.back().back();
    }
    
    // DUMP(ans);
    for(int i=0; i<q; i++) cout << ans[i] << endl;
    return 0;
}

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

class TLcsSolver
{
    typedef std::vector<std::vector<int> > TTable;
    TTable FTable;
    std::string FArray1;
    std::string FArray2;

    int Count(int i, int j)
    {
        if(i == 0 || j == 0) return 0;
        if(FArray1[i-1] == FArray2[j-1]){
            return FTable[i-1][j-1] + 1;
        }
        return std::max(FTable[i-1][j], FTable[i][j-1]);
    }
    void Solve()
    {
        for(std::size_t i = 1; i < FTable.size(); ++i){
            for(std::size_t j = 1; j < FTable[i].size(); ++j){
                FTable[i][j] = Count(i, j);
            }
        }
    }
public:
    TLcsSolver(const std::string& a, const std::string& b)
     : FArray1(a), FArray2(b)
     , FTable(a.size()+1, std::vector<int>(b.size()+1, 0))
    {
        Solve();
    }
    int Answer()const
    {
        return FTable[FArray1.size()][FArray2.size()];
    }
};

int main()
{
    int n;
    std::cin >> n;
    char a[1000];
    char b[1000];
    for(int i = 0; i < n; ++i){
        std::gets(a);
        std::gets(b);
        std::printf("%d", TLcsSolver(a, b).Answer());
    }
    return 0;
}

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

using namespace std;

string str1,str2;
int ans;

void lcs(string str1,string str2){

  int a = str1.size();
  int b = str2.size();
  int cou[1000][1000] ={{}};
  int i,j;


  for(i = 0;i < a;i++){
    for(j = 0;j < b;j++){
      
      // cout << str1[i] << ' ' << str2[j] << endl;


      if(str1[i] == str2[j]){
	cou[i][j] = cou[i-1][j-1] + 1;
      }
      else if(cou[i - 1][j] > cou[i][j - 1]){
	cou[i][j] = cou[i - 1][j];
      }
      else  cou[i][j] = cou[i][j - 1];

      // cout << "cou = " <<  cou[i][j] << endl;
      ans = cou[i][j];
    }
  }

  // return cou[i][j];

}

int main(){

  int n;
  cin >> n;
  for(int i2 = 0;i2 < n;i2++){

  cin >> str1 >> str2;

  lcs(str1,str2);

  cout << ans  << 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=0;i<=m;i++)c[i][0]=0;
  for(int j=1;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() {
  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 <stdio.h>
#include <string.h>

int cache[1001][1001];
const int INF = 1 << 20;

int max(int x, int y) { if (x < y) return y; else return x; }

int lcs(char *x, char *y, int xl, int yl) {
	if (cache[xl][yl] != INF) {
		return cache[xl][yl];
	}
	if (xl == 0 || yl == 0) return 0;
	if (x[xl-1] == y[yl-1]) {
		return cache[xl][yl] = lcs(x, y, xl - 1, yl - 1) + 1;
	} else {
		return cache[xl][yl] = max(lcs(x, y, xl - 1, yl), lcs(x, y, xl, yl - 1));
	}
}

int main(void) {
	int q;
	scanf("%d\n", &q);
	for (int i = 0; i < q; i ++) {
		char X[1000];
		char Y[1000];
		for (int i = 0; i <= 1000; i ++) {
			for (int j = 0; j <= 1000; j ++) {
				cache[i][j] = INF;
			}
		}
		gets(X);
		gets(Y);
		printf("%d\n", lcs(X, Y, strlen(X), strlen(Y)));
	}
	return 0;
}