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 <stdio.h>
#include <iostream>
#include <algorithm>
??
using namespace std;
??
short checkTable[1001][1001];
??
int tmp_max=0;
??
??
void calc(int index,char pattern[],char to_search[],int to_search_length){
??
????????for(int i=to_search_length-1;i>=0;i--){
????????????????if(to_search[i]==pattern[index]){
????????????????????????checkTable[index][i]=1;
????????????????????????if(pattern[index+1]!='\0' && i < to_search_length-1){
????????????????????????????????checkTable[index][i] = checkTable[index+1][i+1] + checkTable[index][i];
????????????????????????}
????????????????}else{
????????????????????????if(i != to_search_length-1){
????????????????????????????????if(pattern[index+1] != '\0'){
????????????????????????????????????????checkTable[index][i]=(checkTable[index+1][i]>checkTable[index][i+1])
?????????????????????????????????????????????????????????checkTable[index+1][i]:checkTable[index][i+1];
??
????????????????????????????????}else{
????????????????????????????????????????checkTable[index][i]=(checkTable[index][i+1]>0)?checkTable[index][i+1]:0;
????????????????????????????????}
????????????????????????}else{
????????????????????????????????if(pattern[index+1] != '\0'){
????????????????????????????????????????checkTable[index][i]=(checkTable[index+1][i]>0)?checkTable[index+1][i]:0;
????????????????????????????????}else{
????????????????????????????????????????checkTable[index][i] = 0;
????????????????????????????????}
????????????????????????}
????????????????}
????????????????tmp_max = (tmp_max >= checkTable[index][i])?tmp_max:checkTable[index][i];
????????}
}
??
int main(){
??
????????int q,left_length,right_length;
????????char left[1001],right[1001];
??
????????scanf("%d",&q);
??
????????for(int i=0;i<q;i++){
????????????????left_length=0;
????????????????right_length=0;
????????????????scanf("%s %s",left,right);
????????????????for(int k=0;left[k]!='\0';k++)left_length++;
????????????????for(int k=0;right[k]!='\0';k++)right_length++;
??
????????????????if(right_length>=left_length){
??
????????????????????????for(int k=left_length-1;k>=0;k--)calc(k,left,right,right_length);
????????????????}else{
??
????????????????????????for(int k=right_length-1;k>=0;k--)calc(k,right,left,left_length);
????????????????}
????????????????printf("%d\n",tmp_max);
????????????????tmp_max=0;
????????}
??
????????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 res = 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]);

			}
			res = max(res, c[i][j]);
		}
	}
	return res;
}

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 <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

string s1, s2;

int lcs(int n1, int n2) {
  if (n1 < 0 || n2 < 0) return 0;
  
  if (s1[n1] == s2[n2]) return 1 + lcs(n1 - 1, n2 - 1);
  else return max(lcs(n1, n2 - 1), lcs(n1 - 1, n2));
}

int main() {
	cin.tie(0);
 	ios::sync_with_stdio(false);

  int q; cin >> q;
  REP(i, q) {
    cin >> s1;
    cin >> s2;

    cout << lcs(s1.length() - 1, s2.length() - 1) << endl;
  }
}