Go: package main

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

// define
var (
	sc = bufio.NewScanner(os.Stdin)
)

func bordersInt(in_map [][]int, invalid int) [][]int{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]int, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]int, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func bordersString(in_map [][]string, invalid string) [][]string{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]string, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]string, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func scanIntLine(len int) []int{
	arr := make([]int, len)
	for i:=0; i<len; i++{
		sc.Scan()
		tmp, err := strconv.Atoi(sc.Text())
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func scanStringLine(len int) []string{
	arr := make([]string, len)
	for i:=0; i<len; i++{
		sc.Scan()
		arr[i] = sc.Text()
	}
	return arr
}

func scanIntSplit(len int) []int{
	arr := make([]int, len)
	sc.Scan()
	for i, v := range strings.Split(sc.Text(), " "){
		tmp, err := strconv.Atoi(v)
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func maxInt(args... int) int{
	sort.Ints(args)
	return args[len(args)-1]
}

func main(){
	buf := make([]byte, 0)
    sc.Buffer(buf, 1000000009)
	var n int 
	fmt.Scan(&n)
	dp := make([][]int, 1003)
	for i:=0; i<1003; i++ {
		dp[i] = make([]int, 1003)
	}
	input_lines := scanStringLine(2*n)
	for i:=0; i<n; i++ {
		S, T := input_lines[2*i], input_lines[2*i+1]
		lenS, lenT := len(S), len(T)
		for i:=1; i<lenS+1; i++ {
			for j:=1; j<lenT+1; j++ {
				if S[i-1] == T[j-1] {
					dp[i][j] = maxInt(dp[i-1][j-1]+1, dp[i-1][j], dp[i][j-1])
				} else {
					dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
				}
			}
		}
		fmt.Println(dp[lenS][lenT])
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;

int dp[1010][1010];

int main(){
  int q;
  cin >> q;
  for(int i = 0; i < q; ++i){
    string x, y;
    cin >> x >> y;
    fill((int*)dp, (int*)dp + sizeof(dp) / sizeof(int), 0);
    // fill((int*)dp, (int*)(dp + 1010), 0);

    int xs = x.size();
    int ys = y.size();

    for(int i = 0; i < xs; ++i){
      for(int j = 0; j < ys; ++j){
        if(x[i] == y[j])dp[i + 1][j + 1] = dp[i][j] + 1;
        else dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
      }
    }

    cout << dp[xs][ys] << endl;
  }

  return 0;
}

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

std::string x,y;
int lcs[1000][1000] = {0};

int max(int k,int l){
  if(k>l){
    return k;
  }else{
    return l;
  }
}

void LCS(int i,int j){
  if(x[i] == y[j]){
    lcs[i][j] = lcs[i-1][j-1]+1;
  }else{
    lcs[i][j] = max(lcs[i-1][j],lcs[i][j-1]);
  }
}

void pre(){
  for(int i=0;i<(int)y.size();i++){
    if(x[0] == y[i]){
      lcs[0][i] = 1;
    }else{
      lcs[0][i] = 0;
    }
  }
  for(int i=0;i<(int)x.size();i++){
    if(x[i] == y[0]){
      lcs[i][0] = 1;
    }else{
      lcs[i][0] = 0;
    }
  }
}

int main(){
  int q;
  std::cin >> q;
  for(int k=0;k<q;k++){
    std::cin >> x;
    std::cin >> y;
    int size_x=x.size(),size_y=y.size();
    pre();
    for(int i=1;i<size_x;i++){
      for(int j=1;j<size_y;j++){
	LCS(i,j);
      }
    }

    /*for(int i=0;i<size_x;i++){
      for(int j=0;j<size_y;j++){
	std::cout << lcs[i][j] << ' ';
      }
      std::cout << '\n';
    }
    */
    std::cout << lcs[size_x-1][size_y-1] << '\n'; 

  }
  return 0;
}

C++: #include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"
*/

int LCS(string s1, string s2){
    int c[s1.size()+1][s2.size()+1];
    rep(i,s1.size()+1) rep(j, s2.size()+1) c[i][j] = 0;
    for(int i=1; i<=s1.size(); i++){
        for(int j=1; j<=s2.size(); j++){
            if(s1[i-1] == s2[j-1]){
                c[i][j] = c[i-1][j-1] + 1;
            }else{
                c[i][j] = max(c[i-1][j], c[i][j-1]);
            }
        }
    }
    return c[s1.size()][s2.size()];
}

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

C++: // #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define Graph vector<vector<int>>
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1000000007;

int main(){
    int Q;
    cin >> Q;
    rep(k, Q){
        string X, Y;
        cin >> X >> Y;
        X = ' ' + X;
        Y = ' ' + Y;

        vector< vector<int> > A(X.size(), vector<int>(Y.size(), 0));
        for(int i = 1; i < X.size(); i++){
            for(int j = 1; j < Y.size(); j++){                
                if(X[i] == Y[j]){
                    A[i][j] = A[i-1][j-1] + 1;
                } else {
                    A[i][j] = max(A[i-1][j], A[i][j-1]);
                }
            }
        }
        cout << A[X.size() - 1][Y.size() - 1] << endl;
    }
}

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();

    X = ' ' + X;
    Y = ' ' + Y;

    int maxl = 0;

    // Initialize
    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 = 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) << endl;
    }

    return 0;
}

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