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 <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int dp[1005][1005];

int main()
{
	ios_base::sync_with_stdio(0);
	int N; cin >> N;
	while (N-- ){
		memset (dp, 0, sizeof (dp ) );
		string s = "", t = "";
		cin >> s >> t;
		int n = s.length();
		int m = t.length();
		
		dp[0][0] = (s[0] == t[0] );

		for (int i = 1; i < n; i++ )
			if (s[i] == t[0] )
				dp[i][0] = 1;
			else
				dp[i][0] = max (dp[i][0], dp[i-1][0] );

		for (int j = 1; j < m; j++ )
			if (s[0] == t[j] )
				dp[0][j] = 1;
			else
				dp[0][j] = max (dp[0][j], dp[0][j-1] );

		for (int i = 1; i < n; i++ ){
			for (int j = 1; j < m; j++ ){
				dp[i][j] = max (dp[i][j], dp[i-1][j-1] + (s[i] == t[j] ) );
				dp[i][j] = max (dp[i][j], dp[i-1][j] );
				dp[i][j] = max (dp[i][j], dp[i][j-1] );
			} // end for
		} // end for
#if (DEBUG )
		rep (i, n ){
			rep (j, m ){
				printf ("%3d", dp[i][j] );
			} // end rep
			printf ("\n" );
		} // end rep
#endif		
		int res = 0;
		rep (i, n ) rep (j, m ) res = max (res, dp[i][j] ); 		
		cout << res << endl;
	} // end loop
		
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
const int mod = 1000000007;
const int max_n = 200005;
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main(){
    ll n;cin >> n;
    rep(i,n){
        string s,t;cin >>s >>t;
        vvl dp(s.size()+1,vl(t.size()+1,0));
        rep(j,s.size()){
            rep(k,t.size()){
                if(s[j]==t[k]){
                    dp[j+1][k+1]=dp[j][k]+1;
                }
                else{
                    dp[j+1][k+1]=max(dp[j][k+1],dp[j+1][k]);
                }
            }
        }
        cout << dp[s.size()][t.size()] <<endl;
    }
}

C++: #include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

int LCS(string s1, string s2){
    int l1 = s1.size();
    int l2 = s2.size();

    int dp[l1 + 1][l2 + 1];
    for(int i = 0; i < l1 + 1; i++){
        for(int j = 0; j < l2 + 1; j++){
            if(i == 0 || j == 0){
                dp[i][j] = 0;
            }
            else if(s1[i - 1] == s2[j - 1]){
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }else{
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }

    return dp[l1][l2];
}

int main(void){
    int n;
    scanf("%d", &n);

    for(int i = 0; i < n; i++){
        string s1, s2;
        cin >> s1;
        cin >> s2;
        printf("%d\n", LCS(s1, s2));
    }


    return 0;
}

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

using namespace std;

template <class T>
T Vec(T v) { return v; }

template <class T, class... Ts>
auto Vec(size_t l, Ts... ts) {
    return vector<decltype(Vec<T>(ts...))>(l, Vec<T>(ts...));
}

int main() {
    int Q;
    cin >> Q;
    for (int q = 0; q < Q; ++q) {
        string S, T;
        cin >> S >> T;
        int N = S.length(), M = T.length();
        S.insert(S.begin(), '$');
        T.insert(T.begin(), '$');

        auto dp = Vec<int>(N + 1, M + 1, 0);
        for (int i = 1; i <= N; ++i) {
            for (int j = 1; j <= M; ++j) {
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
                if (S[i] == T[j]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
            }
        }
        cout << dp[N][M] << endl;
    }
    return 0;
}

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

int C[1001][1001];
string N1, N2;

int lcs()
{
	int m = N1.size();
	int n = N2.size();
	int maxl = 0;

	N1 = " " + N1;
	N2 = " " + N2;

	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 (N1[i] == N2[j])
				C[i][j] = C[i - 1][j - 1] + 1;
			else
				C[i][j] = max(C[i - 1][j], C[i][j - 1]);

			maxl = max(maxl, C[i][j]);
		}
	}

	return maxl;
}

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

	int q;

	cin >> q;
	for (int i = 0; i < q; i++)
	{
		cin >> N1 >> N2;
		cout << lcs() << endl;
	}
		
	return 0;                                                                            
}

C++: #include <iostream>
#include <stdio.h>
#include <vector>
#include <utility>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstring>
 
typedef long long ll;

#define rep(i,n) for(ll i = 0; i < n; i++)

using namespace std;
 
const ll INF = ll(1e18)+1;
const double PI=3.14159265358979323846;

int dp[1010][1010];

int main(){
  int N;
  cin >> N;
  rep(i,N){
    string x,y;
    cin >> x >> y;

    rep(i,x.size()){
      rep(j,y.size()){
	if(x[i] == y[j])
	  dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j]+1);
	else{
	  dp[i+1][j+1] = max(dp[i+1][j+1], dp[i+1][j]);
	  dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j+1]);	  
	}
      }
    }
    cout << dp[x.size()][y.size()] << endl;
    memset(dp, 0, sizeof dp);
  }
}