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++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
#define rep(i, p, n) for(int i = p; i < n; i++) 

int main() {
    int n;
    cin >> n;
    rep(i, 0, n) {
        string s, t;
        cin >> s >> t;
        int lcs[s.size()+1][t.size()+1];
        rep(j, 0, s.size()+1) {
            rep(k, 0, t.size()+1) {
                lcs[j][k] = 0;
            }
        }
        rep(j, 1, s.size()+1) {
            rep(k, 1, t.size()+1) {
                if(s[j-1] == t[k-1]) {
                    lcs[j][k] = max(lcs[j-1][k], max(lcs[j][k-1], lcs[j-1][k-1]+1));
                }
                else {
                    lcs[j][k] = max(lcs[j-1][k], lcs[j][k-1]);
                }
            }
        }

        cout << lcs[s.size()][t.size()] << endl;
        
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>

using namespace std;

string s1,s2;

int L(int i,int j){
	if(i<0||j<0)return 0;
	else if(s1.at(i)==s2.at(j))return 1+L(i-1,j-1);
	else{
		int a=L(i,j-1);
		int b=L(i-1,j);
		return a>b?a:b;
	}
}

int main(){
	int q;
	cin>>q;
	for(int i=0; i<q; i++){
		cin>>s1>>s2;
		cout<<L(s1.size()-1,s2.size()-1)<<endl;
	}
}

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

int q;
string X, Y;

int dp[1000][1000];

int solve(int idx_x, int idx_y) {
  if (idx_x < 0 || idx_y < 0)
    return 0;

  if (dp[idx_x][idx_y] != -1)
    return dp[idx_x][idx_y];

  int ret;
  
  if (X[idx_x] == Y[idx_y]) {
    ret = solve(idx_x - 1, idx_y - 1) + 1;
  } else {
    ret = solve(idx_x - 1, idx_y);
    ret = max(solve(idx_x, idx_y - 1), ret);
  }
  
  return dp[idx_x][idx_y] = ret;
}

int main(void) {
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> X >> Y;
    memset(dp, -1, sizeof(dp));
    cout << solve(X.size()-1, Y.size()-1) << endl;
  }
  return 0;
}

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

using namespace std;

int q;
char X[1000], Y[1000];
int dp[1001][1001];

int main(void) {

    scanf("%d", &q);

    for (int i = 0; i < q; ++i) {
        scanf("%s %s", X, Y);
        int xlen = strlen(X), ylen = strlen(Y);
        for (int x = 0; x < xlen; ++x) {
            for (int y = 0; y < ylen; ++y) {
                if (X[x] == Y[y]) {
                    dp[x + 1][y + 1] = dp[x][y] + 1;
                }
                else {
                    if (dp[x + 1][y] > dp[x][y + 1]) {
                        dp[x + 1][y + 1] = dp[x + 1][y];
                    }
                    else {
                        dp[x + 1][y + 1] = dp[x][y + 1];
                    }
                }
            }
        }
        printf("%d\n", dp[xlen][ylen]);
//        memset(dp, 0, sizeof(int)*1001*1001);
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int calcAns(string s, string t) {
    int r = s.size() + 1;
    int c = t.size() + 1;

    int dp[r][c];
    rep(i, r)rep(j,c) dp[i][j] = 0;

    for(int i = 1; i < r; i++) {
        for(int j = 1; j < c; j++) {
            dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
            if(s[i-1] == t[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
        }
    }
    return dp[r-1][c-1];
}

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

    rep(i, q) {
        string s, t;
        cin >> s >> t;
        int ans = calcAns(s, t);
        cout << ans << endl;
    }
    return 0;
}

C++: #ifdef DEBUG
#include "../utils.cpp"
#endif
#include <bits/stdc++.h>
/*
Problem
最長共通部分列

 
Category
DP
 
URL:
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C&lang=ja

考察:
dp[X][Y] := Xに対するYのLCSを作るのに必要なXの部分文字列のindexの最小値
--> LCSのサイズの情報がないのでダメ。

dp[X][Y] := XとYで作れる LCS の長さ
--> 素直な定義。でも漸化式が作れるのか？
dp[0][Y] = 0
dp[i][j] = X[i] == Y[j] ? dp[i-1][j-1] + 1 : max(dp[i][j-1], dp[i-1][j])



Date: 2020/04/18
*/
 

using namespace std;
using ll = int64_t;
#define int int64_t
#define INF 10e9

 
/* ==== declarations ====== */
// functions
void solve();
void getInput();
 
// global variables
int Q;
vector<vector<int>> dp;
vector<int> V;
string X, Y;

/* ======================== */

void getInput() {
    cin>>X;
    cin>>Y;
}
 
 
void solve() {
    dp = vector<vector<int>>(X.size(), vector<int>(Y.size(), 0));

    for (int i=0; i<X.size(); ++i) {
        for (int j=0; j<Y.size(); ++j) {
            int a = (i>0 and j>0) ? dp[i-1][j-1] : 0;
            int b = (j>0) ? dp[i][j-1] : 0;
            int c = (i>0) ? dp[i-1][j] : 0;
            if (X[i] == Y[j]) {
                dp[i][j] = max(a + 1, max(b, c));
            }
            else {
                dp[i][j] = max(b, c);
            }
        }
    }
    // print(dp);
    cout<<dp[X.size()-1][Y.size()-1]<<endl;
}
 
 
 
signed main(){
    cin>>Q;
    for(int i=0; i<Q; ++i) {
        getInput();
        solve();
    }
    return 0;
}