Go: package main

/*
最長共通部分列
*/

import (
	"bufio"
	"bytes"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

func larger(l, r int) int {
	if l > r {
		return l
	}
	return r
}

var memo [1001][1001]int

func countLCS(x, y string) int {
	x = " " + x
	y = " " + y
	max := 0
	for xi := 1; xi < len(x); xi++ {
		for yi := 1; yi < len(y); yi++ {
			if x[xi] == y[yi] {
				memo[xi][yi] = memo[xi-1][yi-1] + 1
			} else {
				memo[xi][yi] = larger(memo[xi-1][yi], memo[xi][yi-1])
			}
			max = larger(max, memo[xi][yi])
		}
	}
	return max
}

func main() {
	scanner := makeScanner(1024)
	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		x := eGetLine(scanner)
		y := eGetLine(scanner)
		fmt.Println(countLCS(x, y))
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

type ints []int

func (s ints) String() string {
	var buf bytes.Buffer
	buf.WriteString("[")
	sep := ""
	for _, v := range s {
		buf.WriteString(fmt.Sprintf("%s%d", sep, v))
		sep = ", "
	}
	buf.WriteString("]")
	return buf.String()
}

func reverse(a ints) ints {
	r := make(ints, len(a))
	for i := 0; i < len(a); i++ {
		r[i] = a[len(a)-1-i]
	}
	return r
}
func max(a ints) (int, bool) {
	if len(a) == 0 {
		return 0, false
	}
	m := a[0]
	for _, e := range a {
		if e > m {
			m = e
		}
	}
	return m, true
}
func min(a []int) int {
	m := a[0]
	for _, v := range a {
		if v < m {
			m = v
		}
	}
	return m
}

func sum(a ints) int {
	if len(a) == 0 {
		return 0
	}
	sum := 0
	for _, e := range a {
		sum += e
	}
	return sum
}
func find(n int, a ints) int {
	for i, v := range a {
		if n == v {
			return i
		}
	}
	return -1
}

C++: #include<math.h>
#include<limits.h>
#include<iostream>
#include<string>
#include<vector>
#include<stdio.h>
#include<sstream>
#include<list>
#include<queue>
#include<algorithm>
#include<functional>
#include<map>
#include<set>
//#include<utility>
//#include<initializer_list>
//#include<tuple>
//#include<regex>
using namespace std;
#define ull unsigned long long
#define ll long long
#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define all(t) t.begin(), t.end()
bool isSubstring(string substr, string t) {
	int n = substr.length();
	int seek = 0;
	rep(i, n) {
		bool ex = 0;
		for (int j = seek; j < t.length(); j++) {
			if (substr[i] == t[j]) {
				ex = 1;
				seek = j + 1;
				break;
			}
		}
		if (ex) {
			continue;
		}
		else {
			return false;
		}
	}
	return true;

}

int LongestCommonSubstring(string s, string t) {
	// Wrong plan
	vector<int>dp(s.length(), 0);
	int n = s.length();
	string lcs = "";
	if (isSubstring(s.substr(n - 1, n), t)) {

		dp[n - 1] = 1;
		lcs = s[n - 1];
	}
	for (int i = n - 2; i >= 0; i--) {
		//string substr = s.substr(i, n);
		string substr = s[i] + lcs;
		int issubst = isSubstring(substr, t);
		if (issubst) lcs = substr;
		dp[i] = dp[i + 1] + issubst;
	}
	return dp[0];
}
int LongestCommonSubstring2(string s, string t) {
	vector<vector<int>> dp(s.length(), vector<int>(t.length(), 0));
	int n = s.length();
	int m = t.length();
	rep(i, n - 1) {
		rep(j, m - 1) {
			dp[i + 1][m + 1] = s[i] == t[j] ? dp[i][j] + 1 : max(dp[i][j - 1], dp[i + 1][j]);
		}
	}
	return dp[n - 1][m - 1];
}
int main() {
	//cout << isSubstring("abc", "abcde") << endl;
	//cout << isSubstring("bd", "abcde") << endl;
	//cout << isSubstring("db", "abcde") << endl;

	//cout << LongestCommonSubstring("bacd", "becd") << endl;
	//cout << LongestCommonSubstring("abcd", "becd") << endl;
	//cout << endl;
	//cout << LongestCommonSubstring("a", "a") << endl;
	//cout << LongestCommonSubstring("bbb", "bbb") << endl;
	//cout << LongestCommonSubstring("aba", "bbaba") << endl;
	//cout << LongestCommonSubstring("abababbbaa", "bbabaabba") << endl;
	//cout << LongestCommonSubstring("a", "aaaaa") << endl;
	//cout << LongestCommonSubstring("aizojfeiz", "aaaizojifeizz") << endl;
	//cout << LongestCommonSubstring("zzyiioxizu", "zzsyoizuo") << endl;
	//cout << LongestCommonSubstring("iooooioiooiio", "ooiiioioiooooiioio") << endl;
	//cout << LongestCommonSubstring("ajfoaooieellzljieoaelj", "ajoiiffoaolliseellzljieoaelj") << endl;
	//cout << LongestCommonSubstring("zzzzzyyzyy", "zzyyyyz") << endl;
	//cout << LongestCommonSubstring("abcdef", "fedcba") << endl;
	//cout << LongestCommonSubstring("abc", "xyz") << endl;
	int q;
	cin >> q;
	vector<string> s(q);
	vector<string> t(q);
	rep(i, q) {
		cin >> s[i] >> t[i];
	}
	rep(i, q) {
		cout << LongestCommonSubstring2(s[i], t[i]) << endl;
	}

	return 0;
}

C++: #include <iostream>

using namespace std;

/**
 *  lcs(m,n) = 
 *  if x[m] == y[n] lcs[m-1,n-1]+1
 *  max(lcs[m-1,n],lcs[m,n-1])
 *
 */

string x, y;

const int q = 150;

int c[q][q];

int lcs(int m, int n)
{
	if (m == -1 || n== -1) return 0;
	if (x[m] == y[n]) return lcs(m-1,n-1) + 1;
	int & cc = c[m][n];
	if (cc != -1) return cc;
	int xx = lcs(m-1,n);
	int yy = lcs(m,n-1);
	cc = xx > yy ? xx : yy;
	return cc;
}

int main()
{
	int n = 0;
	x.resize(q);
	y.resize(q);
	memset(c, -1, sizeof(c));
	cin >> n; 
	for (int i=0;i<n;i++)
	{
		cin >> x;
		cin >> y;
		cout << lcs(x.size()-1, y.size()-1) << endl;
		x.clear();
		y.clear();
	}
}

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

using namespace std;
static const 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 = 1; i <= m; ++i ) c[i][0] = 0;
    for ( int i = 1; i <= n; ++i ) c[0][i] = 0;
    int max1=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]);
    		}
    		max1 = max (max1, c[i][j]);
    	}
	return max1;

}
int main() {

	int n;
    string X, Y;

    cin >> n;
    while ( n-- ) {
    	cin >> X >> Y;
    	cout << lcs(X, Y) << endl;
    }
	return 0;
}

C++: //	created : 16/03/15
// 	author   : Rp7rf
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define vi vector<int>
#define mp make_pair
#define pb push_back
#define MOD 1000000007
#define INF 1e9


int main(void){
	int n;
	string s1,s2;
	cin>>n;
	int dp[1010][1010];
	memset(dp,0,sizeof(dp));
	rep(i,n){
		int ret = 0;
		cin>>s1>>s2;
		rep(i,s1.size())dp[i][0] = 0;
		rep(j,s2.size())dp[0][j] = 0;

		loop(i,0,s1.size()){
			loop(j,0,s2.size()){
				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]);
				}
				//cout<<dp[i][j];
				ret = max(ret,dp[i][j]);
			}
			//cout<<endl;
		}
		cout<<ret<<endl;
	}
}