Go: package main

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

//Util
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

func absInt(a int) int {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func absFloat64(a float64) float64 {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func minFloat64(a, b float64) float64 {
	if a > b {
		return b
	} else {
		return a
	}
}

func str2Int(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}

//Main
var sc = bufio.NewScanner(os.Stdin)
var q int
var X, Y string

func lcs(X, Y string) int {
	lenX := len(X)
	lenY := len(Y)
	dp := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		dp[i] = make([]int, lenY+1)
	}

	res := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if X[i-1] == Y[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
			}
			//fmt.Println(dp)
			res = maxInt(res, dp[i][j])
		}

	}
	return res
}

func main() {
	sc.Split(bufio.ScanWords)
	q = nextInt()

	for i := 0; i < q; i++ {
		X = nextStr()
		Y = nextStr()

		fmt.Println(lcs(X, Y))
	}
}

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;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#define FOR(i,b,e) for(int i=b;i<e;i++)
#define REP(i,n) FOR(i,0,n)
using namespace std;

int dp[1001][1001];
string s1, s2;

int lcs(int n1, int n2) {
  if (dp[n1][n2] != -1) return dp[n1][n2];
  
  dp[n1][n2] = (s1[n1-1] == s2[n2-1]) ? (1 + lcs(n1 - 1, n2 - 1)) : max(lcs(n1 - 1, n2), lcs(n1, n2 - 1));
  return dp[n1][n2];
}

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


  int q; cin >> q;
  REP(i, q) {
    REP(i, 1001) {
      REP(j, 1001) {
        dp[i][j] = -1;
      }
    }
    REP(i, 1001) dp[i][0] = 0;
    REP(i, 1001) dp[0][i] = 0;

    cin >> s1;
    cin >> s2;

    cout << lcs(s1.length(), s2.length()) << endl;
  }
}