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;

string a, b;
int q;
// iまで、jまででの答え
int dp[1005][1005] = {0};

int solve();

int main() {
  cin >> q;
  for(int qq = 0; qq < q; ++qq) {
    cin >> a >> b;
    cout << solve() << endl;
    for(int i = 1; i <= a.size(); ++i)
      for(int j = 1; j <= b.size(); ++j) dp[i][j] = 0;
  }
  return 0;
}

int solve() {
  for(int i = 1; i <= a.size(); ++i)
    for(int j = 1; j <= b.size(); ++j) {
      dp[i][j] =
          max(dp[i][j - 1],
              max(dp[i - 1][j - 1] + (a[i - 1] == b[j - 1]),
                  dp[i - 1][j]));
    }
  return dp[a.size()][b.size()];
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <set>
#include<algorithm>
#include <iomanip>
int ctoi(char c) {
	switch (c) {
	case '0': return 0;
	case '1': return 1;
	case '2': return 2;
	case '3': return 3;
	case '4': return 4;
	case '5': return 5;
	case '6': return 6;
	case '7': return 7;
	case '8': return 8;
	case '9': return 9;
	default: return 0;
	}
}
using namespace std;
typedef long long ll;
//定義場所//
int i, j, k, l, m, n;
int N, Q;
vector <int> Ans;
////////////

int main()
{
	int N;
	cin >> N;
	for (k = 0; k < N; k++) {
		vector<vector<int> >dp(1001, vector<int>(1001, 0));
		int ans = 0;
		string s1, s2;
		cin >> s1 >> s2;
		s1 = ' ' + s1;
		s2 = ' ' + s2;
		for (i = 0; i < s1.size(); i++) { dp[i][0] = 0; }
		for (i = 1; i < s2.size(); i++) { dp[0][i] = 0; }
		for (i = 1; i < s1.size(); i++) {
			for (j = 1; j < s2.size(); j++) {
				if (s1[i] == s2[j]) {
					dp[i][j] = dp[i - 1][j - 1] + 1;
				}
				else {
					dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
				}
				ans = max(ans, dp[i][j]);
			}
		}
		Ans.push_back(ans);
	}
	for (i = 0; i < Ans.size(); i++) {
		cout << Ans[i] << 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;

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

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int (i)=(a);(i)<(n);++(i))
#define eFOR(i,a,n) for(int (i)=(a);(i)<=(n);++(i))
#define rFOR(i,a,n) for(int (i)=(n)-1;(i)>=(a);--(i))
#define erFOR(i,a,n) for(int (i)=(n);(i)>=(a);--(i))
#define each(i, a) for(auto &i : a)
#define SORT(i) sort((i).begin(),(i).end())
#define rSORT(i,a) sort((i).begin(),(i).end(),(a))
#define all(i) (i).begin(),(i).end()
#define out(y,x) ((y) < 0 || h <= (y) || (x) < 0 || w <= (x))
#define line cout << "------------------------\n" 
#define ENDL(i,n) ((i) == (n) - 1 ? "\n" : " ")
//#define stop system("pause") //comment out this on AOJ.
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10; //1e-9?
constexpr ld pi = 3.1415926535897932;
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; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& elemnt : v)is >> elemnt; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }

void solve() {
    string s, t; cin >> s >> t;
    int n = s.length(), m = t.length();
    auto dp = vec<int>(n + 1, m + 1);
    FOR(i, 0, n)FOR(j, 0, m) {
        chmax(dp[i + 1][j + 1], dp[i][j + 1]);
        chmax(dp[i + 1][j + 1], dp[i + 1][j]);
        if (s[i] == t[j])chmax(dp[i + 1][j + 1], dp[i][j] + 1);
    }
    cout << dp[n][m] << "\n";
}

int main() {
    init();

    int q; cin >> q;
    FOR(i, 0, q)solve();
}

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

int lcs(string X, string Y) {
	int dp[1000 + 1][1000 + 1];
	int max = 0;
	X = ' ' + X;
	Y = ' ' + Y;
	for (unsigned int i = 1; i <= X.size() - 1; i++)
		dp[i][0] = 0;

	for (unsigned int i = 1; i <= Y.size() - 1; i++)
		dp[0][i] = 0;
	for (unsigned int i = 1; i <= X.size() - 1; i++) {
		for (unsigned int j = 1; j <= Y.size() - 1; j++) {
			if (X[i] == Y[i])
				dp[i][j] = dp[i - 1][j - 1] + 1;
			else if (dp[i - 1][j] >= dp[i][j - 1])
				dp[i][j] = dp[i - 1][j];
			else
				dp[i][j] = dp[i][j - 1];

			max = (max < dp[i][j]) ? dp[i][j] : max;
		}
	}

	return max;
}

int main(int argc, char const *argv[]) {
	int N;
	cin >> N;

	for (int i = 0; i < N; i++) {
		string X, Y;
		cin >> X >> Y;

		cout << lcs(X, Y) << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <string>
#include <stdio.h>
#include <algorithm>
#define MAX 1000


int lcs(std::string X, std::string Y) {
    int c[MAX][MAX];
    int maxl = 0;
    int m = X.size();
    int n = Y.size();
    for (int i = 1; i <= m; i++) {
        c[i][0] = 0;
    }
    for (int i = 1; i <= n; i++) {
        c[0][i] = 0;
    }
    X = ' ' + X;
    Y = ' ' + Y;
    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] = std::max(c[i-1][j], c[i][j-1]);
            }
            maxl = std::max(maxl, c[i][j]);
        }
    }
    return maxl;
}

int main() {
    std::string s1, s2;
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        std::cin >> s1 >> s2;
        int a = lcs(s1, s2);
        printf("%d\n", lcs(s1, s2));
    }
    
    return 0;
}