Go: package main

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

// define
var (
	sc = bufio.NewScanner(os.Stdin)
)

func bordersInt(in_map [][]int, invalid int) [][]int{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]int, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]int, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func bordersString(in_map [][]string, invalid string) [][]string{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]string, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]string, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func scanIntLine(len int) []int{
	arr := make([]int, len)
	for i:=0; i<len; i++{
		sc.Scan()
		tmp, err := strconv.Atoi(sc.Text())
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func scanStringLine(len int) []string{
	arr := make([]string, len)
	for i:=0; i<len; i++{
		sc.Scan()
		arr[i] = sc.Text()
	}
	return arr
}

func scanIntSplit(len int) []int{
	arr := make([]int, len)
	sc.Scan()
	for i, v := range strings.Split(sc.Text(), " "){
		tmp, err := strconv.Atoi(v)
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func maxInt(args... int) int{
	sort.Ints(args)
	return args[len(args)-1]
}

func main(){
	var n int 
	fmt.Scan(&n)
	dp := make([][]int, 1003)
	for i:=0; i<1003; i++ {
		dp[i] = make([]int, 1003)
	}
	input_lines := scanStringLine(2*n)
	for i:=0; i<n; i++ {
		S, T := input_lines[2*i], input_lines[2*i+1]
		lenS, lenT := len(S), len(T)
		for i:=1; i<lenS+1; i++ {
			for j:=1; j<lenT+1; j++ {
				if S[i-1] == T[j-1] {
					dp[i][j] = maxInt(dp[i-1][j-1]+1, dp[i-1][j], dp[i][j-1])
				} else {
					dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
				}
			}
		}
		fmt.Println(dp[lenS][lenT])
	}
}

C++: #include <iostream>
#include <stdio.h>
#include <string>
#include <cmath>

#define MAX_LENGTH 1000
#define NIL -1
using namespace std;
//think easy make easy

int q;//num of datasets
string X;
string Y;
char map[MAX_LENGTH+1][MAX_LENGTH+1];

int findMaxLength(int x,int y){//x:Xの終端.y:Yの終端(0オリジン)
	int val;

	if(x < 0 || y < 0) return 0;

	if(map[x][y] == NIL){
		if(X.at(x) == Y.at(y)){
			val = findMaxLength(x-1,y-1) +1;
		}else{
			val = max(findMaxLength(x-1,y),findMaxLength(x,y-1));
		}
		map[x][y] = val;//メモ
		return val;
	}else return map[x][y];
}

int main(){
	
	scanf("%d",&q);

	for(int i=0;i<q;i++){
		cin >>X; cin >> Y;
		for(int i=0;i<X.size();i++){
			for(int j=0;j<Y.size();j++){
				map[i][j] = NIL;
			}
		}
	
		cout <<findMaxLength(X.size()-1,Y.size()-1) <<endl;
	}

	return 0;
}

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

using namespace std;



int n;
string a, b;
string x[1000], y[1000];




int main(){
  cin >> n;
  while(n != 0){
    n = n -1;
    int longest[1000][1000];
    cin >> a;
    cin >> b;
    a = ' ' + a;
    b = ' ' + b;
    int mm = 0;
    
   for(int i = 1; i <= a.size(); ++i){
     longest[i][0] = 0;
    }
   for(int j = 1; j <= b.size(); ++j){
     longest[0][j] = 0;
   }
    for(int i = 1; i <= a.size(); ++i){
      for(int j = 1; j <= b.size(); ++j){
        
        
        if(a[i] == b[j]){
          longest[i][j] = 1 + longest[i-1][j - 1];
        }
        else{
          longest[i][j] = max(longest[i][j-1], longest[i-1][j]);
        }
        mm = max(mm, longest[i-1][j-1]);
      }
    }
    //cout << a[1] << endl;
    cout << mm << endl;
  }
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int dp[1001][1001];
char x[1001], y[1001];
int main() {
	int q; scanf("%d", &q);
	rep(i, q) {
		scanf("%s%s", x, y);
		rep(i, 1000)rep(j, 1000) {
			if (x[i] == y[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]);
		}
		printf("%d\n", dp[1000][1000]);
	}
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;

template<class T> inline void chmax(T& a, T b) { if (a < b) a = b; }
template<class T> inline void chmin(T& a, T b) { if (a > b) a = b; }


namespace Lcs
{

	int exec()
	{
		string S, T; cin >> S >> T;
		vector<vector<int>> dp(S.size() + 1, vector<int>(T.size() + 1, 0));
		for (int s = 0; s < S.size(); ++s) {
			for (int t = 0; t < T.size(); ++t) {
				if (S[s] == T[t]) dp[s + 1][t + 1] = dp[s][t] + 1;
				chmax(dp[s + 1][t + 1], dp[s + 1][t]);
				chmax(dp[s + 1][t + 1], dp[s][t + 1]);
			}
		}
		return dp[S.size()][T.size()];
	}
}

int main()
{
	int Q; cin >> Q;
	vector<int> anss(Q);
	for (int i = 0; i < Q; ++i) {
		anss[i] = Lcs::exec();
	}
	for (int i = 0; i < Q; ++i) {
		cout << anss[i] << endl;;
	}
}

C++: #if 0

#endif

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int Q;
int L[1000+10][1000+10];

int main() {
  cin >> Q;
  string X, Y;
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < 1000+10; j++) {
      L[0][j] = 0; L[j][0] = 0;
    }
    cin >> X >> Y;
    for (int x = 1; x <= X.length(); x++) {
      for (int y = 1; y <= Y.length(); y++) {
        if (X[x-1] == Y[y-1])
          L[x][y] = L[x-1][y-1]+1;
        else
          L[x][y] = max(L[x-1][y], L[x][y-1]);
      }
    }

    cout << L[X.length()][Y.length()] << endl;
  }
}

C++: #include <bits/stdc++.h>

using namespace std;
int main(int argc, const char * argv[]) {
    int q, i, m, n, sl1, sl2;
    cin >> q;
    vector<string> strlist;
    string str, str1, str2;
    for(i = 0;i < q;i++){
        cin >> str;
        strlist.push_back(str);
        cin >> str;
        strlist.push_back(str);
    }
    
    //start
    for(i = 0;i < q*2;i += 2){
        str1 = strlist[i];
        sl1 = str1.length();
        str2 = strlist[i+1];
        sl2 = str2.length();
        int dp[sl1+1][sl2+1];
        for(m = 0;m <= sl2;m++){
            dp[0][m] = 0;
        }
        for(m = 0;m <= sl1;m++){
            dp[m][0] = 0;
        }
        for(m = 1;m <= sl1;m++){
            for(n = 1;n <= sl2;n++){
                if(str1[m] == str2[n]){
                    dp[m][n] = dp[m-1][n-1] + 1;
                }else{
                    dp[m][n] = max(dp[m-1][n], dp[m][n-1]);
                }
            }
        }
        m = sl1;
        n = sl2;
        printf("%d\n", dp[m][n]);
        
    }
    return 0;
}

C++: #include <math.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;
using Graph = vector<vector<int>>;

using ll = long long;
typedef pair<ll, ll> P_ll;
typedef pair<int, int> P;

const ll INF_ll = 1e17;
const int INF = 1e8;

int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    string X, Y;
    cin >> X >> Y;
    vector<vector<int>> dp(X.size() + 1, vector<int>(Y.size() + 1, 0));
    for (int j = 0; j < X.size(); j++) {
      for (int k = 0; k < Y.size(); k++) {
        if (X[j] == Y[k]) {
          dp[j + 1][k + 1] = dp[j][k] + 1;
        } else {
          dp[j + 1][k + 1] = max(dp[j + 1][k], dp[j][k + 1]);
        }
      }
    }
    cout << dp[X.size()][Y.size()] << endl;
  }

  return 0;
}