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] = dp[i-1][j-1]+1
				} else if S[i-1] != T[j-1] && dp[i-1][j] > dp[i][j-1] {
					dp[i][j] = dp[i-1][j]
				} else {
					dp[i][j] = dp[i][j-1]
				}
				// 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 <cstdio>
#include <algorithm>
#include <vector>
#include <string>

class TLcsSolver
{
    typedef std::vector<std::vector<int> > TTable;
    TTable FTable;
    std::string FArray1;
    std::string FArray2;

    int Count(int i, int j)
    {
        if(i == 0 || j == 0) return 0;
        if(FArray1[i-1] == FArray2[j-1]){
            return FTable[i-1][j-1] + 1;
        }
        return std::max(FTable[i-1][j], FTable[i][j-1]);
    }
    void Solve()
    {
        for(std::size_t i = 1; i < FTable.size(); ++i){
            for(std::size_t j = 1; j < FTable[i].size(); ++j){
                FTable[i][j] = Count(i, j);
            }
        }
    }
public:
    TLcsSolver(const std::string& a, const std::string& b)
     : FArray1(a), FArray2(b)
     , FTable(a.size()+1, std::vector<int>(b.size()+1, 0))
    {
        Solve();
    }
    int Answer()const
    {
        return FTable[FArray1.size()][FArray2.size()];
    }
};

int main()
{
    int n;
    std::scanf("%d", &n);
    char a[1000];
    char b[1000];
    for(int i = 0; i < n; ++i){
        std::gets(a);
        std::gets(b);
        std::printf("%d", TLcsSolver(a, b).Answer());
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int n,m,p;
string s,t;

int main(){
    cin >> p;
    for(int i=0;i<p;i++){
        cin >> s >> t;
        n=s.size();
        m=t.size();
        vector<int> memo(m+1,0);
        for(int j=0;j<n;j++){
            vector<int> memo2(m+1,0);
            for(int k=0;k<m;k++){
                memo2[k]=max(memo2[k],memo[k]);
                memo[k+1]=max(memo[k+1],memo[k]);
                memo2[k+1]=memo[k]+(s[j]==t[k]);
            }
            memo2[m]=max(memo2[m],memo[m]);
            for(int k=0;k<m+1;k++){
                memo[k]=memo2[k];
            }
        }
        int c=0;
        for(int j=0;j<m+1;j++){
            c=max(memo[j],c);
        }
        cout << c << endl;
    }
}

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

int main(void){
  string st1, st2;
  int q, val[1001][1001];

  cin >> q;

  for(int i=0; i<q; i++){
    cin>>st1;
    cin>>st2;

    for(int j=0; j<=st1.length(); j++){
      for(int k=0; k<=st2.length(); k++){

	if(j==0 || k==0) val[j][k] = 0;
	else{
	  if(val[j-1][k] == min(st1.length(), st2.length())){
	    val[j][k] = st1.length();
	  }else{

	    if(st1[j-1] == st2[k-1]){
	      val[j][k] = val[j-1][k-1]+1;
	    }else{
	      val[j][k] = max(val[j-1][k], val[j][k-1]);
	    }
	  }
	}
      }
    }

    /*
      for(int j=0; j<=st1.length(); j++){
      for(int k=0; k<=st2.length(); k++){
      cout << val[j][k] << " ";
      }
      cout << "\n";
      }
    */

    cout << val[st1.length()][st2.length()] << "\n";
  }

  return 0;
}

C++: #include <iostream>

using namespace std;
const int MAX = 1001;

short max(short a,short b) {
	if (a > b) {
		return a;
	} else {
		return b;
	}
}
short solve(string str1,string str2) {
	int len1 = str1.length();
	int len2 = str2.length();
	str1 = " " + str1;
	str2 = " " + str2;
	short LCS[MAX][MAX];
	for (int i=0;i<len1;i++) {
		LCS[i][0] = 0;
	}
	for (int i=0;i<len2;i++) {
		LCS[0][i] = 0;
	}
	int max1 = 0;

	for (int i=1;i<=len1;i++) {
		for (int j = 1;j<=len2;j++) {
			if (str1[i] == str2[j]) {
				LCS[i][j] = LCS[i-1][j-1] + 1;
			} else {
				LCS[i][j] = max(LCS[i-1][j],LCS[i][j-1]);
			}
			max1 = max(LCS[i][j],max1);
		}
	}
	return max1;
}

int main() {
	int n;
	cin >> n;
	for (int i=0;i<n;i++) {
		string str1,str2;
		cin >> str1 >> str2;
		cout << solve(str1,str2) << endl;
	}
	return 0;

}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <tuple>
#include <functional>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <map>
#include <random>
#include <cstdlib>
#define INT_MAX 2147483647
#define INT_MIN -2147483646
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;

string x, y;
int memo[1005][1005];

int dp(int a, int b) {
	if (memo[a][b] != INT_MIN) return memo[a][b];
	if (a == 0 || b == 0) memo[a][b] = 0;
	else if (x[a - 1] == y[b - 1]) {
		memo[a][b] = dp(a - 1, b - 1) + 1;
	}
	else {
		memo[a][b] = max(dp(a - 1, b), dp(a, b - 1));
	}
	return memo[a][b];
}

int main() {
	int q;
	cin >> q;
	Loop(i, q) {
		cin >> x >> y;
		Loop(j, x.size()+1) {
			Loop(k, y.size()+1) {
				memo[j][k] = INT_MIN;
			}
		}
		cout << dp(x.size(), y.size()) << endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

template <class RandomIt>
size_t lcsubsequence(
    RandomIt first1, RandomIt last1, RandomIt first2, RandomIt last2
) {
    vector<vector<size_t>> dp(last1-first1+1, vector<size_t>(last2-first2+1));
    for (RandomIt it=first1; it<last1; ++it)
        for (RandomIt it2=first2; it2<last2; ++it2) {
            if (*it == *it2) {
                dp[it-first1+1][it2-first2+1] = dp[it-first1][it2-first2] + 1;
            } else {
                dp[it-first1+1][it2-first2+1] = max(
                    dp[it-first1][it2-first2+1], dp[it-first1+1][it2-first2]
                );
            }
        }

    return dp[last1-first1][last2-first2];
}

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

    for (size_t i=0; i<q; ++i) {
        char buf[1<<10];
        scanf("%s", buf);
        string X(buf);

        scanf("%s", buf);
        string Y(buf);

        printf("%zu\n", lcsubsequence(X.begin(), X.end(), Y.begin(), Y.end()));
    }

    return 0;
}