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<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();
  
  for (int i = 0; i < m; i++) c[i][0] = 0;
  for (int i = 0; i < n; i++) c[0][j] = 0;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (X[i] == Y[j]) 
        if (i == 0 || j == 0) 
          c[i][j] = 1;
        else 
          c[i][j] = c[i - 1][j - 1] + 1;
      else
        c[i][j] = max(c[i][j - 1], c[i - 1][j]);
    }
  }
  return c[m - 1][n - 1];
}

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 <stdio.h>
#include <vector>
#include <math.h>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        string s, t;
        cin >> s >> t;
        int dp[1001][1001] = {};
        for (int j = 0; j < s.length(); ++j) {
            for (int k = 0; k < t.length(); ++k) {
                if (s[j] == t[k]) {
                    dp[j + 1][k + 1] = max(dp[j + 1][k + 1], dp[j][k] + 1);
                }
                dp[j+1][k+1] = max(dp[j+1][k+1], dp[j+1][k]);
                dp[j+1][k+1] = max(dp[j+1][k+1], dp[j][k+1]);
            }
        }
        cout << dp[s.length()][t.length()] << endl;

    }
    return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int p[1001][1001]={0};
int main(){
int i,j,q,e,l,z,m,u,n;
char s[2001],c[2001];
cin>>q;
for(e=0;e<q;e++){
scanf("%s %s",s,c);
l=strlen(s);
z=strlen(c);

for(i=0;i<=l;i++){
	for(j=0;j<=z;j++){
		p[i][j]=0;
	}
}
for(i=1;i<=z;i++){
	for(j=1;j<=l;j++){
		if(c[i-1]==s[j-1])p[j][i]=p[j][i-1]+1;
		else p[j][i]=max(p[j][i-1],p[j][i-1]);
	}
}
cout<<p[l][z]<<endl;
}
return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int main(void){
    int n;
    cin >> n;
    for(int i = 0;i < n;i++){
        string X,Y;
        cin >> X >> Y;
        int L[X.length()+10][Y.length()+10];
        for(int j = 0;j < X.length()+5;j++){
            for(int k = 0;k < Y.length()+5;k++){
                L[j][k] = 0;
            }
        }
        for(int j = 1;j<=X.length();j++){
            for(int k = 1;k<=Y.length();k++){
                if(Y[k-1] == X[j-1]){
                    L[j][k] = 1+L[j-1][k-1];
                }
                else{
                    L[j][k] = max(L[j-1][k],L[j][k-1]);
                }
            }
        }
        cout << L[X.length()][Y.length()] << endl;
    }
    return 0;
}

C++: #include "iostream"
#include "string.h"

using namespace std;

int L[1001][1001];

int lcs( char s1[], char s2[], int l1, int l2 ) {

	if (L[l1][l2] != 0)return L[l1][l2];

	if (l1 == 0 || l2 == 0) return 0;
	else if (s1[l1-1] == s2[l2-1]) L[l1][l2] = lcs(s1, s2, l1 - 1, l2 - 1) + 1;
	else {
		int lsc1 = lcs(s1, s2, l1 - 1, l2);
		int lsc2 = lcs(s1, s2, l1, l2 - 1);

		if (lsc1 > lsc2) L[l1][l2] = lsc1;
		else L[l1][l2] = lsc2;
	}

	return L[l1][l2];
}

int main() {

	int n;
	char s1[1000], s2[1000];

	cin >> n;

	int ans[150];

	for (int i = 0; i < n; i++) {
		cin >> s1;
		cin >> s2;

		ans[i] = lcs(s1, s2, strlen(s1), strlen(s2));

		for (int j = 0; j < strlen(s1); j++) {
			for (int k = 0; k < strlen(s2); k++) {
				L[j][k] = 0;
			}
		}
	}

	for (int i = 0; i < n; i++) {
		cout << ans[i] << endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <cstdint>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <array>
#include <vector>
#include <unordered_map>

int lcs(const std::string& x, const std::string& y)
{
    int c[x.size()][y.size()] = {0};
    for (int i = 0; i < x.size(); ++i) {
        for (int j = 0; j < y.size(); ++j) {
            if (i == 0) {
                if (j == 0) {
                    if (x[i] == y[j]) {
                        c[i][j] = 1;
                    } else {
                        c[i][j] = 0;
                    }
                } else {
                    if (x[i] == y[j]) {
                        c[i][j] = 1;
                    } else {
                        c[i][j] = c[i][j-1];
                    }
                }
            } else if (j == 0) {
                if (i == 0) {
                    if (x[i] == y[j]) {
                        c[i][j] = 1;
                    } else {
                        c[i][j] = 0;
                    }
                } else {
                    if (x[i] == y[j]) {
                        c[i][j] = 1;
                    } else {
                        c[i][j] = c[i-1][j];
                    }
                }
            } else {
                if (x[i] == y[j]) {
                    c[i][j] = c[i-1][j-1] + 1;
                } else {
                    c[i][j] = std::max(c[i][j-1], c[i-1][j]);
                }
            }
        }
    }

    return c[x.size() -1][y.size()-1];
}

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

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

    return 0;
}

C++: #include <iostream>

using namespace std;

int lcs(string X, string Y) {
  int memo[1010][1010];
  int maxl = 0;
  int k = 0;
  for (int i = 0; i < X.size(); i++) {
    for (int j = 0; j < Y.size(); j++) {
      if (i <= 0 || j <= 0) {
        if (X[i] == Y[j]) {
          memo[i][j] = 1;
        } else {
          memo[i][j] = 0;
        }
      }
      else {
        if (X[i] == Y[j]) {
          memo[i][j] = memo[i-1][j-1] + 1;
        } else {
          memo[i][j] = max(memo[i-1][j], memo[i][j-1]);
        }
      }
      maxl = max(maxl, memo[i][j]);
      //cout << i << "," << j << "," << X[i] << "," << Y[j] << "," << maxl << endl;
    }
  }
  return maxl;
}

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