Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	for i := 0; i < n; i++ {
		x := getNextString(scanner)
		y := getNextString(scanner)

		fmt.Fprintln(writer, lcs(x, y))
	}

	writer.Flush()
}

func lcs(x, y string) int {
	lx := len(x)
	ly := len(y)
	dp := make([][]int, lx+1)
	for i := 0; i <= lx; i++ {
		dp[i] = make([]int, ly+1)
	}
	for i := 0; i < lx; i++ {
		for ii := 0; ii < ly; ii++ {
			if x[i] == y[ii] {
				dp[i+1][ii+1] = dp[i][ii] + 1
				continue
			}
			dp[i+1][ii+1] = dp[i][ii+1]
			if dp[i+1][ii] > dp[i+1][ii+1] {
				dp[i+1][ii+1] = dp[i+1][ii]
			}
		}
	}
	return dp[lx][ly]
}

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;
  }
}