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<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0;i < n;i++)
#define repr(i, n) for(int i = n;i >= 0;i--)
#define frep(i, m, n) for(int i = m;i < n;i++)
#define frepr(i, m, n) for(int i = m;i >= n;i--)
#define vsort(v) sort(v.begin(),v.end())
#define ll long long
#define inf 999999999
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<string> vst;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef pair<int, P> PP;
typedef pair<ll, LP> LPP;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

int main(){
    int q;
    cin >> q;
    rep(p,q){
       string x,y;
       cin >> x >> y;
       vvi dp(x.length(),vi(y.length(),inf));
       rep(i,x.length()){
           rep(j,y.length()){
               if(i==0&&j==0)dp[i][j]=(x[i]==y[j]);
               else if(i==0)dp[i][j]=max(dp[i][j-1],(int)(x[i]==y[j]));
               else if(j==0)dp[i][j]=max((int)(x[i]==y[j]),dp[i-1][j]);
               else if(x[i]==y[j])dp[i][j]=1+dp[i-1][j-1];
               else dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
            //    cout << dp[i][j];
           }
        //    cout << endl;
       }
       cout << dp[x.length()-1][y.length()-1] << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  while(n--){
  string  a,b;
  long n=0,i,c=0;
  cin>>a>>b;
  for(i=0;i<a.size();i++)if(a[i]==b[c])if(++n)c++;
  cout<<n<<endl;}
}

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

using namespace std;

static const int N = 1000;

int lcs(string X, string Y) {
  int c[N + 1][N + 1];
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  for (int i = 0; i <= m; i++) {
    c[i][0] = 0;
  }
  for (int j = 1; j <= n; j++) {
    c[0][j] = 0;
  }

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[i]) {
        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() {
  string s1, s2;
  int n;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1 >> s2;
    cout << lcs(s1, s2) << endl;
  }

  return 0;
}

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

int lcs(string s1, string s2)
{
	int m = s1.size();
	int n = s2.size();
	int C[MAXN + 1][MAXN + 1];
	C[0][0] = 0;
	s1 = ' ' + s1;
	s2 = ' ' + s2;
	int max_lcs = 0;
	for (int i = 1; i <= m; i++)
		C[i][0] = 0;
	for (int j = 1; j <= n; j++)
		C[0][j] = 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]);
			max_lcs = max(max_lcs, C[i][j]);
		}
	}
	return max_lcs;
}

int main()
{
	string s1, s2;
	int q;
	cin >> q;
	while (q--)
	{
		cin >> s1 >> s2;
		cout << lcs(s1, s2) << endl;
	}
	return 0;
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int LCS[1000][1000];

void solve(string x, string y)
{
	static const int offset[3][2] = { {0,0},{1,0},{0,1} };
	for (int i = 1; i != (max(x.size(), y.size())) + 1; i++)
	{
		for (int k = 0; k < 3; k++)
		{
			int xi = min(x.size() - 1, i - 1 + offset[k][0]);
			int yi = min(y.size() - 1, i - 1 + offset[k][1]);
			if (x[xi] == y[yi])
			{
				LCS[xi + 1][yi + 1] = LCS[xi][yi] + 1;
			}
			else
			{
				LCS[xi + 1][yi + 1] = max(LCS[xi][yi + 1], LCS[xi + 1][yi]);
			}
		}
	}

	cout << LCS[x.size()][y.size()] << endl;
}

int main()
{
	int q;
	cin >> q;

	for (int i = 0; i < 1000; i++)
	{
		LCS[0][i] = 0;
		LCS[i][0] = 0;
	}

	for (int i = 0; i < q; i++)
	{
		string x, y;
		cin >> x >> y;
		solve(x, y);
	}
	return 0;
}