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 <bits/stdc++.h>
using namespace std;

int lcs(string &x,string &y);

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

int lcs(string &x,string &y)
{
	const int maxlen=1000+24;
	vector<vector<int>> dp(maxlen,vector<int>(maxlen));

	int lenx=x.size();
	int leny=y.size();
	for (int i=0;i<=lenx;++i)
	{
		dp[i][0]=0;
	}
	for (int j=0;j<=leny;++j)
	{
		dp[0][j]=0;
	}

	for (int i=1;i<=lenx;++i)
	{
		for (int j=1;j<=leny;++j)
		{
			if (x[i-1]==y[j-1])
			{
				dp[i][j]=dp[i-1][j-1]+1;
			}
			else
			{
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
	}

	int maxdp=0;
	for (int j=1;j<=leny;++j)
	{
		maxdp=max(maxdp,dp[lenx][j]);
	}
	return maxdp;
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
  int n;
  cin>>n;
  while(n--){
    string s,ss;
    cin>>s>>ss;
    int a[ss.size()+2][s.size()+2];
    r(i,0,ss.size()+2)r(j,0,s.size()+2)a[i][j]=0;
    ss=' '+ss;
    s=' '+s;
    r(i,1,ss.size()+1){
      r(j,1,s.size()+1){
        if(ss[i]==s[j])a[i][j]=a[i-1][j-1]+1;
        else a[i][j]=max(a[i-1][j],a[i][j-1]);
      }
    }
    cout<<a[ss.size()-1][s.size()-1]<<endl;
  }
}

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

int calcLCS(string s, string t) {
    if (s.length() < t.length()) {
        string temp = s;
        s = t;
        t = temp;
    }

    int table[s.length()+1][t.length()+1];
    for (int i=0; i<=s.length(); i++) {
        table[i][0] = 0;
    }
    for (int j=0; j<=t.length(); j++) {
        table[0][j] = 0;
    }

    for (int i=1; i<=s.length(); i++) {
        for (int j=1; j<=t.length(); j++) {
            if (s.at(i-1) == t.at(j-1)) {
                table[i][j] = table[i-1][j-1] + 1;
            } else {
                table[i][j] = max(table[i][j-1], table[i-1][j]);
            }
        }
    }

    return table[s.length()][t.length()];
}

int main() {
    int n;
    cin >> n;
    for (int i=0; i<2*n; i+=2) {
        string x, y;
        cin >> x >> y;

        cout << calcLCS(x, y) << endl;
    }

    return 0;
}

C++: #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 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 n;
    cin >> n;
    while(n)
    {
        n--;
        string X,Y;
        int i=0,j=0;
        cin >> X;
        cin >> Y;
        int L[1001][1001];
        for(i=0;i<1001;i++)
            for(j=0;j<1001;j++)
               L[i][j]=0;
        for(i=1;i<=X.size();i++)
            for(j=1;j<=Y.size();j++)
            {
                if(X[i-1]==Y[j-1])
                    L[i][j]=1+L[i-1][j-1];
                else    L[i][j]=max(L[i][j-1],L[i-1][j]);
            }
        cout << L[X.size()][Y.size()] << endl;
    }
    return 0;
}

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

using namespace std;
static const int N = 1000;

int lenCommon(string s1, string s2)
{
	int len = max(int(s1.size()), int(s2.size()));
	vector<vector<int> > dp(N+1, vector<int>(N+1, 0));

	s1 = " " + s1;
	s2 = " " + s2;

	for (int i = 0; i <= len; ++i) dp[i][0] = 0;
	for (int i = 0; i <= len; ++i) dp[0][i] = 0;

	int ret = 0;
	for (int i = 1; i <= len; ++i)
	{
		for (int j = 1; j <= len; ++j)
		{
			if (s1[i] == s2[j])
				dp[i][j] = dp[i - 1][j - 1] + 1;
			else
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

			ret = max(ret, dp[i][j]);
		}
	}
	return ret;
}

int main()
{
	string s1, s2;
	int num; cin >> num;
	for (int i = 0; i < num; ++i)
	{
		cin >> s1 >> s2;
 		int res = lenCommon(s1, s2);
		cout << res << endl;
	}
}

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

using namespace std;

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

int main() {
  int q;
  int i, j, k;
  string x, y;
  cin >> q;
  for (i = 0; i < q; i++) {
    int maxVal = 0;
    cin >> x;
    cin >> y;
    int lenx = x.size();
    int leny = y.size();
    x = ' ' + x;
    y = ' ' + y;
    int c[lenx + 1][leny + 1];
    for (j = 0; j <= lenx; ++j) {
      c[j][0] = 0;
    }
    for (j = 0; j <= leny; ++j) {
      c[0][j] = 0;
    }
    for (j = 1; j <= lenx; ++j) {
      for (k = 1; k <= leny; ++k) {
        if (x[j] == y[k]) {
          c[j][k] = c[j - 1][k - 1] + 1;
        } else {
          c[j][k] = max(c[j - 1][k], c[j][k - 1]);
        }
        maxVal = max(maxVal, c[j][k]);
      }
    }
    cout << maxVal << '\n';
  }

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <bitset>
#include <iterator>
#include <iomanip>
#include <sstream>
#define INF 2000000000
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
#define sym cout<<"---------"<<endl;
#define ll long long
#define PL pair<long long, long long>
#define P pair<int,int>
#define mk make_pair
#define en endl
#define WHITE 0
#define BLACK 2
#define GRAY 1
#define RE return 0
#define int ll

//  最長共通部分列

int dp[1005][1005];

signed main(){
  int n;
  cin>>n;
  for(int i=0; i<n; i++){
    string s1,s2;
    cin>>s1>>s2;
    s1=" "+s1;
    s2=" "+s2;
    int maxl=0;
    for(int i=0; i<=s1.size()-1; i++) for(int j=0; j<=s2.size(); j++) dp[i][j]=0;
    for(int i=1; i<=s1.size()-1; i++){
      for(int j=1; j<=s2.size(); j++){
        if(s1[i]==s2[j]) dp[i][j]=dp[i-1][j-1]+1;
        else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
        maxl=max(maxl,dp[i][j]);
      }
    }
    cout<<maxl<<en;
  }
}

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

using namespace std;

int lcs(string x,string y){
  int m=x.size();
  int n=y.size();
  int c[m+1][n+1];
  for(int i=0;i<=m;i++)
    c[i][0]=0;
  for(int i=1;i<=n;i++)
    c[0][i]=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]);
    }
  }
  return c[m][n];
}

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