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 <cstring> 
using namespace std;
int c[1001][1001];

int lcs(string x, string y)
{
    int maxl = 0;
    for (int i = 0; i < x.size(); i++)
    {
        c[i][0] = 0;
    } 
    for (int i = 0; i < y.size(); i++)
    {
        c[0][i] = 0; 
    }
    for (int i = 1; i <= x.size(); i++)
    {
        for (int j = 1; j <= y.size(); j++)
        {
            if (x[i]==y[j])
            { 
                c[i][j] = c[i][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 x,y;
    int q;
    cin>>q;
    while(q--)
    {
        cin>>x>>y;
        int ans = lcs(x, y);
        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 = 1; 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<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0;i < n;i++)
#define repr(i, n) for(int i = n;i >= 0;i--)
#define frep(i, m, n) for(int i = m;i < n;i++)
#define frepr(i, m, n) for(int i = m;i >= n;i--)
#define vsort(v) sort(v.begin(),v.end())
#define ll long long
#define inf 999999999
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<string> vst;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

int main(){
    int q;
    cin >> q;
    rep(p,q){
       string x,y;
       cin >> x >> y;
       vvi dp(x.length(),vi(y.length(),inf));
       rep(i,x.length()){
           rep(j,y.length()){
               if(i==0&&j==0)dp[i][j]=(x[i]==y[j]);
               else if(i==0)dp[i][j]=max(dp[i][j-1],(int)(x[i]==y[j]));
               else if(j==0)dp[i][j]=max((int)(x[i]==y[j]),dp[i-1][j]);
               else if(x[i]==y[j])dp[i][j]=1+dp[i-1][j-1];
               else dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
            //    cout << dp[i][j];
           }
        //    cout << endl;
       }
       cout << dp[x.length()-1][y.length()-1] << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  while(n--){
  string  a,b;
  long n=0,i,c=0;
  cin>>a>>b;
  for(i=0;i<a.size();i++)if(a[i]==b[c])if(++n)c++;
  cout<<n<<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;
  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[i]) {
        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 <iostream>
#include <algorithm>
#include <string>
using namespace std;
#define MAXN 1000

int lcs(string s1, string s2)
{
	int m = s1.size();
	int n = s2.size();
	int C[MAXN + 1][MAXN + 1];
	C[0][0] = 0;
	s1 = ' ' + s1;
	s2 = ' ' + s2;
	int max_lcs = 0;
	for (int i = 1; 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 (s1[i] == s2[j]) C[i][j] = C[i - 1][j - 1] + 1;
			else C[i][j] = max(C[i - 1][j], C[i][j - 1]);
			max_lcs = max(max_lcs, C[i][j]);
		}
	}
	return max_lcs;
}

int main()
{
	string s1, s2;
	int q;
	cin >> q;
	while (q--)
	{
		cin >> s1 >> s2;
		cout << lcs(s1, s2) << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int LCS[1000][1000];

void solve(string x, string y)
{
	static const int offset[3][2] = { {0,0},{1,0},{0,1} };
	for (int i = 1; i != (max(x.size(), y.size())) + 1; i++)
	{
		for (int k = 0; k < 3; k++)
		{
			int xi = min(x.size() - 1, i - 1 + offset[k][0]);
			int yi = min(y.size() - 1, i - 1 + offset[k][1]);
			if (x[xi] == y[yi])
			{
				LCS[xi + 1][yi + 1] = LCS[xi][yi] + 1;
			}
			else
			{
				LCS[xi + 1][yi + 1] = max(LCS[xi][yi + 1], LCS[xi + 1][yi]);
			}
		}
	}

	cout << LCS[x.size()][y.size()] << endl;
}

int main()
{
	int q;
	cin >> q;

	for (int i = 0; i < 1000; i++)
	{
		LCS[0][i] = 0;
		LCS[i][0] = 0;
	}

	for (int i = 0; i < q; i++)
	{
		string x, y;
		cin >> x >> y;
		solve(x, y);
	}
	return 0;
}