Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Position struct {
	X int
	Y int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}
	for i := 0; i < n; i++ {
		strX := nextText(sc)
		strY := nextText(sc)
		fmt.Println(calcLcs(strX, strY))
	}
}

func calcLcs(strX, strY string) int {
	return lcs(strX, strY, Position{0, 0}, make(map[Position]int))
}

func lcs(strX string, strY string, position Position, memo map[Position]int) int {
	if position.X >= len(strX) || position.Y >= len(strY) {
		return 0
	}
	if v, ok := memo[position]; ok {
		return v
	}
	if strX[position.X] == strY[position.Y] {
		result := lcs(strX, strY, Position{position.X + 1, position.Y + 1}, memo)
		memo[position] = result + 1
		return memo[position]
	} else {
		result1 := lcs(strX, strY, Position{position.X + 1, position.Y}, memo)
		result2 := lcs(strX, strY, Position{position.X, position.Y + 1}, memo)
		var result int
		if result1 > result2 {
			result = result1
		} else {
			result = result2
		}
		memo[position] = result
		return result
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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;  // X[0] に空白を挿入
  Y = ' ' + Y;  // Y[0] に空白を挿入
  for ( int i = 1; 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;
}

C++: #include <cstdio>
#include <algorithm>
#include <string.h>

using namespace std;
const int N_MAX = 1000;
char x[N_MAX+10], y[N_MAX+10];
int dp[N_MAX][N_MAX];

void solve() {
  int i, j;
  scanf("%s", x);
  scanf("%s", y);
  int N = strlen(x);
  int M = strlen(y);
  //printf("N = %d M = %d", N, M);
  for (i = 0; i < N+1; i++) {
    dp[i][0] = 0;
  }
  for (j = 0; j < M+1; j++) {
    dp[0][j] = 0;
  }
  for (i = 0; i < N; i++) {
    for (j = 0; j < M; j++) {
      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[N][M]);
} 

int main() {
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    solve();
  }
  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#define DATA_SIZE 1001

using namespace std;

int lcs(int dp[][DATA_SIZE], string x, string y){
    int xl = x.size();
    int yl = y.size();
    
    for(int i = 1; i <= xl; i++){
        for(int j = 1; j <= yl; j++){
            if(x[i-1] == y[j-1]){ dp[i][j] = dp[i-1][j-1] + 1; }
            else{ dp[i][j] = max(dp[i-1][j], dp[i][j-1]); }
        }
    }

    return dp[xl][yl];
}

int main(){
    int q, dp[DATA_SIZE][DATA_SIZE] = {};
    string X, Y;

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

    return 0;
}

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>
#include <algorithm>
using namespace std;
int dp[1002][1002];

int main(){
	int n;cin >> n;
	string x, y;
	while(n--){
		fill((int*)dp, (int*)(dp+1002), 0);
		cin >> x >> y;
		for(int i = 1;i <= x.size();i++){
			for(int j = 1;j <= y.size();j++){
				if(x[i-1] == y[j-1])dp[i][j] = dp[i-1][j-1] + 1;
				else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
			}
		}
		cout << dp[x.size()][y.size()] << endl;
	}
	return 0;
}

C++: // 17D8104010B 宮本拓輝 10_c c++

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main(){
  int score[1001][1001];
  int n;
  cin>>n;
  int result[n];
  for(int s=0;s<n;s++){
    string x,y;
    cin>>x;
    cin>>y;

    for(int i=0;i<x.size();i++){
      for(int j=0;j<y.size();j++){
        if(x[i]==y[j]){
          score[i+1][j+1]=score[i][j]+1;
        }
        else{
          score[i+1][j+1]=max(score[i+1][j],score[i][j+1]);
        }
      }
    }
    //cout<<score[x.size()][y.size()]<<"\n";
    result[s]=score[x.size()][y.size()];

  }
  for(int i=0;i<n;i++){
    cout<<result[i]<<"\n";
  }

  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;
}

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;
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <queue>

using namespace std;

const char *X, *Y;

int table_lcs[1001][1001];

int lcs(int x_begin, int y_begin) {
	if (table_lcs[x_begin][y_begin] != -1) {
		return table_lcs[x_begin][y_begin];
	}
	int max = 0;
	for (int i = x_begin; X[i] != '\0'; i++) {
		for (int j = y_begin; Y[j] != '\0'; j++) {
			if (X[i] == Y[j]) {
				int v_lcs = lcs(i + 1, j + 1);
				if (v_lcs + 1 > max) {
					max = v_lcs + 1;
				}
			}
		}
	}
	return table_lcs[x_begin][y_begin] = max;

}

int main() {

	int N;
	cin >> N;
	string stringX, stringY;
	for (int i = 1; i <= N; i++) {
		for (int i = 0; i < 1001; i++) {
			for (int j = 0; j < 1001; j++) {
				table_lcs[i][j] = -1;
			}
		}
		cin >> stringX >> stringY;
		X = stringX.c_str();
		Y = stringY.c_str();
		printf("%d\n", lcs(0, 0));
	}
	return 0;
}