Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	Q := sc.nextInt()
	for q := 0; q < Q; q++ {
		X, Y := sc.nextStr(), sc.nextStr()
		xLen, yLen := len(X), len(Y)
		dp := make([][]int, xLen+1)
		for i := 0; i <= xLen; i++ {
			dp[i] = make([]int, yLen+1)
		}
		for x := 0; x <= xLen; x++ {
			for y := 0; y <= yLen; y++ {
				if x == 0 || y == 0 {
					dp[x][y] = 0
				} else if X[x-1] == Y[y-1] {
					dp[x][y] = dp[x-1][y-1] + 1
				} else {
					dp[x][y] = max(dp[x-1][y], dp[x][y-1])
				}
			}
		}
		fmt.Fprintln(wtr, dp[xLen][yLen])
	}

	wtr.Flush()
}

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

int main(void){
  string st1, st2;
  int q, val[151][151];

  cin >> q;

  for(int i=0; i<q; i++){
    cin>>st1;
    cin>>st2;

    for(int j=0; j<=st1.length(); j++){
      for(int k=0; k<=st2.length(); k++){

	if(j==0 || k==0) val[j][k] = 0;
	else{
	  if(val[j-1][k] == min(st1.length(), st2.length())){
	    val[j][k] = st1.length();
	  }else{

	    if(st1[j-1] == st2[k-1]){
	      val[j][k] = val[j-1][k-1]+1;
	    }else{
	      val[j][k] = max(val[j-1][k], val[j][k-1]);
	    }
	  }
	}
      }
    }

    for(int j=0; j<=st1.length(); j++){
      for(int k=0; k<=st2.length(); k++){
	cout << val[j][k] << " ";
      }
      cout << "\n";
    }
    

    cout << val[st1.length()][st2.length()] << "\n";
  }

  return 0;
}

C++: #include<math.h>
#include<limits.h>
#include<iostream>
#include<string>
#include<vector>
#include<stdio.h>
#include<sstream>
#include<list>
#include<queue>
#include<algorithm>
#include<functional>
#include<map>
#include<set>
//#include<utility>
//#include<initializer_list>
//#include<tuple>
//#include<regex>
using namespace std;
#define ull unsigned long long
#define ll long long
#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define all(t) t.begin(), t.end()
bool isSubstring(string substr, string t) {
	int n = substr.length();
	int seek = 0;
	rep(i, n) {
		bool ex = 0;
		for (int j = seek; j < t.length(); j++) {
			if (substr[i] == t[j]) {
				ex = 1;
				seek = j + 1;
				break;
			}
		}
		if (ex) {
			continue;
		}
		else {
			return false;
		}
	}
	return true;

}

int LongestCommonSubstring(string s, string t) {
	// Wrong plan
	vector<int>dp(s.length(), 0);
	int n = s.length();
	string lcs = "";
	if (isSubstring(s.substr(n - 1, n), t)) {

		dp[n - 1] = 1;
		lcs = s[n - 1];
	}
	for (int i = n - 2; i >= 0; i--) {
		//string substr = s.substr(i, n);
		string substr = s[i] + lcs;
		int issubst = isSubstring(substr, t);
		if (issubst) lcs = substr;
		dp[i] = dp[i + 1] + issubst;
	}
	return dp[0];
}
int LongestCommonSubstring2(string s, string t) {
	vector<vector<int>> dp(s.length() + 1, vector<int>(t.length() + 1, 0));
	int n = s.length() + 1;
	int m = t.length() + 1;
	rep(i, n - 1) {
		rep(j, m - 1) {
			dp[i + 1][j + 1] = s[i] == t[j] ? dp[i][j] + 1 : max(dp[i][j + 1], dp[i + 1][j]);
		}
	}
	return dp[n - 1][m - 1];
}
int main() {
	//cout << isSubstring("abc", "abcde") << endl;
	//cout << isSubstring("bd", "abcde") << endl;
	//cout << isSubstring("db", "abcde") << endl;

	//cout << LongestCommonSubstring2("bacd", "becd") << endl;
	//cout << LongestCommonSubstring2("abcd", "becd") << endl;
	//cout << endl;
	cout << LongestCommonSubstring2("a", "a") << endl;
	cout << LongestCommonSubstring2("bbb", "bbb") << endl;
	cout << LongestCommonSubstring2("aba", "bbaba") << endl;
	cout << LongestCommonSubstring2("abababbbaa", "bbabaabba") << endl;
	cout << LongestCommonSubstring2("a", "aaaaa") << endl;
	cout << LongestCommonSubstring2("aizojfeiz", "aaaizojifeizz") << endl;
	cout << LongestCommonSubstring2("zzyiioxizu", "zzsyoizuo") << endl;
	cout << LongestCommonSubstring2("iooooioiooiio", "ooiiioioiooooiioio") << endl;
	cout << LongestCommonSubstring2("ajfoaooieellzljieoaelj", "ajoiiffoaolliseellzljieoaelj") << endl;
	cout << LongestCommonSubstring2("zzzzzyyzyy", "zzyyyyz") << endl;
	cout << LongestCommonSubstring2("abcdef", "fedcba") << endl;
	cout << LongestCommonSubstring2("abc", "xyz") << endl;
	int q;
	cin >> q;
	vector<string> s(q);
	vector<string> t(q);
	rep(i, q) {
		cin >> s[i] >> t[i];
	}
	rep(i, q) {
		cout << LongestCommonSubstring2(s[i], t[i]) << 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[1001][1001] = {};
    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;
  }
}

C++: #include <bits/stdc++.h>
#define MOD 1000000007
#define INF 1<<30
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(ll i=(a); i<(b); i++)
#define RREP(i,a,b) for(ll i=(a); i>=(b); i--)
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> p;
typedef vector<pair<ll, ll>> vpl;


int main(){
    ll q; cin >> q;
    ll dp[1005][1005];
    rep(i,q){
        string a,b; cin >> a >> b;
        ll m = a.size();
        ll n = b.size();
        a = ' ' + a;
        b = ' ' + b;
        rep(i,m+1) dp[i][0] = 0;
        rep(i,n+1) dp[0][i] = 0;
        REP(i,1,m+1){
            REP(j,1,n+1){
                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]);
                }
            }
        }
        cout << dp[m][n] << "\n";
    }
}