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>
using namespace std;

int lcs(string &X, string &Y) {
  int dp[1000 + 1][1000 + 1];
  int max = 0;
  int m = X.size();
  int n = Y.size();
  X = ' ' + X;
  Y = ' ' + Y;

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

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

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[j])
	dp[i][j] = dp[i-1][j - 1] + 1;
      else if (dp[i - 1][j] >= dp[i][j - 1])
	dp[i][j] = dp[i - 1][j];
      else
	dp[i][j] = dp[i][j - 1];

      if(max < dp[i][j])
	max = dp[i][j];
    }
  }

  return max;
}

int main() {
  int N = 0;
  cin >> N;

  string X, Y;
  for (int i = 0; i < N; i++) {
    cin >> X >> Y;

    cout << lcs(X, Y) << endl;
  }

  return 0;
}

C++: #include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

int c[1010][1010];

void lcs(char X[], char Y[]){
	int m = strlen(X);
	int n = strlen(Y);
	int i, j;
	for(i = 1;i <= m;i++)
		c[i][0] = 0;
	for(j = 1;j <= m;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]);
		}
	}
}

int main(void){
	int q, i;
	char A[1000], B[1000];
	scanf("%d", &q);
	for(i = 0;i < q;i++){
		scanf("%s", A);
		scanf("%s", B);
		lcs(A, B);
		printf("%d\n", c[strlen(A)][strlen(B)]);
	}
	return 0;
}

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

int main(){
  int q;
  cin >> q;
  for(int m=0; m<q; m++){
    string wordx, wordy;
    cin >> wordx >> wordy;
    int L[1000][1000]={0};
    
    for(int j=0; j<wordy.size(); j++){
      for(int i=0; i<wordx.size(); i++){
	if(wordx[i]==wordy[j]){
	  L[i][j] = 1+L[i-1][j-1];
	}
	else{
	  L[i][j] = max(L[i][j-1], L[i-1][j]);
	}
      }
    }
    cout << L[wordx.size()-1][wordy.size()-1] << endl;
    
    
  }
}

C++: #include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int solve() {
    string x, y;
    cin >> x >> y;
    vector<vector<int>> dp(x.size() + 1, vector<int>(y.size() + 1, 0));

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

    return dp[x.size()][y.size()];
}

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cout << solve() << endl;
    }
}

C++: #include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;

int c[1010][1010];

void lcs(char X[], char Y[]){
	int m = strlen(X);
	int n = strlen(Y);
	int i, j;
	for(i = 1;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 - 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]);
		}
	}
}

int main(void){
	int q, i;
	char A[1000], B[1000];
	scanf("%d", &q);
	for(i = 0;i < q;i++){
		scanf("%s", A);
		scanf("%s", B);
		lcs(A, B);
		printf("%d\n", c[strlen(A)][strlen(B)]);
	}
	return 0;
}

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

#define REP(i, n) for(int (i) = 0; (i) < (n); (i)++)

string str1, str2;
int dp[1001][1001];

void solve()
{
  REP(i, str1.size())
    if(str1[i] == str2[str2.size() - 1])
      dp[i][str2.size() - 1] = 1;
    else
      dp[i][str2.size() - 1] = 0;
  for(int i = str2.size() - 2; i >= 0; i--)
    if(str1[str1.size() - 1] == str2[i])
      dp[str1.size() - 1][i] = 1;
    else
      dp[str1.size() - 1][i] = dp[str1.size() - 1][i + 1];
  for(int i = str1.size() - 2; i >= 0; i--){
    for(int j = str2.size() - 2; j >= 0; j--){
      if(str1[i] == str2[j])
	dp[i][j] = dp[i + 1][j + 1] + 1;
      else
	dp[i][j] = max(dp[i + 1][j], dp[i][j + 1]);
    }
  }
}
  
int main()
{
  int q;
  cin >> q;
  cin.ignore();
  REP(i, q){
    getline(cin, str1);
    getline(cin, str2);
    fill(&dp[0][0], &dp[1001][0], -1);
    swap(str1, str2);
    solve();
    cout << dp[0][0] << endl;
  }
}