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<algorithm>
using namespace std;

static const int N = 100;

int main() {
  int n, p[N + 1], m[N + 1][N + 1];
  cin >> n;
  for ( int i = 1; i <= n; i++ ) {
    cin >> p[i - 1] >> p[i];
  }

  for ( int i = 1; i <= n; i++ ) m[i][i] = 0;

  for ( int l = 2; l <= n; l++ ) {
    for ( int i = 1; i <= n - l + 1; i++ ) {
      int j = i + l - 1;
      m[i][j] = (1 << 21);
      for ( int k = i; k <= j - 1; k++ ) {
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

  cout << m[1][n] << endl;

  return 0;
}

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

using namespace std;

int main() {
	int q;
	string strX, strY;

	cin >> q;
	for (int n = 0; n < q; n++) {
		cin >> strX;
        cin >> strY;
		char X[strX.length() + 1];
		for (int x = 0; x < strX.length(); x++) {//文字列を配列に入れる
			X[x + 1] = strX[x];
		}

        char Y[strY.length() + 1];
		for (int y = 0; y < strY.length(); y++) {//文字列を配列に入れる
			Y[y + 1] = strY[y];
		}

        int c[strX.length()+1][strY.length()];
        int maxLen = 0;

		for (int i = 0; i<=strX.length(); i++){
            for(int j = 0; j<=strY.length(); j++){
                if(i == 0 || j == 0){
                    c[i][j] = 0;
                }
                else 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]);
                }

                if(c[i][j] > maxLen){
                    maxLen = c[i][j];
                }
            }
        }
        cout << maxLen << endl;
	}

    return 0;
}

C++: #include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;

int dp[1001][1001];

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

	vector<int> res;
	for(int i = 0; i < q; i++){
		string s, t;
		cin >> s >> t;

		fill(dp[0], dp[1000] + 1000, 0);
		for(int j = 0; j < s.size(); j++){
			for(int k = 0; k < t.size(); k++){
				if(s[j] == t[k]) dp[j+1][k+1] = dp[j][k] + 1;
				else dp[j+1][k+1] = max(dp[j+1][k], dp[j][k+1]);
			}
		}

		res.push_back(dp[s.size()][t.size()]);
	}

	for(int i = 0; i < q; i++) cout << res[i] << '\n';

	return 0;
}

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

int dp[1001][1001];
string str1, str2;
int n;

int solve(string str1, string str2) {
  memset(dp, 0, sizeof(dp));
  for(int r = 0; r < str2.size(); r++) {
    for(int c = 0; c < str1.size(); c++) {
      if(str2[r] == str1[c]) {
        dp[r + 1][c + 1] = dp[r][c] + 1;
      }
      else {
        dp[r + 1][c + 1] = max(dp[r + 1][c], dp[r][c + 1]);
      }
    }
  }
  return dp[str2.size()][str1.size()];
}

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

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

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    std::cin >> n;
    std::string s1, s2;
    for (int q=0; q<n; q++) {
        std::cin >> s1 >> s2;
        int m1 = s1.size()+1;
        int m2 = s2.size()+1;
        int* dp = (int*)malloc(m1*m2*sizeof(int));
        for (int i=0; i<m1; i++) {
            dp[i*m2]=0;
        }
        for (int i=0; i<m2; i++) {
            dp[i]=0;
        }
        for (int i=1; i<m1; i++) {
            int leftmax=0;
            for (int j=1; j<m2; j++) {
                dp[i*m2+j] = leftmax;
                if (s1[i-1] == s2[j-1]) {
                    dp[i*m2+j]+=1;
                }
                if (dp[(i-1)*m2+j] > leftmax) {
                    leftmax = dp[(i-1)*m2+j];
                }
                if (dp[i*m2+j] < dp[(i-1)*m2+j]) {
                    dp[i*m2+j] = dp[(i-1)*m2+j];
                }
            }
        }
        int max=0;
        for (int i=0; i<m2; i++) {
            if (dp[(m1-1)*m2+i] > max) {
                max = dp[(m1-1)*m2+i];
            }
        }
        std::cout << max << "\n";
        free(dp);
    }

    return 0;
}

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

int dp[maxn][maxn];
int len1, len2;
string s1, s2;

int lcs()
{
	memset(dp, 0, sizeof(dp));
	for (int i=1;i<=len1;i++)
		for (int j = 1; j <= len2; j++) {
			if (s1[i] == s2[j]) {
				dp[i][j] = dp[i - 1][j - 1] + 1;
			}
			else
			{
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
			}
		}
	return dp[len1][len2];
}

int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		cin >> s1 >> s2;
		len1 = s1.size(), len2 = s2.size();
		cout << lcs() << endl;
	}
	return 0;
}