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 <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
char A[N],B[N];
int dp[N][N];
void lcs(char A[],char B[]){
    int lenA = strlen(A+1);
    int lenB = strlen(B+1);
    
    for(int i=0;i<=lenA;i++){
        dp[i][0] = 0;
    }
    for(int i=0;i<lenB;i++){
        dp[0][i] = 0;
    }
    for (int i=1; i<=lenA; i++) {
        for(int j=1;j<=lenB;j++){
            if(A[i] == B[j]){
                dp[i][j] = dp[i-1][j-1] + 1;
            }
            else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
    }
    printf("%d\n",dp[lenA][lenB]);
}

int main(){
    int n;
    scanf("%d",&n);
    for (int i=0; i<n; i++) {
        //从下标为1开始读入
        gets(A+1);
        gets(B+1);
        lcs(A, B);
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(i, n) for(int i=0; i<=n; i++)
#define repr(i, n) for(int i=n; i>0; i--)
#define REPR(i, n) for(int i=n; i>=0; i--)
#define FOR(i, a, b) for(int i=a; i<b; i++)
#define ALL(v) v.begin(),v.end()
#define pb push_back
#define eb emplace_back
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = 1e9+7;
template<class T> void puts(T x) { std::cout << x << std::endl; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int q;

int mmax(int a, int b, int c) {
    int ret;
    ret = max(a, b);
    ret = max(ret, c);
    return ret;
}

int lcs(string s1, string s2) {
    int n1 = s1.length();
    int n2 = s2.length();

    int dp[n1][n2];
    dp[0][0] = s1[0] == s2[0];
    for (int i = 1; i < n1; i++) {
        dp[i][0] = s1[i] == s2[0] ? 1 : dp[i-1][0];
    }
    for (int i = 1; i < n2; i++) {
        dp[0][i] = s2[i] == s1[0] ? 1 : dp[0][i-1];
    }

    for (int i = 1; i < n1; i++) {
        for (int j = 1; j < n2; j++) {
            int t = s1[i] == s2[j];
            dp[i][j] = mmax(dp[i-1][j-1] + t,
                            dp[i-1][j],
                            dp[i][j-1]);
        }
    }

    return dp[n1-1][n2-1];
}

int main() {
    cin >> q;
    rep(i, q) {
        string s1, s2; cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
}

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

int lcs(string X, string Y) {
  int cs[1001][1001];
  int m = X.size();
  int n = Y.size();
  int answer = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  for (int i = 1; i <= m; i++) cs[i][0] = 0;
  for (int j = 1; j <= n; j++) cs[0][j] = 0;

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

  return answer;
}

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

  int n;
  cin >> n;

  string X, Y;
  int answers[n];

  for (int i = 0; i < n; i++) {
    cin >> X >> Y;
    answers[i] = lcs(X, Y);
  }

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

  return 0;
}

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

using namespace std;
static const int N = 100;

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 = 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 (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);
		cout << endl;
	}
	return 0;
}

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

typedef vector<int> vi;

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

	string x, y;
	int t[1001][1001];

	while(n--) {
		cin >> x >> y;
		int xs = x.size(), ys = y.size();
		for (int i = 1; i <= xs; i++)
			for (int j = 1; j <= ys; j++) {
				t[i][j] = x[i - 1] == y[j - 1] ?
					t[i - 1][j - 1] + 1 :
					max(t[i - 1][j], t[i][j - 1]);
			}
		cout << t[xs][ys] << endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#define _A_ true
using namespace std;
 
int main(){
  int n,AC=0,strA,strB;
  int dp[1010][1010];
  char *A = new char[1500];
  char *B = new char[1500];
 
  scanf("%d",&n);
  while(_A_){
    if(AC == n)break;
    scanf("%s",A);
    scanf("%s",B);
    for(int i=0;i<1010;i++)for(int j=0;j<1010;j++)dp[i][j]=0;
strA = strlen(A); 
strB = strlen(B);

    for(int i=0;i<strA;i++){
      for(int j=0;j<strB;j++){
    if(A[i] == B[j]){
      dp[i+1][j+1]=dp[i][j]+1;
    }
    else{
      if(dp[i+1][j] > dp[i][j+1]){
        dp[i+1][j+1]=dp[i+1][j];
      }
      else {
        dp[i+1][j+1] = dp[i][j+1];
      }
    }
      }
    }
    printf("%d\n",dp[strA][strB]);
    AC++;
  }
  delete [] A;
  delete [] B;
 
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
char A[N],B[N];
int dp[N][N];
void lcs(char A[],char B[]){
    int lenA = strlen(A+1);
    int lenB = strlen(B+1);
    
    for(int i=0;i<=lenA;i++){
        dp[i][0] = 0;
    }
    for(int i=0;i<lenB;i++){
        dp[0][i] = 0;
    }
    for (int i=1; i<=lenA; i++) {
        for(int j=1;j<=lenB;j++){
            if(A[i] == B[j]){
                dp[i][j] = dp[i-1][j-1] + 1;
            }
            else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
    }
    printf("%d\n",dp[lenA][lenB]);
}

int main(){
    int n;
    scanf("%d",&n);
    for (int i=0; i<n; i++) {
        //从下标为1开始读入
        lcs(gets(A+1),gets(B+1));
    }
}

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

using namespace std;
 
 
int main(){
 
  string str1;
  string str2;
  int n,s1,s2;
  int c[1001][1001] = {{}};
  int max = 0;
 
  cin >> n;
 
  for(int i = 0;i < n;i++){
 
    cin >> str1 >> str2;
 
    str1 = ' ' + str1;
    str2 = ' ' + str2;
 
 
    s1 = str1.size();
    s2 = str2.size();
 
    for(int j = 1;j < s1;j++){
      for(int k = 1;k < s2;k++){
    if(str1[j] == str2[k]){
      c[j][k] = c[j - 1][k - 1] + 1;
      if(c[j][k] > max) max = c[j][k];
    }
    else if(c[j - 1][k] > c[j][k - 1]){
      c[j][k] =  c[j - 1][k];
    }
    else {
      c[j][k] = c[j][k - 1];
    }
      }
    }
 
 
    cout << max << endl;
       
    max = 0;
 
    for(int j = 0;j <= s1;j++){
      for(int k = 0;k <= s2;k++){
        c[j][k] = 0;
      }
    }
  }
}