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>
#include<string>

using namespace std;

int main(){
  string X,Y;
  int n;
  cin>>n;
  for(int m=0;m<n;m++){
  cin>>X>>Y;

  int N[X.size()+1][Y.size()+1];

  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      N[i][j]=1;
}
  }

  /*
  for(int i=0;i<X.size();i++){
    cout<<X[i]<<endl;
  }

for(int i=0;i<Y.size();i++){
    cout<<Y[i];
  }
  */

  
for(int i=0;i<=X.size();i++){
  N[i][0]=0;
  }

for(int i=0;i<=Y.size();i++){
  N[0][i]=0;
  }

 int point;

  for(int i=1;i<=X.size();i++){
    for(int j=1;j<=Y.size();j++){
      if(X[i-1]==Y[j-1]){
	point=1;
	
      }else{
	point=0;
      }
       N[i][j]=max({N[i-1][j],N[i][j-1],N[i-1][j-1]+point});
       point=0;
}
   
  }

  cout<<N[X.size()][Y.size()];
  /*
  //answer
  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      cout<<N[i][j]<<" ";
      if(j==Y.size()){
	cout<<endl;
      }
}
  }
  */

  }

}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>

#ifdef DEBUG

#define printd printf

#else

#define printd(...)

#endif


int lcs(std::string &x, std::string &y) {
    static int dp[1002][1002];

    for (int i = 0; i < x.length() + 2; i++) {
        for (int j = 0; j < y.length() + 2; j++) {
            dp[i][j] = 0;
        }
    }

    for (int i = 1; i <= x.length(); i++) {
        for (int j = 1; j <= y.length(); j++) {
            if (x[i - 1] == y[j - 1]) {
                dp[i][j] = std::max(dp[i - 1][j - 1] + 1, dp[i - 1][j]);
                dp[i][j] = std::max(dp[i][j], dp[i][j - 1]);
            } else {
                dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }

    #ifdef DEBUG
    for (int i = 1; i <= x.length(); i++) {
        for (int j = 1; j <= y.length(); j++) {
            std::cout << dp[i][j] << ", ";
        }
        std::cout << std::endl;
    }
    #endif

    return dp[x.length()][y.length()];
}

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

    for (int qi = 0; qi < q; qi++) {
        std::string x, y;
        std::cin >> x >> y;
        std::cout << lcs(x, y) << std::endl;
    }

    return 0;
}

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

constexpr int nil = -1;

class ILcs
{
public:
    using vvi = std::vector<std::vector<int>>;
    virtual int lcs() = 0;
    virtual int lcs(int i1, int i2) = 0;
};

class LcsLoop : public ILcs
{
public:

    LcsLoop(std::string str1, std::string str2)
        : str1 {str1}
        , str2 {str2}
        , dp {vvi(str1.size(), std::vector<int>(str2.size()))}
    {
        CalcLcs();
    }

    int lcs() override
    {
        return lcs(str1.size() - 1, str2.size() - 1);
    }

    int lcs(int i1, int i2) override
    {
        return dp[i1][i2];
    }

private:

    std::string str1;
    std::string str2;
    vvi         dp;
    
    void CalcLcs()
    {
        for (int i = 0; i < str1.size(); ++i)
        {
            for (int j = 0; j < str2.size(); ++j)
            {
                if (i == 0 || j == 0)
                {
                    dp[i][j] = (str1[i] == str2[j]);

                    if (i == 0 && j == 0)
                        continue;

                    if (i == 0)
                        dp[i][j] = std::max(dp[i][j], dp[i][j - 1]);
                    else
                        dp[i][j] = std::max(dp[i][j], dp[i - 1][j]);
                }
                else
                    dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]) + (str1[i] == str2[j]);
            }
        }
    }
};

class LcsReccursion : public ILcs
{
public:
    LcsReccursion(const std::string& str1, const std::string& str2)
        : str1 {str1}
        , str2 {str2}
        , dp {vvi(str1.size(), std::vector<int>(str2.size(), nil))}
    {}

    int lcs() override
    {
        return lcs(str1.size() - 1, str2.size() - 1);
    }

    int lcs(int i1, int i2) override
    {
        if (dp[i1][i2] != nil)
            return dp[i1][i2];

        if (i1 == 0 || i2 == 0)
        {
            if (i1 == 0 && i2 == 0) return dp[i1][i2] = (str1[i1] == str2[i2]);
            else if (i1 == 0)       return dp[i1][i2] = std::max(lcs(i1, i2 - 1), (str1[i1] == str2[i2] ? 1 : 0));
            else                    return dp[i1][i2] = std::max(lcs(i1 - 1, i2), (str1[i1] == str2[i2] ? 1 : 0));
        }

        dp[i1][i2] = std::max(lcs(i1 - 1, i2), lcs(i1, i2 - 1))
            + (str1[i1] == str2[i2]);
        return dp[i1][i2];
    }

private:

    std::string str1;
    std::string str2;
    vvi         dp;
};

void solve()
{
    using namespace std;

    int n;
    cin >> n;

    string s1, s2;

    for (int i = 0; i < n; ++i)
    {
        cin >> s1 >> s2;
        //LcsLoop lcs {s1, s2};
        LcsReccursion lcs {s1, s2};
        cout << lcs.lcs() << ((i != n - 1) ? "\n" : "");
    }
}

int main()
{
    solve();
    return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

#define rep(i,x) for(int i=0;i<x;++i)
#define rrep(i,x) for(int i=x-1;i>=0;--i)

int dp[1010][1010];

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

    rep(_, q) {
        string a,b; cin >> a>>b;

        int n = a.size(), m = b.size();

        rep(i, n) {
            rep(j, m) {
                if (a[i] == b[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[n][m] << endl;
    }
}

C++: #include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#define MAX_N 1000

int solve(std::string s,std::string t){
        int dp[MAX_N][MAX_N]={0};
        int n,m;
        n=s.size(),m=t.size();
        for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                        if(s[i]==t[j])
                                dp[i+1][j+1]=dp[i][j]+1;
                        else
                                dp[i+1][j+1]=std::max(dp[i][j+1],dp[i+1][j\
]);
                }
        }
        return dp[n][m];
}

int main(){
        int q;
        std::cin>>q;
        for(int i=0;i<q;i++){
                std::string s,t;
                std::cin >> s>>t;
                std::cout<<solve(s,t)<<std::endl;
        }
        return 0;
}

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

int main(){
  int n;
  cin >> n;
  for (int k=0;k<n;k++){
    string x,y;
    cin >> x >> y;
    int p=x.size();
    int q =y.size();
    int l[1000][1000] = {};
    for(int i=1;i<p+1;i++){
      for(int j=1;j<q+1;j++){
	if(x[i-1] == y[j-1] ){
	  l[i][j] = 1+l[i-1][j-1];
	}else{
	  l[i][j]= max(l[i-1][j],l[i][j-1]);
	}
      }
    }
    cout << l[p][q] << endl;
  }
}