Go: package main

import (
	"bufio"
	"os"
	"fmt"
	"strconv"
)

const max int = 1000 + 1
var A [][]int

func lcs(x, y string) int {
	x = " " + x
	y = " " + y
	for i:=0;i<len(x); i++ {
		for j:=0;j<len(y); j++ {
			A[i][j] = 0
		}
	}
	for i:=1; i<len(x); i++ {
		for j:=1; j<len(y); j++ {
			if x[i] == y[j] {
				A[i][j] = A[i-1][j-1] + 1
			} else if A[i-1][j] > A[i][j-1] {
				A[i][j] = A[i-1][j]
			} else {
				A[i][j] = A[i][j-1]
			}
		}
	}
	return A[len(x)-1][len(y)-1]
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num 
}

func nextStr(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}


func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	A = make([][]int, max)
	for i := range A {
		A[i] = make([]int, max)
	}
	n := nextInt(scanner)
	for i:=0; i<n; i++ {
		x := nextStr(scanner)
		y := nextStr(scanner)
		fmt.Printf("%d\n", lcs(x, y))
	}
}

C++: /*
* @Author: Samson
* @Date:   2018-06-11 11:31:19
* @Last Modified by:   Samson
* @Last Modified time: 2018-06-11 11:46:49
*/
//   @URL : http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C
#include<bits/stdc++.h>
#include<algorithm>
#include <cstdlib>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e3+10;

string a,b;
int f[MAXN][MAXN];

int solve()
{
	memset(f,0,sizeof f);
	int lena = a.size(),lenb = b.size();
	int maxv = 0;
	a = ' '+a,	b = ' '+b;
	for(int i = 1; i <= lena; ++i)
	{
		for(int j = 1; j <= lenb; ++j)
		{
			if(a[i] == b[j])
				f[i][j] = f[i-1][j-1]+1;
			else
				f[i][j] = max(f[i-1][j],f[i][j-1]);
			if(f[i][j] > maxv)	maxv = f[i][j];
		}
	}
	return maxv;
}
int main(void)
{
	ios::sync_with_stdio(false); 
	//cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		cin>>a>>b;
		cout<<solve()<<'\n';
	}
	return 0;
}

C++: #include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ld long double
#define INF 1000000000000000000
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N;
    cin >> N;
    rep(i, N) {
        string s, t;
        cin >> s >> t;
        int N = s.size(), M = t.size();
        vector<vector<int>> dp(N + 1, vector<int>(M + 1));

        rep(i, N) {
            rep(j, M) {
                if (s[i] == t[j])
                    dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
            }
        }

        cout << dp[N][M] << endl;
    }
}

C++: #include "bits/stdc++.h"
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;
	Y= " " +Y;
	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;
}

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

#if __has_include("print.hpp")
  #include "print.hpp"
#endif

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef long long ll;
typedef pair<int, int> p;

vector<vector<int>> dp(2000, vector<int>(2000));

void solve(string s, string t){
  int n = int(s.size());
  int m = int(t.size());

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if(s[i] == t[j]){
        dp[i+1][j+1] = dp[i][j] + 1;
      }else{
        dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
      }
    }
  }

  cout << dp[n][m] << endl;
}

int main(){
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    string s, t;
    cin >> s >> t;
    solve(s, t);
  }
}

C++: #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
static const int N = 1000;
void lcs(string x, string y)
{
    int m = x.length(), n = y.length();
    int c[1001][1001]; //c[0][0] = 0;
    int maxv = 0;
    x = ' ' + x;
    y = ' ' + y;
    for(int i=0; i<=m; i++) c[i][0] = 0;
    for(int j=0; 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]);
            maxv = max(maxv, c[i][j]);
        }
    }
    printf("%d\n", maxv);
}
int main()
{
    int q; string x, y;
    cin >> q;
    for(int i=0; i<q; i++)
    {
        cin >> x >> y;
        lcs(x, y);
    }
    //system("pause");
    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 = 0; i <= m; i++) c[i][0] = 0;
    for (int j = 0; 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;
}

C++: #include<stdio.h>
#include<string.h>
#define max(a,b) ((a)>(b) ? (a):(b))
int dp[1001][1001];
int lcs(char *s,char *t){
    int i,j,ls,lt;

    ls=strlen(s);
    lt=strlen(t);
    for(i=0;i<ls;i++){
	for(j=0;j<lt;j++){
	    if(s[i]==t[j]) dp[i+1][j+1]=dp[i][j]+1;
	    else dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
	}
    }
    return dp[ls][lt];
}
int main(){
    char s[1001],t[1001];
    int k;

    scanf("%d", &k);
    while(k--){
	scanf("%s%s", s, t);
	printf("%d\n",lcs(s,t));
    }

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#define _A_ true
using namespace std;
 
int main(){
  int n,AC=0,strA,strB;
  int dp[1010][1010];
  char *A = new char[1500];
  char *B = new char[1500];
 
  scanf("%d",&n);
  while(_A_){
    if(AC == n)break;
    scanf("%s",A);
    scanf("%s",B);
    for(int i=0;i<1010;i++)for(int j=0;j<1010;j++)dp[i][j]=0;
strA = strlen(A); 
strB = strlen(B);

    for(int i=0;i<strA;i++){
      for(int j=0;j<strB;j++){
    if(A[i] == B[j]){
      dp[i+1][j+1]=dp[i][j]+1;
    }
    else{
      if(dp[i+1][j] > dp[i][j+1]){
        dp[i+1][j+1]=dp[i+1][j];
      }
      else {
        dp[i+1][j+1] = dp[i][j+1];
      }
    }
      }
    }
    printf("%d\n",dp[strA][strB]);
    AC++;
  }
  delete [] A;
  delete [] B;
 
  return 0;
}

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


int S[1001][1001];

void prep(string str1, string str2, int len1, int len2)
{
	int a, b, i;
	S[0][0] = 0;
	for(i = 1; i <= len1; i++){ S[i][0] = 0; }
	for(i = 1; i <= len2; i++){ S[0][i] = 0; }
	for(a = 1; a <= len1; a++){
		for(b = 1; b <= len2; b++){
			if(str1[a - 1] == str2[b - 1]){ S[a][b] = 1; }else{ S[a][b] = 0; }
		}
	}
	return;
}

int main()
{
	int i, a, b, q, len1, len2;
	cin >> q;
	string str1, str2;

	for(i = 0; i < q; i++){
		cin >> str1 >> str2;
		len1 = str1.size();
		len2 = str2.size();
		prep(str1, str2, len1, len2);
		for(a = 1; a <= len1; a++){
			for(b = 1; b <= len2; b++){
				if(S[a][b]){ S[a][b] = S[a - 1][b - 1] + 1; }
				else{ S[a][b] = max(S[a - 1][b], S[a][b - 1]); }
			}
		}
		cout << S[len1][len2] << endl;
	}
	return 0;
}