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++: // Last Change: 12/29/2018 23:21:03.
//
// XとYの先頭に空白を入れることで、探索するべき要素のindexを0~m-1ではなく
// 1~mにしている。これにより、一番最初のcの更新において、i=j=0におけるc[i-1][j-1]
//すなわちc[-1][-1]にアクセスしてしまうことを防いでいる。
//
//***********************************************************************
//下から順番に計算していく方法
//
//***********************************************************************

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

#define MAXLENGTH 1001

int LCS(string, string);

int LCS(string X, string Y) {
  static int c[MAXLENGTH][MAXLENGTH];
  for (int i = 0; i < MAXLENGTH; i++) {
    c[i][0] = c[0][i] = 0;
  }
  int m, n, maxL;
  m = X.size();
  n = Y.size();
  maxL = 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] = max(c[i - 1][j], c[i][j - 1]);
      }
      maxL = max(maxL, c[i][j]);
    }
  }
  return maxL;
}

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

C++: #include <iostream>
#include <string>
#include <algorithm>
const int MAX_S = 1005;
using namespace std;

int main(void) {
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        string x, y;
        cin >> x >> y;
        x = ' ' + x;
        y = ' ' + y;

        int dp[MAX_S][MAX_S];
        for (auto j = 1; j < x.size(); j++) {
            dp[j][0] = 0;
        }
        for (auto k = 1; k < y.size(); k++) {
            dp[0][k] = 0;
        }

        int maxlength = 0;
        for (auto j = 1; j < x.size(); j++) {
            for (auto k = 1; k < y.size(); k++) {
                if (x[j] == y[k]) {
                    dp[j][k] = dp[j-1][k-1] + 1;
                } else {
                    dp[j][k] = max(dp[j-1][k], dp[j][k-1]);
                }
                maxlength = max(maxlength, dp[j][k]);
            }
        }

        cout << maxlength << 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; // X[0]?????????????????\
  Y = ' ' + Y; // Y[0]?????????????????\
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[i]) {
        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 <vector>

int main(int argc, char const *argv[]) {
  int q;
  std::cin >> q;
  for (size_t _ = 0; _ < q; _++) {
    std::string x, y;
    std::cin >> x;
    std::cin >> y;

    std::vector<std::vector<int> > dp(x.size()+1, std::vector<int>(y.size()+1));
    for (size_t ii=0; ii<x.size(); ++ii){
      for(size_t jj=0; jj<y.size(); ++jj){
        size_t i = ii + 1;
        size_t j = jj + 1;
        if (x[ii]==y[jj]){
          dp[i][j] = dp[i-1][j-1] + 1;
        }
        else{
          dp[i][j] = std::max(dp[i-1][j], dp[i][j-1]);
        }
      }
    }
    std::cout << dp[x.size()][y.size()] << std::endl;
  }
  return 0;
}

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

int lcs(string str1, string str2)
{
	int len[1001][1001] = { 0 };

	int len1 = str1.size();
	int len2 = str2.size();
	str1 = ' ' + str1;
	str2 = ' ' + str2;

	for (int i = 0; i <= len1; i++)
		for (int j = 0; j <= len2; j++) len[i][j] = 0;

	for (int i = 1; i <= len1; i++)
	{
		for (int j = 1; j <= len2; j++)
		{
			if (str1[i] == str2[j]) len[i][j] = len[i - 1][j - 1] + 1;
			else len[i][j] = max(len[i - 1][j], len[i][j - 1]);
		}
	}
	return len[len1][len2];
}

int main()
{
	int n;
	string str1, str2;

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

	return 0;
}

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

int solve(string x, string y){
    int m = x.length();
    int n = y.length();
    int maxl = 0;
    int c[1001][1001];
    
    for(int i = 0; i < m; i++) c[i][0] = 0;
    for(int i = 0; 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] = max(c[i][j-1], c[i-1][j]);
            }
            maxl = max(maxl,c[i][j]);
        }
    }
    return maxl;
}

int main(){
    int q, l = 0;
    string x,y;
    vector<int> a;

    cin >> q;
    for(int i = 0; i < q; i++){
        cin >> x >> y;
        a.push_back(solve(x,y));
    }

    for(auto i: a){
        cout << i << 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;
}