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++: /* test */
#include <bits/stdc++.h>
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 k=1;k<=n;k++) c[0][k] = 0;
 
    for(int i=1;i<=m;i++){
        for(int k=1;k<=n;k++){
            if(X[i]==Y[k]){
                c[i][k] = c[i-1][k-1] + 1;
            } else {
                c[i][k] = max(c[i-1][k],c[i][k-1]);
            }
            maxl = max(maxl,c[i][k]);
        }
    }
 
    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<string>
#include<algorithm>
usig 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 <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
	int q;	cin>>q;
	string a,b;
	while(q--){
		cin>>a;
		cin>>b;
		int data[a.size()+1]={};
		for(int i=0;i<b.size();i++){
			for(int j=0;j<a.size();j++){
				if(a[j]==b[i]){
					for(int k=j;k>=0;k--){
						data[j+1]=max(data[j+1],data[k]+1);
					}
				}
			}
		}
		int ans=0;
		for(int i=1;i<=a.size();i++){
			ans=max(ans,data[i]);
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
struct node{
  int value;
  node *next;
  node(int value,node *next):value(value),next(next){}
};
int INF=1LL<<55LL;
string lcs(string x,string y){
  int n=x.size(),m=y.size();
  map<char,vector<int> > M;
  for(int j=m-1;j>=0;j--) M[y[j]].push_back(j);
  vector<int> xs(n+1,INF);
  xs[0]=-INF;
  vector<node* > link(n+1);
  for(int i=0;i<n;i++){
    if(M.count(x[i])){
      vector<int> ys=M[x[i]];
      for(int j=0;j<(int)ys.size();j++){
	int k=distance(xs.begin(),lower_bound(xs.begin(),xs.end(),ys[j]));
	xs[k]=ys[j];
	link[k]=new node(y[ys[j]],link[k-1]);
      }
    }
  }
  string res;
  int l=distance(xs.begin(),lower_bound(xs.begin(),xs.end(),INF-1))-1;
  for(node *p=link[l];p!=NULL;p=p->next)
    res.push_back(p->value);
  return res;
}
signed main(){
  int q;
  cin>>q;
  while(q--){
    string x,y;
    cin>>x>>y;
    cout<<lcs(x,y).size()<<endl;
  }
  return 0;
}

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

class TLongistCommons
{
	std::vector<std::vector<int> > FTable;
	const std::string& FSequenceX;
	const std::string& FSequenceY;
	
	int MeasureCommons(int PrefixX, int PrefixY);
public:
	TLongistCommons(const std::string& SequenceX, const std::string& SequenceY);
	
	int Length();
};

TLongistCommons::TLongistCommons(const std::string& SequenceX, const std::string& SequenceY)
: FSequenceX(SequenceX), FSequenceY(SequenceY), FTable(SequenceX.length() + 1, std::vector<int>(SequenceY.length() + 1, -1))
{
}

int TLongistCommons::MeasureCommons(int PrefixX, int PrefixY)
{
	if(FTable[PrefixX][PrefixY] < 0){
		int Longest;
		if(PrefixX == 0 || PrefixY == 0){
			Longest = 0;
		}else if(FSequenceX[PrefixX - 1] == FSequenceY[PrefixY - 1]){
			Longest = MeasureCommons(PrefixX - 1, PrefixY - 1) + 1;
		}else{
			Longest = std::max(MeasureCommons(PrefixX - 1, PrefixY), MeasureCommons(PrefixX, PrefixY - 1));
		}
		FTable[PrefixX][PrefixY] = Longest;
	}
	return FTable[PrefixX][PrefixY];
}

int TLongistCommons::Length()
{
	return MeasureCommons(FSequenceX.length(), FSequenceY.length());
}

int MeasureLcs(const std::string& SequenceX, const std::string& SequenceY)
{
	TLongistCommons Subsequence(SequenceX, SequenceY);
	return Subsequence.Length();
}

int main()
{
	int Count;
	std::cin >> Count;
	for(int i = 0; i < Count; ++i){
		std::string SequenceX, SequenceY;
		std::cin >> SequenceX;
		std::cin >> SequenceY;
		std::cout << MeasureLcs(SequenceX, SequenceY) << std::endl;
	}
	return 0;
}

C++: #include "iostream"
#include"string"
#include"algorithm"
using namespace std;
int c[1001][1001];
int lcs(string X,string Y) {
	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 = 0; 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<iomanip>
#include<limits>
#include<thread>
#include<utility>
#include<iostream>
#include<string>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<numeric>
#include<cassert>
#include<random>
#include<chrono>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pi;
#define F first
#define S second
#define MK make_pair
const ll E=1e17+7;
const ll MOD=1000000007;






int main(){
    ll q;
    cin>>q;
    while(q--){
        
        
    string a,b;
    cin>>a>>b;
    
    vector<set<ll>> w(300);
    vector<vector<ll>> dp(2000);
    for(int i=0;i<dp.size();i++){dp[i].resize(2000,E);}
    
    for(int i=0;i<b.size();i++){
        w[b[i]].insert(i);
    }
    
    ll mx=0;
    
    for(int i=0;i<a.size();i++){
        for(int t=0;t<b.size()+100;t++){
            if(t==0 && !w[a[i]].empty() && *w[a[i]].begin()<dp[i][t]){
                dp[i+1][t]=*w[a[i]].begin();
                mx=max(mx,(ll)t+1);
            }
            else if(w[a[i]].upper_bound(dp[i][t-1])!=w[a[i]].end() && *w[a[i]].upper_bound(dp[i][t-1])<dp[i][t]){
                dp[i+1][t]=*w[a[i]].upper_bound(dp[i][t-1]);
                mx=max(mx,(ll)t+1);
            }
            else{dp[i+1][t]=dp[i][t];}
        }
    }
    cout<<mx<<endl;
        
        
    }
    
    return 0;
}