Go: package main

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

const max int = 1000 + 1
var A [][]int

func lcs(x, y string) int {
	x = " " + x
	y = " " + y
	for i:=0;i<len(x); i++ {
		for j:=0;j<len(y); j++ {
			A[i][j] = 0
		}
	}
	for i:=1; i<len(x); i++ {
		for j:=1; j<len(y); j++ {
			if x[i] == y[j] {
				A[i][j] = A[i-1][j-1] + 1
			} else if A[i-1][j] > A[i][j-1] {
				A[i][j] = A[i-1][j]
			} else {
				A[i][j] = A[i][j-1]
			}
		}
	}
	return A[len(x)-1][len(y)-1]
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num 
}

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


func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	A = make([][]int, max)
	for i := range A {
		A[i] = make([]int, max)
	}
	n := nextInt(scanner)
	for i:=0; i<n; i++ {
		x := nextStr(scanner)
		y := nextStr(scanner)
		fmt.Printf("%d\n", lcs(x, y))
	}
}

C++: #include<bits/stdc++.h>
using namespace std;

int Q;
string X,Y;
int N,M;
int dp[1001][1001];

int solve(int xd,int yd){
  if( dp[xd][yd] != -1 ) return dp[xd][yd];
  if( xd==N || yd==M ) return dp[xd][yd] = 0;
  int ret;
  if( X[xd] == Y[yd] )
    ret = solve( xd+1,yd+1 ) + 1;
  else
    ret = max( solve(xd+1,yd), solve(xd,yd+1) );

  return dp[xd][yd] = ret;
}

int main(){
  cin >> Q;
  for(int ttt=0;ttt<Q;ttt++){
    cin >>X >> Y;
    N = X.size(); M = Y.size();
    memset(dp,-1,sizeof(dp));
    cout << solve(0,0) << endl;
  }
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <queue>

using namespace std;

const char *X, *Y;
int X_length, Y_length;

int table_lcs[1001][1001];

int lcs(int x_begin, int y_begin) {
	int i, j, to;
	int max = 0;
	int v_lcs;
	bool alreadySeenTable[256];
	if (table_lcs[x_begin][y_begin] != -1) {
		return table_lcs[x_begin][y_begin];
	}
	
	for (i = 0; i < 256; i++) {
		alreadySeenTable[i] = false;
	}
	i = x_begin;
	to = Y_length-1;
	while(i <= X_length-1) {
		j = y_begin;
		if (alreadySeenTable[X[i]] == true) {
			i++;
			continue;
		}
		bool found = false;
		while (j <= to) {
			if (X[i] == Y[j]) {
				v_lcs = lcs(i + 1, j + 1) + 1;
				if (v_lcs > max) {
					max = v_lcs;
				}
				found = true;
				break;
			}
			else {
				j++;
			}
		}
		if (found == false) {
			alreadySeenTable[X[i]] = true;
			i++;
		}
		else {
			alreadySeenTable[X[i]] = true;
			i++;
			to = j - 1;
		}
	}
	return table_lcs[x_begin][y_begin] = max;
}

int main() {

	int N;
	cin >> N;
	string stringX, stringY;

	for (int i = 1; i <= N; i++) {
		for (int i = 0; i < 1001; i++) {
			for (int j = 0; j < 1001; j++) {
				table_lcs[i][j] = -1;
			}
		}
		cin >> stringX >> stringY;
		X = stringX.c_str();
		X_length = stringX.size();
		Y = stringY.c_str();
		Y_length = stringY.size();
		printf("%d\n", lcs(0, 0));
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

#define int long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define dbg3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define dbgB(value, size) cerr<<#value<<": "<<bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }

template<typename T>
void printA(vector<T> &printArray, char between = ' ') {
   int paSize = printArray.size();
   for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between;}
   if (between != '\n') {cerr << endl;}
}

// ------------------------------------------------------------------------------------------

int dp[11111][11111];

void print(string s, string t) {
   int n = s.size(), m = t.size();
   rep (i, n+1) {
      rep (j, m+1) {
         printf("%2lld ", dp[i][j]);
      }
      puts("");
   }
}

int dist(string s, string t) {
   fill(dp[0], dp[1010], 1 << 28);
   dp[0][0] = 0;
   dp[0][0] = 0;
   int N = s.size(), M = t.size();
   rep (i, N+1) dp[i][0] = i;
   rep (i, M+1) dp[0][i] = i;

   rep (i, N) {
      rep (j, M) {
         dp[i+1][j+1] = min({dp[i][j+1]+2, dp[i+1][j]+2, dp[i][j] + (s[i] != t[j])});
      }
   }

   cout << dp[N][M] << endl;
}

int lcs(string s, string t) {
   for (int i = 0; i < s.size(); i++) {
      for (int j = 0; j < t.size(); j++) {
         dp[i+1][j+1] = (s[i] == t[j] ? dp[i][j]+1 : max(dp[i][j+1], dp[i+1][j]));
      }
   }
   return dp[s.size()][t.size()];
}

signed main() {
   int n;
   cin >> n;

   while (n--) {
      string s, t;
      cin >> s >> t;

      cout << lcs(s, t) << endl;
   }

   return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;

int lcs (string x, string y) {
	int m = x.length();
	int n = y.length();
	int maxl = 0;
	vector<vector<int>> c(m + 1, vector<int>(n + 1, 0));

	x = ' ' + x;
	y = ' ' + y;
	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() {
	int q; cin >> q;
	for(int cnt = 0; cnt < q; cnt++){
		string x, y;
		cin >> x >> y;
		cout << lcs(x, y) << 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;
    int i,j;

    X = ' ' + X;
    Y = ' ' + Y;

    for(i=0;i <= m;i++) c[i][0] = 0;
    for(j = 1; j <= n;j++) c[0][j] = 0;

    for(i = 1 ;i <= m;i++)
    {
        for(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;

constexpr int N=1000;

int lcs(string x, string y){
   int m=x.size();
   int n=y.size();
   int c[N+1][N+1];
   
   for(int i=0; i<N; i++)
      for(int j=0; j<N; j++)
         c[i][j]=0;
   int ans=0;
   for(int i=1; i<=m; i++){
      for(int j=1; j<=n; j++){
         if(x[i-1]==y[j-1]) c[i][j]=c[i-1][j-1]+1;
         else c[i][j]=max(c[i-1][j], c[i][j-1]);
         ans=c[i][j];
      }
   }
   return ans;
}

int main(){
   string x, y;
   int n;
   cin >> n;
   for(int i=0; i<n; i++){
      cin >> x >> y;
      cout << lcs(x, y) << endl;
   }

   return 0;
}