Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	for i := 0; i < n; i++ {
		x := getNextString(scanner)
		y := getNextString(scanner)

		fmt.Fprintln(writer, lcs(x, y))
	}

	writer.Flush()
}

func lcs(x, y string) int {
	lx := len(x)
	ly := len(y)
	dp := make([][]int, lx+1)
	for i := 0; i <= lx; i++ {
		dp[i] = make([]int, ly+1)
	}
	for i := 0; i < lx; i++ {
		for ii := 0; ii < ly; ii++ {
			if x[i] == y[ii] {
				dp[i+1][ii+1] = dp[i][ii] + 1
				continue
			}
			dp[i+1][ii+1] = dp[i][ii+1]
			if dp[i+1][ii] > dp[i+1][ii+1] {
				dp[i+1][ii+1] = dp[i+1][ii]
			}
		}
	}
	return dp[lx][ly]
}

C++: #include <iostream>
#include <string>
using namespace std;
int A[1005][1005]={};

int main() {
  int i,j,k,q;
  cin >> q;
  string a,b;
  for(i=0;i<q;i++) {
    cin  >> a >> b;
    int al = a.size();
    int bl = b.size();
    for(j=1;j<=al;j++) {
      for(k=1;k<=bl;k++) {
        if(a[j-1]==b[k-1]) {
          A[j][k]=A[j-1][k-1]+1;
        } else {
          A[j][k]=max(A[j][k-1],A[j-1][k]);
        }
      }
    }
    cout << A[al][bl] << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int DP_SIZE = 1010;

int dp[DP_SIZE][DP_SIZE];

int main(){
	int N;
	cin >> N;
	
	for(int n=0; n<N; n++){
		for(int i=0; i<DP_SIZE; i++) for(int j=0; j<DP_SIZE; j++) dp[i][j] = 0;
		string S, T;
		cin >> S >> T;
		for(int i=0; i<(int)S.size(); i++){
			for(int j=0; j<(int)T.size(); j++){
				if(S[i] == T[j]) dp[i+1][j+1] = dp[i][j] + 1;
				else dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
			}
		}
		cout << dp[S.size()][T.size()] << endl;
	}
	
	return 0;
}

C++: #include<bits/stdc++.h>

#define int int64_t
#define rep(i,n) for(int i=0; i<(n); i++)
#define reps(i,n) for(int i=1; i<=(n); i++)
#define pii pair<int,int>
#define gsort(x,n) sort((x),(x)+(n),greater<int>())
#define all(x) (x).begin(), (x).end()
#define ll long long
#define re(s) reverse((s).begin(),(s).end())
#define setout(n,x) setw(n) << setfill(x)
#define Fixed fixed << setprecision(10)
const long long mod = 1000000007;
using namespace std;

int q,cnt=0;
string xy[1005];
int lcs[1010][1010];
string s,v;

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

    cin>>q;
    rep(i,q){
        cin>>s>>v;

        rep(j,s.size()){
            rep(k,v.size()){
                if(s[j] == v[k]){
                    lcs[j+1][k+1] = lcs[j][k]+1;
                }else{
                    lcs[j+1][k+1] = max(lcs[j+1][k],lcs[j][k+1]);
                }
            }
        }
        
        cout<<lcs[s.size()][v.size()]<<'\n';
    }
}

C++: #include<bits/stdc++.h>
using ll=long long;
using namespace std;
//本の問題と全く同じ

int main(){
  //input1
  int le;
  cin>>le;
  for(int pp=0;pp<le;pp++){
    //input2
    string x,y;
    cin>>x>>y;
    int n,m;
    n=x.size();
    m=y.size();

    int dp[1001][1001];//dp[i+1][j+1]:x[0]~x[i]とy[0]~y[j]のLCSの長さ

    for(int i=0;i<n;i++){
      for(int 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]);}}//どっちかの共通部分列のうち長い方でいいよね
    cout<<dp[n][m]<<endl;
  }
  return 0;
}

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

static const int N = 1000;

