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 f() {
	var S, T string
	fmt.Scan(&S)
	fmt.Scan(&T)
	lenS, lenT := len(S), len(T)
	dp := make([][]int, lenS+1)
	for i:=0; i<lenS+1; i++ {
		dp[i] = make([]int, lenT+1)
	}
	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])
}

func main(){
	var n int 
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		f()
	}
}

C++: #include<bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i=0;i<(n);++i)
#define per(i, n) for(int i=(n)-1;i>=0;--i)
#define repa(i, n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
const ll MOD = (ll)1e9+7;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
using namespace std;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}

ll modpow(ll x, ll b){
	ll res = 1;
	while(b){
		if(b&1)res = res * x % MOD;
		x = x * x % MOD;
		b>>=1;
	}
	return res;
}

ll modinv(ll x){
	return modpow(x, MOD-2);
}

string str1;
string str2;
int len1;
int len2;

vvector<int> dp;

int func(int p1, int p2){
	if(p1==len1||p2==len2)return 0;
	if(dp[p1][p2]>=0)return dp[p1][p2];
	int res = 0;
	if(str1[p1]==str2[p2])res = 1 + func(p1+1, p2+1);
	chmax(res, func(p1+1, p2));
	chmax(res, func(p1, p2+1));
	return dp[p1][p2] = res;
}

int main(){
	int n;
	cin >> n;
	rep(i, n){
		cin >> str1 >> str2;
		len1 = str1.size();
		len2 = str2.size();
		dp.assign(len1, vector<int>(len2, -1));
		cout << func(0, 0) << endl;
	}
	return 0;
}

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

int main()
{
	int n;
	string a, b;

	while (n--)
	{
		int memo1[1005] = {};
		int memo2[1005] = {};
		cin >> a >> b;

		for (int i = 1; i <= a.size(); i++)
		{
			memo2[0] = 0;
			for (int j = 1; j <= b.size(); j++)
			{
				if (a[i] == b[j])
					memo2[j] = max({ memo2[j - 1], memo1[j], memo1[j - 1] + 1 });
				else
					memo2[j] = max(memo2[j - 1], memo1[j]);
			}
			swap(memo1, memo2);
		}

		cout << memo1[b.size()] << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <climits>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <sstream>
#include <numeric>
// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>

///////////////////////////////////////////////////////////////
// namespace
///////////////////////////////////////////////////////////////
using namespace std;
// namespace mp = boost::multiprecision;

///////////////////////////////////////////////////////////////
// types
///////////////////////////////////////////////////////////////
using pii = pair<int, int>;
using ll = long long int;
using ld = long double;
// using mpi = mp::cpp_int;
// using mpf = mp::number<mp::cpp_dec_float<1024>>;

///////////////////////////////////////////////////////////////
// mod https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a
///////////////////////////////////////////////////////////////
// ll MOD = 1000000007;
// ll mod(long long val) {
//     long long res = val % MOD;
//     if (res < 0) res += MOD;
//     return res;
// }
// ll modinv(ll a) {
//     ll b = MOD, u = 1, v = 0;
//     while (b) {
//         ll t = a / b;
//         a -= t * b; swap(a, b);
//         u -= t * v; swap(u, v);
//     }
//     u %= MOD;
//     if (u < 0) u += MOD;
//     return u;
// }

vector<vector<int>> dp;

int lcs(const string &x, const string &y, int i, int j) {
    if (i >= x.size() || j >= y.size()) return 0;
    if (dp[i][j] >= 0) return dp[i][j];
    int ret = 0;
    ret = max(ret, lcs(x, y, i + 1, j));
    ret = max(ret, lcs(x, y, i, j + 1));
    if (x[i] == y[j]) ret = max(ret, lcs(x, y, i + 1, j + 1) + 1);
    return dp[i][j] = ret;
}

int lcs(const string &x, const string &y) {
    for (int i = x.size() - 1; i >= 0; --i) {
        for (int j = y.size() - 1; j >= 0; --j) {
            dp[i][j] = max(dp[i][j], dp[i + 1][j]);
            dp[i][j] = max(dp[i][j], dp[i][j + 1]);
            if (x[i] == y[j]) dp[i][j] = max(dp[i][j], dp[i + 1][j + 1] + 1);
        }
    }
    return dp[0][0];
    // return lcs(x, y, 0, 0);
}

int main() {
    int n;
    string x, y;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> x >> y;
        dp = vector<vector<int>>(x.size() + 5, vector<int>(y.size() + 5, 0));
        cout << lcs(x, y) << endl;
    }
    return 0;
}

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

int lcs(string X, string Y){
  int c[MAX][MAX]; 
  int m = X.length();
  int n = Y.length();
  for(int i=0; i<m; i++){
    c[i][0] = 0;
  }
  for(int i=0; i<=n; i++){
    c[0][i] = 0;
  }
  for(int i=1; i<=m; i++){
    for(int j=1; j<=n; j++){
      if(X[i-1] == Y[j-1]){
        c[i][j] = c[i-1][j-1] + 1;
      }else{
        c[i][j] = (c[i][j-1] > c[i-1][j] ? c[i][j-1] : c[i-1][j]);
      }
    }
  }
  return c[m][n];
}

int main(void){
  int q, l;
  string X, Y;
  cin >> q;
  for(int i=0; i<q; i++){
    cin >> X >> Y;
    l = lcs(X, Y);
    cout << l << endl;
  }
  return 0;
}

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

int main(){
  int n; cin >> n;
  for( int i = 0; i < n; i++ ){
    int dp[1010][1010];
    for( int i = 0; i < 1010; i++ ) dp[i][0] = dp[0][i] = 0;
    
    char x[1010], y[1010];
    cin >> x >> y;

    for( int i = 0; i < strlen( x ); i++ ){
      for( int j = 0; j < strlen( y ); j++ ){
	if( x[i] == y[j] ){
	  dp[i + 1][j + 1] = dp[i][j] + 1; 
	}else if( dp[i][j + 1] >= dp[i + 1][j] ){
	  dp[i + 1][j + 1] = dp[i][j + 1];
	}else{
	  dp[i + 1][j + 1] = dp[i + 1][j];
	}
      }
    }

    cout << dp[strlen( x )][strlen( y )] << endl;
  }
}