Go: package main

import "fmt"

func initialize_grid(width int,height int)[][]int{
	var grid [][]int
	for i:=0;i<height+1;i++{
		var grid_row  []int
		for j:=0;j<width+1;j++{
			grid_row = append(grid_row,0)
		}
		grid = append(grid,grid_row[:])
	}
	return grid
}

func ma(x int,y int)int{
	if x >= y{
		return x
	}else{
		return y
	}
}

func main()  {
	//Take input
	var num int
	var st1,st2 string
	var string_pairs [][]string
	fmt.Scan(&num)
	for i:=0;i<num;i++{
		var string_pair []string
		fmt.Scan(&st1)
		fmt.Scan(&st2)
		string_pair = append(string_pair,st1,st2)
		string_pairs = append(string_pairs,string_pair)
	}
	//DP
	//initialize grid
	for i:=0;i<num;i++{
		string_1 := string_pairs[i][0]
		string_2 := string_pairs[i][1]

		dp := initialize_grid(len(string_2),len(string_1))

		for j:=1;j<len(string_1)+1;j++{
			for k:=1;k<len(string_2)+1;k++{
				if string_1[j-1] == string_2[k-1]{
					dp[j][k] = dp[j-1][k-1] + 1
				}else{
					dp[j][k] = ma(dp[j-1][k],dp[j][k-1])
				}
			}
		}
		fmt.Println(dp[len(string_1)][len(string_2)])
	}
}

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

#define REP(i, n) for(int i = 0; i < (n); i++)

string str1, str2;
int dp[1001][1001];
int k;

int solve(int i, int j)
{
  if(dp[i][j] > 0)
    return dp[i][j];
  if(i == str1.size() || j == str2.size()){
    if(i != str1.size()){
      return solve(i + 1, k);
    }else{
      return 1;
    }
  }
  if(str1[i] == str2[j]){
    k = j + 1;
    dp[i][j] = solve(i + 1, j + 1);
    return dp[i][j] + 1;
  }else{
    return solve(i, j + 1);
  }
}

int main()
{
  int q, n, ans = 0;
  cin >> q;
  cin.ignore();
  fill(&dp[0][0], &dp[1001][0], -1);
  REP(i, q){
    getline(cin, str1);
    getline(cin, str2);
    if(str1.size() < str2.size())
      swap(str1, str2);
    REP(i, str1.size()){
      n = solve(i, 0) - 1;
      ans = (n > ans) ? n : ans;
    }
    cout << ans << endl;
    ans = 0;
  }
}

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()+1) {
            rep(j,0,Y.size()+1) {
                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;
}