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 <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <typeinfo>
#include <fstream>
#include <climits>
#include <functional>

#define REP(i,n) for(int i = 0;i < n;i++)

using namespace std;
typedef long long ll;
const int INF = INT_MAX / 4;

const int max_n = 1e3;

int solve(void) {
  string x, y;
  cin >> x >> y;

  int xl = x.length(), yl = y.length();
  int dp[xl][yl];

  dp[0][0] = (int)(x[0] == y[0]);
  for (int j = 1;j < yl;j++)
    dp[0][j] = max(dp[0][j-1], (int)(x[0] == y[j]));
  for (int i = 1;i < xl;i++) {
    dp[i][0] = max(dp[i-1][0], (int)(x[i] == y[0]));
    for (int j = 1;j < yl;j++) {
      dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
      if (x[i] == y[j]) {
	dp[i][j] = max(dp[i][j], dp[i-1][j-1] + 1);
      }
    }
  }

  return dp[xl-1][yl-1];
}


int main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  int cases;
  cin >> cases;
  REP(i, cases) {
    cout << solve() << "\n";
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxx = 1010;
int c[maxx][maxx];

int lcs(char s1[],char s2[]) {
    int m = strlen(s1), n = strlen(s2);
    for (int i = 0; i <= m; i++)
    c[i][0] = 0;
    for (int i = 0; i <= n; i++)
    c[0][i] = 0;
    
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (s1[i] == s2[j])
            c[i+1][j+1] = c[i][j] + 1;
            else
            c[i+1][j+1] = max(c[i][j+1], c[i + 1][j]);
        }
    }
    return c[m][n];
}
int main()
{
    char s1[maxx], s2[maxx];
    int t;
    scanf("%d", &t);
    for (int i = 0; i < t; i++) {
        scanf("%s %s", s1, s2);
        printf("%d\n",lcs(s1,s2));
    }
    
    return 0;
}

C++: #include <iostream>
#include <string>
#define repi(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,a) repi(i,0,a)
#define clr(a,v) memset((a),(v),sizeof(a))

int q;
int dp[1000][1000];
std::string s, t;

int rec( int i, int j )
{
  int &ret = dp[i][j];

  if( ~ret )
    return ret;

  if( i == s.size() || j == t.size() )
    return 0;

  ret = 0;

  if( s[i] == t[j] )
    ret = std::max( ret, rec( i+1, j+1 )+1 );

  ret = std::max( { ret, rec( i+1, j ), rec( i, j+1 ) } );

  return ret;
}

int main()
{
  scanf( "%d", &q );
  while( q-- )
  {
    std::cin >> s >> t;

    clr( dp, -1 );

    std::cout << rec( 0, 0 ) << std::endl;
  }

  return 0;
}

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

using namespace std;

string str1,str2;

int m[1000][1000];

int LCS(int i,int j){
  
  if(m[i][j]!=0){
    return m[i][j];
  }
  if(i==-1||j==-1){
    return 0;
  }else if(str1[i]==str2[j]){
    m[i][j]=LCS(i-1,j-1)+1;
    return m[i][j];
  }else{
    m[i][j]=max(LCS(i-1,j),LCS(i,j-1));
    return m[i][j];
  }
  
}

int main(){
  int N;
  
  cin >> N;
  
  
  for(int i=0;i<N;i++){
    cin >> str1 >> str2;
    for(int j=0;j<str1.size();j++){
      for(int k=0;k<str2.size();k++){
        m[j][k]=0;
      }
    }
    cout << LCS(str1.size()-1,str2.size()-1) << endl;
  }
  return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxx=1010;

int main (){
	int dp[maxx][maxx];
	string s,p;
	int q;
	cin>>q;
	while(q--){
		cin>>s>>p;
		for(int i=0;i<=s.size();i++){
			for(int j=0;j<=p.size();j++){
				if(i==0||j==0) dp[i][j]=0;
				else if(s[i]==p[j]) dp[i][j]=dp[i-1][j-1]+1;
				else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
		cout<<dp[s.size()][p.size()]<<endl;
	}
	return 0;
}

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

using namespace std;

const int max_size = 1001;

int main() {
  int n_input; // number of inputs of pairs of sequences
  cin >> n_input;

  for (int i = 0; i < n_input; i++) {
    string seq1, seq2;
    cin >> seq1 >> seq2;
    int seq1_len = seq1.length(), seq2_len = seq2.length();
    int grid[max_size][max_size] = {{0}};
    for (int rowi = 1; rowi <= seq1_len; rowi++) {
      for (int coli = 1; coli <= seq2_len; coli++) {
	if (seq1[rowi - 1] == seq2[coli - 1]) {
	  grid[rowi][coli] = grid[rowi - 1][coli - 1] + 1;
	} else {
	  int above_grid = grid[rowi - 1][coli], left_grid = grid[rowi][coli - 1];
	  grid[rowi][coli] = (above_grid >= left_grid)?above_grid:left_grid;
	}
      }
    }
    cout << grid[seq1_len][seq2_len] << endl;
  }
}

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

int dp[1001][1001];
int main() {
	int q; scanf("%d", &q);
	rep(i, q) {
		string x, y; cin >> x >> y;
		rep(i, x.size())rep(j, y.size()) {
			if (x[i] == y[j])dp[i + 1][j + 1] = dp[i][j] + 1;
			else dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
		}
		printf("%d\n", dp[x.size()][y.size()]);
	}
}