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<algorithm>
using namespace std;

int main(){
    //input
    long long q;
    cin >> q;
    long long i,j,k, m, n;
    string S, T;
    const char *s = NULL, *t = NULL;
    for(k = 1; k <= q; k++){
        cin >> S;
        m = S.length();
        s = S.c_str();
        cin >> T;
        n = T.length();
        t = T.c_str();
        long long dp[m+1][n+1];
        for(i = 0; i<= m; i++){
            dp[i][0] = 0;
        }
        for(j = 0; j <= n; j++){
            dp[0][j] = 0;
        }
        for(i = 1; i <= m; i++){
            for(j = 1; j <= n; j++){
                dp[i][j] = max({dp[i-1][j-1] + (s[i-1]==t[j-1]) , dp[i-1][j], dp[i][j-1]});
            }
        }
        cout<< dp[m][n] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,x,n) for(int i=x; i<(n); i++)
#define ALL(n) begin(n),end(n)
#define MOD (1000000007)
#define INF (1e9)
#define INFL (1e12)
 
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
template<class T>using arr=vector<vector<T>>;
template<class T>void pr(T x){cout << x << endl;}
template<class T>void prvec(vector<T>& a){rep(i, a.size()-1){cout << a[i] << " ";} cout << a[a.size()-1] << endl;}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
 
int main()
{
    int q; cin >> q;
    arr<int> dp(1005, vector<int>(1005, 0));
    string s, t;
    rep(i, q){
        cin >> s >> t;
        int s_l = s.size(), t_l = t.size();
        rep(si, s_l) rep(ti, t_l){
            if(s[si]==t[ti]){
                dp[si+1][ti+1] = max(dp[si][ti]+1, dp[si+1][ti]);
            }else{
                dp[si+1][ti+1] = max(dp[si+1][ti], dp[si][ti+1]);
            }
        }

        pr(dp[s_l][t_l]);
        rep(si, s_l+1) rep(ti, t_l+1) dp[si][ti] = 0;
    }
    return 0;}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxx = 1010;
int c[maxx][maxx];

int lcs(char s1[],char s2[]) {
	int m = strlen(s1), n = strlen(s2);
	for (int i = 1; i <= m; i++)
		c[i][0] = 0;
	for (int i = 1; i <= n; i++)
		c[0][i] = 0;

	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= n; j++) {
			if (s1[i] == s2[j])
				c[i][j] = c[i - 1][j - 1] + 1;
			else
				c[i][j] = max(c[i - 1][j], c[i][j - 1]);
		}
	}
	return c[m][n];
}
int main()
{
	char s1[maxx], s2[maxx];
	int t;
	scanf("%d", &t);
	for (int i = 0; i < t; i++) {
		scanf("%s %s", s1, s2);
		printf("%d\n", lcs(s1,s2));
	}

	return 0;
}

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

void solve(){
  int q; cin >> q;
  for(int i = 0; i < q; i++){
    string x, y; cin >> x >> y;
    int xn = x.size(), yn = y.size();
    vector<vector<int> > lcs(xn + 1, vector<int>(yn + 1));
    for(int i = 0; i <= xn; i++) lcs[i][0] = 0;
    for(int i = 0; i <= yn; i++) lcs[0][i] = 0;
    for(int j = 1; j <= xn; j++){
      for(int k = 1; k <= yn; k++){
        if(x[j - 1] == y[k - 1]) lcs[j][k] = lcs[j - 1][k - 1] + 1;
        else lcs[j][k] = max(lcs[j - 1][k], lcs[j][k - 1]);
      }
    }
    cout << lcs[xn][yn] << endl;
  }
  return;
}

int main(){
  solve();
  return 0;
}

C++: 

#include <iostream>
#include<string>
#include<algorithm>
using namespace std;
#define LEN  1001
int Position[LEN][LEN];//每个坐标都是LCS的长度
int includeGetLCS(string first, string second)
{
	int firstlen = first.size();
	int secondlen = second.size();//得到string的长度
	first = " " + first, second = " " + second;//在前面加个空格，以便下面；
	int Position[LEN][LEN];//每个坐标都是LCS的长度
	for (int i = 0; i <= secondlen; i++) Position[0][i]=0;//任意一方没有数据，LCS为0；
	for (int i = 1; i <=firstlen; i++)Position[i][0]=0;
	for (int i = 1; i <= firstlen; i++)
	{
		for (int j = 1; j <= secondlen; j++)
		{
			if (first[i] == second[j])//最后一个相等时候
				Position[i][j] = Position[i - 1][j - 1] + 1;//前面的LCS+1；
			else
			{
				if (Position[i][j - 1] >= Position[i - 1][j])//不相等的时候，比较各自减一的LCS，取最大的那个LCS
					Position[i][j] = Position[i][j - 1];
				else
					Position[i][j] = Position[i - 1][j];
			}
		}

	}
int max=0;
	if (max >= Position[firstlen][secondlen])
		return max;
	else
		return	Position[firstlen][secondlen];
}
int main()
{
	string first, second;
	int number;
	cin >> number;
	for (int i = 0; i < number; i++)
	{
		cin >> first>>second;
		cout << includeGetLCS(first, second)<<endl;
	}
}

C++: #include<iostream>
#include<vector>
#include<string>
#include<cstdio>
#include<set>
#include<algorithm>
#define ll long long
using namespace std;

int dp[1001][1001];
int main() {
	int b;
	scanf("%d", &b);
	char e[1001];
	char f[1001];
	for (int c = 0; c < b; c++) {
		scanf("%s%s", e, f);
		for (int g = 0; g <= strlen(e); g++) {
			for (int h = 0; h <= strlen(f); h++) {
				dp[g][h] = 0;
			}
		}
		for (int g = 1; g <= strlen(e); g++) {
			for (int h = 1; h <= strlen(f); h++) {
				if (e[g] == f[h]) {
					dp[g][h] = dp[g - 1][h - 1] + 1;
				}
				else {
					dp[g][h] = max(dp[g][h - 1], dp[g - 1][h]);
				}
			}
		}
		printf("%d\n", dp[strlen(e)][strlen(f)]);
	}
}

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

const int s=1001;

using namespace std;

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

	int q;
	static unsigned int dp[s][s] = {0};
	
	dp[0][0] = 0; dp[0][1] = 0; dp[1][0] = 0;

	cin >> q;
	for(int k=0; k < q; k++){
		string X,Y;
		cin >> X >> Y;
		int i,j;
		for(i=0;i<X.size();i++){
			for(j=0;j<Y.size();j++){
				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]);
			}
		}
		cout << dp[i][j] << "\n";
	}
	return 0;
}