Go: package main

import (
	"fmt"
)

func main() {
	var q int
	fmt.Scan(&q)

	for i := 0; i < q; i++ {
		var x, y string
		fmt.Scan(&x)
		fmt.Scan(&y)

		xs := []rune(x)
		ys := []rune(y)

		xn := len(xs)
		yn := len(ys)
		dp := make([][]int, xn+1)
		for j := 0; j <= xn; j++ {
			dp[j] = make([]int, yn+1)
		}

		for j := 1; j <= xn; j++ {
			for k := 1; k <= yn; k++ {
				dp[j][k] = max(dp[j-1][k], dp[j][k-1])
				if xs[j-1] == ys[k-1] {
					dp[j][k] = max(dp[j][k], dp[j-1][k-1] + 1)
				} else {
					dp[j][k] = max(dp[j][k], dp[j-1][k-1])
				}
			}
		}
		fmt.Println(dp[xn][yn])
	}
}

func max(a, b int) int {
	if a < b {
		return b
	} else {
		return a
	}
}

C++: #include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
//#include <unordered_map> //???????????\??¢??°
#include <fstream> //ifstream, ofstream

//#define NDEBUG //NDEBUG???#include <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG?????????????????????????????????????????????
#include <cassert> //assert


using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???                           ????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#define din cin
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout dummy.str(""); dummy.clear(stringstream::goodbit); dummy //dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§???????????????????????????????????? http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUTFILE????????????????????¬???????????????????????????????????¢????????????
//#define OUTPUTFILE "output.txt" //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/output.txt"
#endif

//?¨??????\???????????????????????????????????????????????´??????INPUTFROMTEXTFILE????????????????????¬???????????????????????????????????¢????????????
//#define INPUTFILE "input.txt" //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif


#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A) dout << setw(3) << (A) << " " // << setw(3) ??????????????\????????????
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef unsigned long long ll;


const int INF = 999;//1e9-1;

int dp[1001][1001] = {0};
//pii from[1001][1001];

string X, Y;



int solve(int i, int j) {
//    disp(i); disp(j);
    
    int ans;
    
    if(i==0 or j==0) return dp[i][j] = 0;
    
    if(dp[i][j]!=0) return dp[i][j];
    
    if(X[i-1]==Y[j-1]) {
        ans = solve(i-1,j-1) + 1;
//        from[i][j] = make_pair(i-1, j-1);
    } else {
        if( solve(i-1,j) > solve(i,j-1) ) {
            ans = solve(i-1,j);
//            from[i][j] = make_pair(i-1, j);
        } else {
            ans = solve(i,j-1);
//            from[i][j] = make_pair(i, j-1);
        }
    }
    
    dp[i][j] = ans;

//    //test display
//    dout<<"-------------------------------------\n";
//    dout<<"                ";
//    rep(j,0,Y.size()) disP(j); dout<<endl;
//    dout<<"                ";
//    rep(j,0,Y.size()) disP(Y[j]); dout<<endl;
//    rep(i,0,X.size()) {
//        disP(i); disP(X[i]); dispAll(dp[i], Y.size());
//    }
    
    return ans;
}


int main() {
    int q; din >> q;
    rep(i,0,q) {
        din >> X >> Y;
//        disp(X); disp(Y);
        dout<<solve(X.size(),Y.size())<<endl;

        rep(i,0,X.size()) {
            rep(j,0,Y.size()) {
                dp[i][j]=0;
//                from[i][j] = make_pair(-1, -1);
            }
        }
        
//        //initialize
//        rep(i,0,X.size()+1) {
//            rep(j,0,Y.size()+1) {
//                dp[i][j] = 0;
//                from[i][j] = make_pair(-1, -1);
//            }
//        }
//        
//        rep(i,1,X.size()+1) {
//            rep(j,1,Y.size()+1) {
//                if(X[i-1]==Y[j-1]) {
//                    dp[i][j] = dp[i-1][j-1] + 1;
//                }
//                else {
//                    dp[i][j] = max( dp[i-1][j], dp[i][j-1]);
//                }
//            }
//        }
//        
////            //test display
////            dout<<"-------------------------------------\n";
////            dout<<"                ";
////            rep(j,0,Y.size()+1) disP(j); dout<<endl;
////            dout<<"                    ";
////            rep(j,1,Y.size()+1) disP(Y[j-1]); dout<<endl;
////            rep(i,0,X.size()+1) {
////                if(i==0) {
////                    disP(i); disP(0); dispAll(dp[i], Y.size()+1);
////                } else {
////                    disP(i); disP(X[i-1]); dispAll(dp[i], Y.size()+1);
////                }
////            }
//        
//        dout<<dp[X.size()][Y.size()]<<endl;
        
    }
    
    
    
#ifdef INPUTFILE
    inputfile.close();
#endif
    
#ifdef OUTPUTFILE
    outputfile.close();
    cout << "\"" << OutputFilePath << "\"" << endl;
#endif
    
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<climits>
#include<set>


using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef pair<ll, ll> pll;
int dx[4] = { 1,0,0,-1 };
int dy[4] = { 0,1,-1,0 };

#define ARRAY_MAX 1005

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;

int dp[ARRAY_MAX][ARRAY_MAX];


int main() {

	int n;
	cin >> n;
	for (int tm = 0; tm < n; tm++)
	{
		string s, t;
		cin >> s >> t;
		int s_siz = s.size();
		int t_siz = t.size();

		for (int i = 0; i < s_siz; i++) {
			for (int j = 0; j < t_siz; j++) {
				if (s[i] == t[j]) {
					dp[i + 1][j + 1] = dp[i][j] + 1;
				}
				else {
					dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
				}
			}
		}
		cout << dp[s_siz][t_siz] << endl;

	}

	return 0;
}