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 <iostream>
#include <string>
#include <cstring>
using namespace std;

int q;
int **len;
int common(string a,string b,int m,int n){
    if(m==0||n==0){
        return 0;
    }
    if(a[m-1]==b[n-1]){
        if(m>1&&n>1){
            if(len[m-2][n-2]!=-1)len[m-1][n-1]=len[m-2][n-2]+1;
            else len[m-1][n-1]=common(a.substr(0,m-1),b.substr(0,n-1),m-1,n-1)+1;
        }else{
            len[m-1][n-1]=1;
        }
    }
    else{
        int l1,l2;
        //?±?l1
        if(n>1){
            if(len[m-1][n-2]!=-1)l1=len[m-1][n-2];
            else l1=len[m-1][n-2]=common(a,b.substr(0,n-1),m,n-1);
        }else l1=0;
        if(m>1){
            if(len[m-2][n-1]!=-1)l2=len[m-2][n-1];
            else l2=len[m-2][n-1]=common(a.substr(0,m-1),b,m-1,n);
        }else l2=0;

        len[m-1][n-1]=max(l1,l2);
    }
    return len[m-1][n-1];
}

int main()
{
    cin>>q;
    int i;
    for(i=0;i<q;i++){
        string a,b;
        cin>>a>>b;
        int m=a.length();
        int n=b.length();
        len=new int*[m];
        int j;
        for(j=0;j<m;j++){
            len[j]=new int[n];
            memset(len[j],-1,n*sizeof(int));
        }
        cout<<common(a,b,m,n)<<endl;
    }
    return 0;
}

//int main(){
//    int **p=new int*[5];
//    p[1]=new int[5];
//    memset(p[1],-1,5*sizeof(int));
//    cout<<p[1][3]<<" "<<p[0][1];
//}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i < (n);++i)
using namespace std;
template <class _t> inline bool chmax (_t& a,_t b){if(a<b){a=b;return true;}return false;} template <class _t> inline bool chmin (_t& a,_t b){if(a>b){a=b;return true;}return false;}

const long long inf = 1ll << 60;
const long long mod = 1000000007;

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

int main() {
    int t;cin >> t;
    for(int i=0;i<t;i++){
	string s1,s2;cin >> s1 >> s2;
	int m=s1.size(),n = s2.size();
	int dp[m+1][n+1];
	rep(i,m+1)rep(j,n+1) dp[i][j] = 0;
	for(int i=1;i<=m;i++){
	    for(int j=1;j<=n;j++){
		if(s1[i-1]==s2[j-1]) dp[i][j] = dp[i-1][j-1]+1;
		else{
		    dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
		}
	    }
	}
	cout << dp[m][n] << endl;
    }
    return 0;
}

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

using namespace std;

int main(void)
{
    int q;
    char x[1001], y[1001];
    int table[1001][1001];
    
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> x >> y;
        int xl = strlen(x), yl = strlen(y);

        for (int i = 0; i < xl; i++)
            for (int j = 0; j < yl; j++)
                if (x[i] == y[j])
                    table[i+1][j+1] = 1 + table[i][j];
                else
                    table[i+1][j+1] = max(table[i][j+1], table[i+1][j]);

        cout << table[xl][yl] << endl;
    }

    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>

int table[1001][1001];

int lcs(char *x, char *y){
    int i, j, lx, ly;
    lx = strlen(x+1);
    ly = strlen(y+1);
    for(i=1;i<=lx;i++){
        for(j=1;j<=ly;j++){
            if(x[i]==y[j])table[i][j] = table[i-1][j-1] + 1;
            else table[i][j] = std::max(table[i][j-1],table[i-1][j]);
        }
    }
    return table[lx][ly];
}

int main(){
    int i, j, q;
    char x[1002],y[1002];
    scanf("%d",&q);
    for(i=0;i<1001;i++){
        table[i][0] = 0;
    }
    for(j=0;j<1001;j++){
        table[0][j] = 0;
    }
    for(i=0;i<q;i++){
        scanf("%s %s",x+1,y+1);
        printf("%d\n",lcs(x,y));
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <cstring>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define clr(a,v) memset((a),(v),sizeof(a))

int q;
int dp[1001][1001];
std::string s, t;

int rec( int i, int j )
{
  int &ret = dp[i][j];

  if( ~ret )
    return ret;

  if( i == s.size() || j == t.size() )
    return 0;

  ret = 0;

  if( s[i] == t[j] )
    ret = std::max( ret, rec( i+1, j+1 )+1 );

  ret = std::max( ret, std::max( rec( i+1, j ), rec( i, j+1 ) ) );

  return ret;
}

int main()
{
  std::cin >> q;
  while( q-- )
  {
    std::cin >> s >> t;

    clr( dp, -1 );

    std::cout << rec( 0, 0 ) << std::endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
#include <string>
using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define _GLIBCXX_DEBUG

void printv(vector<int> v) {
  for (int i : v)
    cout << i << " ";
  cout << endl;
}

int main() {
  int q;
  cin >> q;
  rep(loop, q) {
    string x, y;
    cin >> x >> y;
    vector<vector<int>> L(1010, vector<int>(1010, 0));
    int lx = x.size(), ly = y.size();
    for (int i = 1; i <= lx; i++) {
      for (int j = 1; j <= ly; j++) {
        if (x[i-1] == y[j-1]) {
          L[i][j] = L[i - 1][j - 1] + 1;
        } else
          L[i][j] = max(L[i - 1][j], L[i][j - 1]);
      }
    }
    cout << L[lx][ly] << endl;
  }
}