Go: // AOJ ALDS1_10_C: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C

package main

import (
	"fmt"
)

func main() {
	var dataNum int
	var s1, s2 string

	fmt.Scanf("%d", &dataNum)

	for i := 0; i < dataNum; i++ {
		fmt.Scanf("%s", &s1)
		fmt.Scanf("%s", &s2)

		lcs := make([][]int, len(s1)+1)
		for j := 0; j < len(s1)+1; j++ {
			lcs[j] = make([]int, len(s2)+1)
		}

		for m, vs1 := range s1 {
			for n, vs2 := range s2 {
				if vs1 == vs2 {
					lcs[m+1][n+1] = lcs[m][n] + 1
				} else if lcs[m+1][n] > lcs[m][n+1] {
					lcs[m+1][n+1] = lcs[m+1][n]
				} else {
					lcs[m+1][n+1] = lcs[m][n+1]
				}
			}
		}

		// Answer
		fmt.Println(lcs[len(s1)][len(s2)])
	}
}

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>
#include<algorithm>
#include<string>

using namespace std;

int main() {

	int n;
	string A, B;
	int sizA, sizB, max=0;
	int c[1001][1001];
	
	cin >> n;
	
	for (int i = 0;i < n;++i) {
		cin >> A >> B;
		sizA = (int)(A.length());
		sizB = (int)(B.length());

		for (int a = 0;a <= sizA;++a) {
			for (int b = 0;b <= sizB;++b) {
				if (a == 0 || b == 0) {
					c[a][b] = 0;
				}else if(A[a-1]==B[b-1]){
					c[a][b] = c[a-1][b-1]+1;
				}else if(c[a][b-1]>c[a-1][b]){
				    c[a][b] = c[a][b-1];
				}else{
				    c[a][b] = c[a-1][b];
				}



				if (max < c[a][b]) {
					max = c[a][b];
				}
			}
		}
		cout << max << endl;
		max = 0;
	}
	return 0;
}