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++: #pragma region Macros
#include <bits/stdc++.h>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())
#define IN(x, a, b) x >= a and x < b

using namespace std;
using ll = long long;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> constexpr T INF = numeric_limits<T>::max() / 2;
#pragma endregion

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

  int n;
  cin >> n;
  REP(_, n) {
    string a, b;
    cin >> a >> b;
    vector<vector<int>> dp(a.size() + 1, vector<int>(b.size() + 1, 0));
    REP(i, a.size()) {
      REP(j, b.size()) {
        if (a[i] == b[j]) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        }
        chmax(dp[i + 1][j + 1], dp[i][j + 1]);
        chmax(dp[i + 1][j + 1], dp[i + 1][j]);
      }
    }
    cout << dp[a.size()][b.size()] << endl;
  }

  return 0;
}

C++: #include<iostream>

using namespace std;

int q;

string x, y;

int dp(string x, string y){
  int longest[1051][1051];
  for(int j = 1; j <= y.size(); ++j){
    for(int i = 1; i <= x.size(); ++i){
      if(i <= 0 || j <= 0){
        longest[i][j] = 0;
      }else if(x[i - 1] == y[j - 1]){
        longest[i][j] = 1 + longest[i-1][j-1];
      }else{
        longest[i][j] = max(longest[i][j -1], longest[i - 1][j]);
      }
    }
  }
  //cout << x.size() << endl;
  return longest[x.size()][y.size()];
}

int main(){
  cin >> q;
  while(q != 0){
    q--;
    cin >> x;
    cin >> y;
    
    cout << dp(x, y) << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
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[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(){
    string  s1,s2;
    int n; cin>>n;
    for(int i=0;i<n;i++){
        cin>>s1>>s2;
        cout<<lcs(s1,s2)<<'\n';
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define inf 1e9+7

int dp[1001][1001];
char x[1001],y[1001];

void init(){
    for(int i=0;i<1001;i++){
        dp[0][i]=0;
        dp[i][0]=0;
    }
}

void solve(int sn, int tn){
    for(int i=1;i<=sn;i++){
        for(int j=1;j<=tn;j++){
            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]);
        }
    }
}

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

    int n;
    cin >> n;
    string s,t;
    for(int i=0;i<n;i++){
        init();
        cin >> s >> t;
        int sn=s.size();
        int tn=t.size();
        for(int j=0;j<sn;j++){
            x[j]=s[j];
        }
        for(int j=0;j<tn;j++){
            y[j]=t[j];
        }

        solve(sn, tn);
        cout << dp[sn][tn] << endl;
    }

}

C++: #include <iostream>
#include <algorithm>
#include <stack>
#include <string>
#include <queue>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <string.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <map>
#define INFTY (1<<30)
#define MAX 100000000

using namespace std;

int lcs(string x, string y) {
	int m = x.size(), n = y.size();
	int c[1000+1][1000+1];
	x = ' ' + x; y = ' ' + y;
	for( int i = 0; i <= m; i++) c[i][0] = 0;
	for( int j = 0; j <= n; j++) c[0][j] = 0;
	for( int i = 1; i <= m; i++) {
		for( int j = 0; j <= n; j++) {
			if(x[i] == y[j]) c[i][j] = c[i-1][j-1];
			else if(c[i-1][j] >= c[i][j-1]) c[i][j] = c[i-1][j];
			else c[i][j] = c[i][j-1];
		}
	}
	return c[m][n];
}

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

	return 0;
}

C++: //============================================================================
// Name        : LongestCommonSeq.cpp
// Author      : Ken
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
using namespace std;

class DPMatrix{
public:
	vector<vector<int> > matrix;
	string seq1;
	string seq2;

	DPMatrix(const string sequence1, const string sequence2):matrix(),seq1(sequence1), seq2(sequence2){
		for(int i=0; i<sequence1.size()+1; i++){
			vector<int> vec(sequence2.size()+1);
			matrix.push_back(vec);
		}
	}

	void update(int i, int j){
		if(i<1 or j<1){
			cout << "caution! i: " << i << ", j: " << j << "\n";
		}

		if(seq1.at(i-1) == seq2.at(j-1)){
			matrix.at(i).at(j) = max(matrix.at(i).at(j-1), max(matrix.at(i-1).at(j), matrix.at(i-1).at(j-1)+1));
		}else{
			matrix.at(i).at(j) = max(matrix.at(i).at(j-1), max(matrix.at(i-1).at(j), matrix.at(i-1).at(j-1)));
		}
	}

	int fillMatrix(){
		for(int i=1; i<seq1.size()+1; i++){
			for(int j=1; j<seq2.size()+1; j++){
				update(i, j);
			}
		}

		return matrix.at(seq1.size()).at(seq2.size());
	}
};

int main() {
	int n;
	string sequence1, sequence2;
	cin >> n;

	for(int i=0; i<n; i++){
		cin >> sequence1;
		cin >> sequence2;

		DPMatrix dpmat(sequence1, sequence2);
		int score = dpmat.fillMatrix();

		cout << score << "\n";
	}

	return 0;
}

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

int LCS(std::string str1, std::string str2, int i, int j){
  if(str1[i] == str2[j]){
    return 1;
  }
  else{
    return 0;
  }
}

int max(int a, int b, int c){
  int m = a;
  if(b > m){
    m = b;
  }
  if(c > m){
    m = c;
  }
  return m;
}

int main(){
  int q;
  std::cin >> q;
  //int ans[q];
  std::string str1, str2;
  for(int z=0; z<q; z++){
    std::cin >> str1 >> str2;
    int l1 = str1.size();
    int l2 = str2.size();
    int DP[l1+1][l2+1];

    for(int i=0; i<=l1; i++){
      DP[i][0] = 0;
    }
    for(int j=0; j<=l2; j++){
      DP[0][j] = 0;
    }

    for(int i=1; i<=l1; i++){
      for(int j=1; j<=l2; j++){
	DP[i][j] = max(DP[i-1][j], DP[i][j-1], DP[i-1][j-1] + LCS(str1, str2, i-1, j-1));
      }
    }
    //ans[z] = DP[l1][l2];
    std::cout << DP[l1][l2] << std::endl;
  }

  /*for(int z=0; z<q; z++){
    std::cout << ans[z] << std::endl;
    }*/
  return 0;
}