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 f() {
	var S, T string
	fmt.Scan(&S)
	fmt.Scan(&T)
	lenS, lenT := len(S), len(T)
	dp := make([][]int, lenS+1)
	for i:=0; i<lenS+1; i++ {
		dp[i] = make([]int, lenT+1)
	}
	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])
}

func main(){
	var n int 
	fmt.Scan(&n)
	for i:=0; i<n; i++ {
		f()
	}
}

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

int main(void){
	int q;
	string a,b;
	cin>>q;
	while(q--){
		cin>>a>>b;
		int dp[1010][1010] = {0};
		for(int j = 0 ; j < a.size() ; j ++){
			for(int i = 0 ; i < b.size() ; i ++){
				if(a[j] == b[i]){
					dp[j+1][i+1] = dp[j][i]+ 1;
				}else{
					dp[j+1][i+1] = max(dp[j][i+1],dp[j+1][i]);
				}
			}
		}
		cout<<dp[a.size()][b.size()]<<endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int dp[1001][1001];
int main()
{
    int q;
    cin >> q;
    rep(i, q) {
        string A, B;
        cin >> A >> B;
        rep(i, A.size()) {
            rep(j, B.size()) {
                dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
                if (A[i] == B[j]) chmax(dp[i + 1][j + 1], dp[i][j] + 1);
            }
        }
        cout << dp[(int)A.size()][(int)B.size()] << endl;
    }
    return 0;
}

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

using namespace std;

int lcs(string a, string b)
{
	vector<vector<int> > dp(1001, vector<int>(1001));
	for (int i = 0; i < a.size(); ++i)
	{
		for (int j = 0; j < b.size(); ++j)
		{
			if (a[i] == b[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]);
			}
		}
	}
	return(dp[a.size()][b.size()]);
}

void solve()
{
	int q;
	cin >> q;
	for (int i = 0; i < q; ++i)
	{
		string a, b;
		cin >> a >> b;
		cout << lcs(a, b) << endl;
	}
}

int main()
{
	solve();
	return(0);
}

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

using namespace std;

int LCS(string X, string Y) {

	int lcs[X.size() + 1][Y.size() + 1];

	for (int i = 0; i < X.size(); ++i) {
		lcs[0][i] = 0;
	}
	for (int i = 0; i < Y.size(); ++i) {
		lcs[i][0] = 0;
	}

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

	return lcs[X.size()][Y.size()];
}

int main() {

	int q;
	scanf("%d", &q);

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

	return 0;

}

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

using namespace std;


int solve(const string& s1, const string& s2) {
    const size_t N = 1000 + 1;
    int A[N][N] = {0};

    auto m = s1.size();
    auto n = s2.size();
    for (int i = 1; i <= m; ++i) {
        for (int j = 1; j <= n; ++j) {
            if (s1[i-1] == s2[j-1]) {
                A[i][j] = A[i-1][j-1] + 1;
            } else {
                A[i][j] = max(A[i][j-1], A[i-1][j]);
            }
        }
    }
    return A[m][n];
}

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

    string s1, s2;
    for (int i = 0; i < q; ++i) {
        cin >> s1 >> s2;
        cout << solve(s1, s2) << endl;
    }

    return 0;
}

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

using namespace std;

#if 0

以下のURLを参照。参照した箇所は、a = ' ' + a;のように空白をいれていることで文字のサイズを合わせていることである。
http://judge.u-aizu.ac.jp/onlinejudge/review.jsp?rid=2065462#1

サンプルインプットでは正しい出力を得られているようにみられているが、なぜかwrong answerが出力されてしまう。
#endif

int n;
string a, b;



int main(){
  cin >> n;
  while(n != 0){
    n = n -1;
    int longest[1051][1051];
    cin >> a;
    cin >> b;
    //a = ' ' + a;
    //b = ' ' + b;
    for(int i = 0; i <= a.size(); ++i){
      for(int j = 0; j <= b.size(); ++j){
        if(j <= 0 || i <= 0){
          longest[i][j] = 0;
        }
        else if(a[i] == b[j]){
          longest[i][j] = 1 + longest[i-1][j - 1];
        }
        else{
          longest[i][j] = max(longest[i][j-1], longest[i-1][j]);
        }
        
      }
    }
    //cout << a[1] << endl;
    cout << longest[a.size()][b.size()] << endl;
  }
}

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

using namespace std;
static const int N=1000;
int lcs(string X, string Y){
    int m=X.size();
    int n=Y.size();
    int c[N+1][N+1];
    int maxl=0;
    X=' '+X;
    Y=' '+Y;
    int i,j;
    for(i=1;i<=m;i++)c[i][0]=0;
    for(j=1;j<=n;j++)c[0][j]=0;

    for(i=1;i<=m;i++){
        for(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][j-1],c[i-1][j]);
            }
            maxl=max(maxl,c[i][j]);
        }
    }
    return maxl;
}
int main()
{
    int n;
    string s1,s2;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>s1>>s2;
        cout<<lcs(s1,s2)<<endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
typedef long long ll;
using namespace std;


int main()
{
    int q;
    cin >> q;
    for(int i = 0; i < q; i++){
        string x, y;
        cin >> x >> y;
        int c[1001][1001];
        for(int j = 0; j <= (int)x.size(); j++) c[j][0] = 0;
        for(int j = 0; j <= (int)y.size(); j++) c[0][j] = 0;
        for(int j = 1; j <= (int)x.size(); j++){
            for(int k = 1; k <= (int)y.size(); k++){
                if(x[j - 1] == y[k - 1]) c[j][k] = c[j - 1][k - 1] + 1;
                else c[j][k] = max(c[j - 1][k],c[j][k - 1]);
                
            
            }
        
        }
        cout << c[x.size()][y.size()] << endl;
        
        
        
    }
    
    
    return 0;
}