int lcs(string X,string Y){
    int c[N+1][N+1];    //c[i][k]???Xi??¨Yk???LCS??¨??????????????????
    int m = X.size();
    int n = Y.size();
    int maxl = 0;
    X = ' ' + X; // X[0]?????????????????\
    Y = ' ' + Y;
    for(int i=1;i<=m;i++) c[i][0] = 0;
    for(int k=1;k<=n;k++) c[0][k] = 0;

    for(int i=1;i<=m;i++){
        for(int k=1;k<=m;k++){
            if(X[i]==Y[k]){
                c[i][k] = c[i-1][k-1] + 1;
            } else {
                c[i][k] = max(c[i-1][k],c[i][k-1]);
            }
            maxl = max(maxl,c[i][k]);
        }
    }

    return maxl;
}

int main(){
    string s1,s2;
    int n;scanf("%d",&n);
    for(int i=0;i<n;i++){
        cin >> s1 >> s2;
        printf("%d\n",lcs(s1,s2));
    }

    return 0;
}

C++: #include <utility>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;

int a[1001][1001] = {0};

int main(){
	int n;
	cin >> n;
	string x;
	string y;
	for ( int i = 0; i < n; i++ ) {
		cin >> x;
		cin >> y;
		for ( int j = 0; j < x.size(); j++ ) {
			int f = 0;
			for ( int k = 0; k < y.size(); k++ ) {
				if ( x[j] == y[k] ) {
					a[j+1][k+1] = a[j][k] + 1;
				}else {
					a[j+1][k+1] = max(a[j+1][k],a[j][k+1]);
				}
				cout << a[j+1][k+1];
			}
			cout << endl;
		}
		cout << a[x.size()][y.size()] << 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++: #include<iostream>
#include<string>
using namespace std;

int lcs(string X, string Y) {
  int m = X.size();
  int n = Y.size();
  int c[m+1][n+1];
  X = ' ' + X;
  Y = ' ' + Y;
  for (int i=0; i<m+1; i++) {
    c[i][0] = 0;
  }
  for (int j=0; j<n+1; j++) {
    c[0][j] = 0;
  }

  int max_lcs = 0;
  for (int i=1; i<m+1; i++) {
    for (int j=1; j<n+1; 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]);
      }
      max_lcs = max(max_lcs, c[i][j]);
    }
  }

  return max_lcs;
}


int main() {
  int N;
  string X, Y;
  cin >> N;
  for (int i=0; i<N; i++) {
    cin >> X >> Y;
    cout << lcs(X, Y) << endl;
  }
  return 0;
}

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

#define _For(idx, from, to, inc, cmp) \
	for(auto idx=(from) ; idx cmp (to) ; inc idx)
#define ForLE(idx, from, to) \
	_For(idx, from, to, ++, <=)
#define For(idx, from, to) \
	_For(idx, from, to, ++, <)

constexpr int MaxLen = 1000;
int Memo[MaxLen+1][MaxLen+1];
int main() {
	std::string line;
	const auto nextline = [&line](){
		std::getline(std::cin, line);
	};
	nextline();
	const int NQ = std::stoi(line);
	For(i, 0, NQ) {
		std::string s0, s1;
		std::getline(std::cin, s0);
		std::getline(std::cin, s1);

		int len0 = s0.length(),
			len1 = s1.length();
		if(len0 < len1) {
			std::swap(s0, s1);
			std::swap(len0, len1);
		}
		ForLE(j, 0, len0)
			Memo[j][0] = 0;
		ForLE(j, 0, len1)
			Memo[0][j] = 0;

		ForLE(j, 1, len1) {
			ForLE(k, 1, len0) {
				int res = std::max(
								Memo[k][j-1],
								Memo[k-1][j]
							);
				if(s0[k-1] == s1[j-1])
					res = std::max(Memo[k-1][j-1] + 1, res);
				Memo[k][j] = res;
			}
		}
		std::cout << Memo[len0][len1] << std::endl;
	}
	return 0;
}