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 <bits/stdc++.h>

using namespace std;

using ll = long long;

// Longest Common Subsequence between X=x_1...x_m and Y=y_1...y_n
// O(mn)
// ALDS_1_10_C: verified
int main(){
    ll Q; cin >> Q;
    for(ll q=0;q<Q;q++){
        string X, Y; cin >> X >> Y;
        ll m = X.size(), n = Y.size();
        vector<vector<ll> > dp(m + 1, vector<ll>(n + 1));
        for(ll i=1;i<=m;i++){
            for(ll j=1;j<=n;j++){
                if(X[i-1] == Y[j-1]){
                    dp[i][j] = max(max(dp[i][j-1], dp[i-1][j]), dp[i-1][j-1] + 1);
                }else{
                    dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
                }
            }
        }
        cout << dp[m][n] << endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
#include <string>
#include <vector>
#include <utility>
#include <functional>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cassert>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)

int dp[1001][1001];

int main() {
    int q;
    cin >> q;
    REP(i, q) {
        string s, t;
        cin >> s >> t;
        REP(i, s.size() + 1) REP(j, t.size() + 1) dp[i][j] = 0;
        REP(i, s.size()) {
            REP(j, t.size()) {
                if (s[i] == t[j])
                    dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
                dp[i + 1][j + 1] = dp[i + 1][j];
                dp[i + 1][j + 1] = dp[i][j + 1];
            }
        }
        cout << dp[s.size()][t.size()] << 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];
  for(int i=0;i<=m;i++)
    c[i][0]=0;
  for(int i=1;i<=n;i++)
    c[0][i]=0;
  for(int i=1;i<=m;i++){
    for(int j=1;j<=n;j++){
      if(x[i-1]==y[j-1]) c[i][j]=c[i-1][j-1]+1;
      else c[i][j]=max(c[i-1][j],c[i][j-1]);
    }
  }
  return c[m][n];
}

int main(){
  int n; cin >> n;
  for(int i=0;i<n;i++){
    string x,y;
    cin >> x >> y;
    cout << lcs(x,y) << endl;
  }
}

C++: #include<stdio.h>

int strlen(char s[]) {
  int w = 0;
  while (1) {
    if (s[w] == '\0')
      break;
    w++;
  }
  return w;
}

int max(int a, int b, int c){
  int m = a;
  if (m < b)
    m = b;
  if (m < c)
    m = c;
  return m;
}

int main(void){
  int q, xl = 0, yl = 0, h = 0, i = 0, j = 0;
  int d, ans = 0;
  int DP[1001][1001];
  char X[1001], Y[1001];

  for (i = 0; i < 1001; i++) {
    DP[i][0] = 0;
  }
  for (i = 0; i < 1001; i++) {
    DP[0][i] = 0;
  }

  scanf("%d", &q);
  for (h = 0; h < q; h++) {

    scanf("%s", X);
    scanf("%s", Y);
    xl = strlen(X);
    yl = strlen(Y);

    for (i = 1; i <= xl; i++) {
       for (j = 1; j <= yl; j++) {
        if (X[i-1] == Y[j-1])
          d = 1;
        else
          d = 0;
        DP[i][j] = max(DP[i-1][j-1], DP[i-1][j], DP[i-1][j]) + d;
        if(DP[i][j] > ans)
          ans = DP[i][j];
      }
    }
    printf("%d\n", ans);
    ans = 0;
  }
  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;
const static int N = 1000;
int dp[N + 1][N + 1];

int lcs(string X, string Y) {
	int max = 0;
	int m = X.size();
	int n  = Y.size();
	X = ' ' + X;
	Y = ' ' + Y;

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

	for (int i = 1; i <= m; i++) {
		for ( int j = 1; j <= n; j++) {
			if (X[i] == Y[j])
				dp[i][j] = dp[i - 1][j - 1] + 1;
			else {
				if (dp[i - 1][j] > dp[i][j - 1])
					dp[i][j] = dp[i - 1][j];
				else
					dp[i][j] = dp[i][j - 1];
			}

			max = (max < dp[i][j]) ? dp[i][j] : max;
		}
	}

	return max;
}

int main(int argc, char const *argv[]) {
	int n;
	cin >> n;


	for (int i = 0; i < n; i++) {
		string X, Y;
		cin >> X >> Y;

		cout << lcs(X, Y) << endl;
	}

	return 0;
}

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

const int INF = 1e8;

int main(){ 
  int Q; cin >> Q;

  for(int query = 0; query<Q; query++){
    string S, T; cin >> S >> T;

    //dp[i][j]: S[i],T[j]まででの最長共通部分列
    vector<vector<int>> dp(1010, vector<int>(1010, 0));
    
    for(int i=0; i<S.length(); i++){
      for(int j=0; j<T.length(); 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.length()][T.length()] << endl;
  }
}

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

#define MAX_N 1000
#define MAX_M 1000

string s, t;
int dp[MAX_N + 1][MAX_M + 1];

void solve(){
	for(int i = 0; i < s.length(); i++){
		for(int j = 0; j < t.length(); j++){
			if(s[i] == t[j]){
				dp[i + 1][j + 1];
					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[s.length()][t.length()] << endl;
}

int main(){
	int n;
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> s >> t;
		solve();
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v
#define NUM(x,v) (POSU(x,v)-POSL(x,v))
const ll INF=1e9+7;
const ll N = 1002;
const ll M = 21;
ll q,dp[N][N];
string x,y;
vector <ll> ans;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>q;
	FOR(k,0,q){
		cin>>x>>y;
		FOR(i,0,x.size()+1){
			FOR(j,0,y.size()+1){
				dp[i][j]=0;
			}
		}
		FOR(i,0,x.size()+1){
			FOR(j,0,y.size()+1){
				if(i==0||j==0)dp[i][j]=0;
				else{
					if(x[i]==y[j])dp[i][j] = max({dp[i-1][j-1]+1,dp[i][j-1],dp[i-1][j]});
					else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
				} 
			}
		}
		ans.push_back(dp[x.size()][y.size()]);
	}
	FOR(i,0,ans.size()){
		cout<<ans[i]<<endl;
	}
	return 0;
}