Go: package main

import (
	. "fmt"
    "math"
)

// func init() { Println("----------------------------------------") }

func main() {
    const L_MAX = 1003

	var q int
	Scanf("%d", &q)
	X := make([]string, 0, 150)
	Y := make([]string, 0, 150)
	for i := 0; i < 2 * q; i++ {
	    var tmp string
	    Scanf("%s", &tmp)

	    if i % 2 == 0 {
	        X = append(X, tmp)
	    } else {
	        Y = append(Y, tmp)
	    }
	}

    for i := 0; i < q; i++ {
        dp := make([][]int, L_MAX)
        for k, _ := range dp {
            dp[k] = make([]int, L_MAX)
        }

        x, y := X[i], Y[i]

        // init
        isXFound := false
        for c := 0; c < len(x); c++ {
            if isXFound || y[0] == x[c] {
                dp[0][c] = 1
                isXFound = true
            }
        }
        isYFound := false
        for r := 0; r < len(y); r++ {
            if isYFound || x[0] == y[r] {
                dp[r][0] = 1
                isYFound = true
            }
        }

        for r := 1; r < len(y); r++ {
            for c := 1; c < len(x); c++ {
                // dp[r][c]
                if x[c] == y[r] {
                    dp[r][c] = int(math.Max(float64(dp[r - 1][c - 1] + 1), float64(dp[r][c])))
                }

                dp[r][c] = int(math.Max(float64(dp[r][c]), math.Max(float64(dp[r - 1][c]), float64(dp[r][c - 1]))))
            }
        }
        Println(dp[len(y) - 1][len(x) - 1])

        // LCSも求めておく
        // ans := ""
        // r, c := len(y) - 1, len(x) - 1
        // for r >= 0 && c >= 0 {
        //     if y[r] == x[c] {
        //         ans = string(x[c]) + ans
        //         r--
        //         c--
        //     } else {
        //         if dp[r - 1][c] > dp[r][c - 1] {
        //             r--
        //         } else {
        //             c--
        //         }
        //     }
        // }
        // Println(ans)
    }
}

C++: #include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

#define NUM 1000

string X;
string Y;
int dp[NUM + 1][NUM + 1];

void init(){
    for(int i = 0; i < NUM + 1; i++){
        for(int j = 0; j < NUM + 1; j++){
            dp[i][j] = 0;
        }
    }
}

