Go: package main

/*
最長共通部分列
*/

import (
	"bufio"
	"bytes"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

func larger(l, r int) int {
	if l > r {
		return l
	}
	return r
}

var memo [1001][1001]int

func countLCS(x, y string) int {
	x = " " + x
	y = " " + y
	max := 0
	for xi := 1; xi < len(x); xi++ {
		for yi := 1; yi < len(y); yi++ {
			if x[xi] == y[yi] {
				memo[xi][yi] = memo[xi-1][yi-1] + 1
			} else {
				memo[xi][yi] = larger(memo[xi-1][yi], memo[xi][yi-1])
			}
			max = larger(max, memo[xi][yi])
		}
	}
	return max
}

func main() {
	scanner := makeScanner(1024)
	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		x := eGetLine(scanner)
		y := eGetLine(scanner)
		fmt.Println(countLCS(x, y))
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

type ints []int

func (s ints) String() string {
	var buf bytes.Buffer
	buf.WriteString("[")
	sep := ""
	for _, v := range s {
		buf.WriteString(fmt.Sprintf("%s%d", sep, v))
		sep = ", "
	}
	buf.WriteString("]")
	return buf.String()
}

func reverse(a ints) ints {
	r := make(ints, len(a))
	for i := 0; i < len(a); i++ {
		r[i] = a[len(a)-1-i]
	}
	return r
}
func max(a ints) (int, bool) {
	if len(a) == 0 {
		return 0, false
	}
	m := a[0]
	for _, e := range a {
		if e > m {
			m = e
		}
	}
	return m, true
}
func min(a []int) int {
	m := a[0]
	for _, v := range a {
		if v < m {
			m = v
		}
	}
	return m
}

func sum(a ints) int {
	if len(a) == 0 {
		return 0
	}
	sum := 0
	for _, e := range a {
		sum += e
	}
	return sum
}
func find(n int, a ints) int {
	for i, v := range a {
		if n == v {
			return i
		}
	}
	return -1
}

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

int main(){
	string X, Y;
	int q;
	vector<vector<int> > dp;

	cin>>q;
	for(int i=0;i<q;i++){
		cin>>X;
		cin>>Y;

		dp.resize(X.length()+1);
		for(int j=0;j<=X.length();j++)
			dp[j].resize(Y.length()+1);

		for(int j=0;j<=X.length();j++)
			dp[j][0] = 0;
		for(int k=0;k<=Y.length();k++)
			dp[0][k] = 0;

		for(int j=1;j<=X.length();j++){
			for(int k=1;k<=Y.length();k++){
				if(X[j-1] == Y[k-1])
					dp[j][k] = dp[j-1][k-1] + 1;
				else
					dp[j][k] = max(dp[j-1][k], dp[j][k-1]);
			}
		}

		cout<<dp[X.length()][Y.length()]<<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 i, j, c[N+1][N+1];
	int m = X.size(), n = Y.size(), max1 = 0;
	X = ' '+X, Y = ' '+Y;
	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-1][j], c[i][j-1]);
			max1 = max(max1, c[i][j]);
		}
	}
	return max1;
}

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

	return 0;
}

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

int solve(string s, string t) {
  int ss = s.size();
  int tt = t.size();
  vector<vector<int> > dp(ss+1, vector<int>(tt+1, 0));
  s.insert(0, " ");
  t.insert(0, " ");
  
  int ret = 0;
  rep(i, 1, ss+1) {
    rep(j, 1, tt+1) {
      if(s[i] == t[j])
        dp[i][j] = dp[i-1][j-1] +1;
      else
        dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
      ret = max(ret, dp[i][j]);
    }
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  int q;
  cin >> q;
  rep(aa, 0, q) {
    string x, y;
    cin >> x >> y;
    cout << solve(x, y) << "\n";
  }
}

C++: #if 0
PDFに記載されている方針がわかりやすかったので、比較的簡単に解くことができた。
#endif

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

int Q;
int L[1000+10][1000+10];

int main() {
  cin >> Q;
  string X, Y;
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < 1000+10; j++) {
      L[0][j] = 0; L[j][0] = 0;
    }
    cin >> X >> Y;
    for (int x = 1; x <= X.length(); x++) {
      for (int y = 1; y <= Y.length(); y++) {
        if (X[x-1] == Y[y-1])
          L[x][y] = L[x-1][y-1] + 1;
        else
          L[x][y] = max(L[x-1][y], L[x][y-1]);
      }
    }
    cout << L[X.length()][Y.length()] << endl;
  }
}

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

#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
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; }


int lcs(string s, string t) { // 最長共通部分列長
  int n = s.size();
  int m = t.size();
  vector<vector<int>> dp(n+1,vector<int>(m+1));
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
      if(s[i] == t[j]) {
        dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j] + 1);
      }
      dp[i+1][j+1] = max(dp[i+1][j+1], dp[i+1][j]);
      dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j+1]);
    }
  }
  return dp[n][m];
}


int main(){
    int n;
    cin >> n;
    rep(i,n) {
        string s, t;
        cin >> s >> t;
        int ans = lcs(s,t);
        cout << ans << endl;
    }
}

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

using namespace std;

int dp[1010][1010];

int main() {
    int q;  cin >> q;
    int *ans = new int[q];
    string s, t;
    int ls, lt, i, j;
    for (int k = 0; k < q; k++) {
        cin >> s >> t;
        ls = s.size(), lt = t.size();

        for (i = 0; i <= ls; i++) {
            for (j = 0; j <= lt; j++)
                dp[i][j] = 0;
        }

        for (i = 0; i < ls; i++) {
            for (j = 0; j <= lt; j++) {
                if (s[i] == t[j])
                    dp[i+1][j+1] = max(dp[i][j]+1, max(dp[i+1][j], dp[i][j+1]));
                else
                    dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
            }
        }   
        ans[k] = dp[ls][lt];
    }
    for (int k = 0; k < q; k++)
        cout << ans[k] << endl;
    return 0;
}