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(){
	buf := make([]byte, 0)
    sc.Buffer(buf, 1000000009)
	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 <string>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
#define MAX 100000

static const int N =100;

int lcs(string X,string Y){
  int C[N+1][N+1];
  int m=X.size();
  int n=Y.size();
  int ans=0;
  X= ' '+X;
  Y= ' '+Y;
  for(int i;i<=m;i++)c[i][0]=0;
  for(int j;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]);
      }
      ans=max(ans,c[i][j]);
    }
  
  }
  return ans;
}

int main(){
  string s1,s2;
  int n; cin >> n;
  for(int i=0;i<n;i++){
    cin >> s1>> s2;
    cout << lsc(s1,s2) << endl;
  }
  return 0;
}

C++: //
// Created by 李青坪 on 2018/10/29.
//

#include <cstdio>
#include <algorithm>
#include <string>
#include <iostream>

#define MAXN 150
#define MAXLEN 1000
using namespace std;

int c[MAXLEN+1][MAXLEN+1];

int getSubsequence(string x, string y){
    int i = 0, j = 0;
    while (i <= x.length()){
        while (j <= y.length()){
            if (i == 0 || j ==0){
                c[i][j] = 0;
            } else if (x[i-1] == y[j-1]){
                c[i][j] = c[i-1][j-1] + 1;
            } else if (x[i-1] != y[j-1]){
                c[i][j] = max(c[i-1][j], c[i][j-1]);
            }
            ++j;
        }
        ++i;
        j = 0;
    }
    return c[x.length()][y.length()];
}

int main(){
    int n;
    scanf("%d", &n);
    string x, y;
    for (int i = 0; i < n; ++i) {
        cin >> x >> y;
        printf("%d\n", getSubsequence(x, y));
    }

}
/*
3
abcbdab
bdcaba
abc
abc
abc
bc
 */

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

string lcs(string &s, string &t) {
    auto chmax = [](int& a, int b) { if (a < b) { a = b; return 1; } return 0; };
    int n = s.size(), m = t.size();
    vector<vector<int>> dp(n + 1, vector<int>(m + 1));
    vector<vector<int>> prv(n + 1, vector<int>(m + 1)); // ↓: 0, →: 1, ↘︎: 2
    for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
        if (s[i] != t[j]) {
            if (chmax(dp[i + 1][j + 1], dp[i][j + 1])) prv[i + 1][j + 1] = 0;
            if (chmax(dp[i + 1][j + 1], dp[i + 1][j])) prv[i + 1][j + 1] = 1;
        } else {
            if (chmax(dp[i + 1][j + 1], dp[i][j] + 1)) prv[i + 1][j + 1] = 2;
        }
    }
    string res = "";
    while (n > 0 && m > 0) {
        if (prv[n][m] == 0) n--;
        if (prv[n][m] == 1) m--;
        if (prv[n][m] == 2) n--, m--, res += s[n];
    }
    reverse(res.begin(), res.end());
    return res;
}

int main() {
    int T; cin >> T;
    while (T--) {
        string s, t; cin >> s >> t;
        cout << lcs(s, t).size() << endl;
    }
    return 0;
}

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

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 N = 1000;

int c[N][N];

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.reserve(N);
	y.reserve(N);
	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();
		memset(c, -1, sizeof(c));
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int dp[1001][1001];
int lcs(string x,string y) {
    int m = x.size(), n = y.size(),la = 0;
    x = " " + x;
    y = " " + y;
    for (int i = 0; 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
                dp[i][j] = max(dp[i-1][j],dp[i][j -1]);
            la = max(la, dp[i][j]);
        }
    }
    return la;
}
int main() {
    int q;string x, y;
    cin >> q;
    for (int i = 0; i < q;i++) {
        cin >> x >> y;
        cout << lcs(x, y) << endl;
    }

}

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

int main() {
  std::ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int N;
  cin>>N;
  string S1,S2;
  for (int k=0; k<N; k++) {
    cin>>S1>>S2;
    int m=S1.length();
    int n=S2.length();
    S1=' '+S1;
    S2=' '+S2;
    int lcs[1001][1001];
    for (int i=1; i<=m; i++) {
      lcs[i][0]=0;
    }
    for (int j=1; j<=n; j++) {
      lcs[0][j]=0;
    }
    int maxl=0;
    for (int i=1; i<=m; i++) {
      for (int j=1; j<=n; j++) {
        if (S1[i]==S2[j]) {
          lcs[i][j]=lcs[i-1][j-1]+1;
        }
        else {
          lcs[i][j]=max(lcs[i-1][j],lcs[i][j-1]);
        }
        maxl=max(maxl,lcs[i][j]);
      }
    }
    cout<<maxl<<endl;
  }
  return 0;
}

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

int N;


void solve(string s,string t){
  vector<vector<int >> dp(1001, vector<int>(1001));
  int S=s.size();
  int T=t.size();
  for(int i=0;i<S;i++){
    for(int j=0;j<T;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[S][T]<<endl;
}

int main() {

cin>>N;

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

}