void calc(void){
    init();

    for(int j = 1; j <= Y.size(); j++){
        for(int i = 1; i <= X.size(); i++){
            if(Y[j] == X[i]){
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }else{
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }
    
    printf("%d\n", dp[X.size()][Y.size()]);

}

int main(void){
    int q;
    scanf("%d", &q);

    for(int i = 0; i < q; i++){
        cin >> X;
        cin >> Y;
        calc();
    }
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxx=1010;

int main (){
	int dp[maxx][maxx];
	string s,p;
	int q;
	cin>>q;
	while(q--){
		cin>>s>>p;
		for(int i=1;i<=s.size();i++) dp[i][0]=0;
		for(int i=1;i<=p.size();i++) dp[0][i]=0;
		for(int i=0;i<=s.size();i++){
			for(int j=0;j<=p.size();j++){
				if(s[i]==p[j]) dp[i][j]=dp[i-1][j-1]+1;
				else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
		cout<<dp[s.size()][p.size()]-1<<endl;
	}
	return 0;
}

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


int main(int argc, char *argv[]){
  int n;
  cin >> n;
  for (int r = 0; r < n; r++) {

    string x, y;
    cin >> x >> y;
    string dp[y.length() + 1][x.length() + 1];

    for (int i = 0; i < y.length(); i++) {
      for (int j = 0; j < x.length(); j++) {
        if(y[i] == x[j]){
          dp[i + 1][j + 1] = dp[i][j] + y[i];
        }else{
          dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < x.length(); i++) {
      ans = max(ans, (int)dp[y.length()][i].length());
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

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

int q;
string x, y;
int memo[1000][1000];

void init(){
  for(int i = 0; i < x.size(); i++){
    for(int j = 0; j < y.size(); j++){
      memo[i][j] = -1;
    }
  }
}

int lcs(int xp, int yp){
  if(xp < 0 || yp < 0) return 0;
  if(memo[xp][yp] >= 0) return memo[xp][yp];

  if(x[xp] == y[yp]){
    return memo[xp][yp] = lcs(xp - 1, yp - 1) + 1;
  }else{
    return memo[xp][yp] = max(lcs(xp - 1, yp), lcs(xp, yp - 1));
  }
}

int main(void){
  cin >> q;
  for(int i = 0; i < q; i++){
    cin >> x >> y;
    init();
    cout << lcs(x.size() - 1, y.size() - 1) << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define MAX 1002
#define INF 100000000
using namespace std;

int c[MAX][MAX]={},F[MAX]={};

int fib(int n){
  if(n==0||n==1)return 1;
  if(F[n]!=0)return F[n];
  return F[n]=fib(n-1)+fib(n-2);
}

void matrixChain(int p[],int n){
    int l,i,k,j,q;
    for(i=1;i<=n;i++)c[i][i]=0;
    for(l=2;l<=n;l++){
        for(i=1;i<=n-l+1;i++){
            j=i+l-1;
            c[i][j]=INF;
            for(k=i;k<j;k++){
                q=c[i][k]+c[k+1][j]+p[i-1]*p[k]*p[j];
                c[i][j]=min(c[i][j],q);
            }
        }
    }
}

void LCSLength(char *x,char *y){
    int m,n,i,j;
    for(m=0;x[m]!='\0';m++);
    for(n=0;y[n]!='\0';n++);

    // cout<<m<<" "<<n<<endl;
    for(i=0;i<MAX;i++)for(j=0;j<MAX;j++)c[i][j]=0;
    for(i=1;i<=m;i++)c[i][0]=0;
    for(j=0;j<=n;j++)c[0][j]=0;
    for(i=1;i<=m;i++){
        for(j=1;j<=n;j++){
            if(x[i-1]==y[j-1])c[i][j]=c[i-1][j-1]+1;
            else if(c[i-1][j]>=c[i][j-1])c[i][j]=c[i-1][j];
            else c[i][j]=c[i][j-1];
        }
    }
    // for(i=0;i<=m;i++){
    //     for(j=0;j<=n;j++){
    //         cout<<c[i][j];
    //     }
    //     cout<<endl;
    // }
    cout<<c[m][n]<<endl;
}


int main(){
    int n,p[MAX],i,j,k,m,g,count=0;
    char x[MAX],y[MAX];
    cin>>n;
    for(i=0;i<n;i++){
        scanf("%s",x);
        scanf("%s",y);
        LCSLength(x,y);
    }
    
    // matrixChain(p,n);
}

C++: //#define		FILE_INPUT_DEFINE
#ifdef		FILE_INPUT_DEFINE
#include	<fstream>
#endif		// FILE_INPUT_DEFINE

#include	<iostream>
#include	<string>
#include	<algorithm>

using namespace std;

#define			MAX		1000
#define			ind(x)		((x) - 1)

int C[MAX + 1][MAX + 1];

int LCS(string &X, string &Y)
{
	int			Xlen = X.length();
	int			Ylen = Y.length();

	for (int i = 0; i <= Xlen; ++i)
	{
		C[i][0] = 0;
	}

	for (int i = 0; i <= Ylen; ++i)
	{
		C[0][i] = 0;
	}

	for (int i = 1; i <= Xlen; ++i)
	{
		for (int j = 1; j <= Ylen; ++j)
		{
			if (X[ind(i)] == Y[ind(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[Xlen][Ylen]);
}

int main()
{
#ifdef		FILE_INPUT_DEFINE
	// input from txt
	std::ifstream in(R"(C:\Temp\input.txt)");
	std::cin.rdbuf(in.rdbuf());
#endif		// FILE_INPUT_DEFINE

	string	X, Y;
	int		n;

	cin >> n;
	cin.clear();
	cin.ignore();

	for (int i = 0; i < n; ++i)
	{
		getline(cin, X);
		getline(cin, 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;
  X = ' ' + X;  // X[0] に空白を挿入
  Y = ' ' + Y;  // Y[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 ( 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;
}