Go: package main

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func lcs(a, b string) int {
	lenA := len(a)
	lenB := len(b)
	dp := make([][]int, lenA+1)
	for i := range dp {
		dp[i] = make([]int, lenB+1)
	}

	for i := 1; i <= lenA; i++ {
		for j := 1; j <= lenB; j++ {
			if a[i-1] == b[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = max(dp[i][j-1], dp[i-1][j])
			}
		}
	}
	return dp[lenA][lenB]
}

func main() {
	n := scanInt()
	w := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		a := scanString()
		b := scanString()
		fmt.Fprintln(w, lcs(a, b))
	}
	w.Flush()
}

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