Go: package main

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

func max(x, y int) int {
	if x < y {
		return y
	} else {
		return x
	}
}

/*********************************************************************
長さmの列X_(m)と長さnの列Y_(n)の最長共通部分列LCSは、
	(1)X[m] == Y[n]ならば、X_(m-1)とY_(n-1)のLCSにX[m]を繋げたもの
	(2)X[m] != Y[n]ならば、X_(m-1)とY_(n)のLCSもしくはX_(m)とY_(n-1)のLCS
	のいずれかのうち長い方

c[m+1][n+1]：c[i][j]にX_(i)とY_(j)のLCSの長さを格納する二次元配列
*********************************************************************/

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		s1 := scanner.Text()
		scanner.Scan()
		s2 := scanner.Text()
		s1, s2 = s1 + " ", s2 + " "
		c := make([][]int, len(s1))
		for i := range c {
			c[i] = make([]int, len(s2))
		}

		LCS := 0

		for i := 1; i < len(s1); i++ {
			for j := 1; j < len(s2); j++ {
				if s1[i] == s2[j] {
					c[i][j] = c[i-1][j-1] + 1
				} else {
					c[i][j] = max(c[i-1][j], c[i][j-1])
				}
				LCS = max(LCS, c[i][j])
			}
		}
		fmt.Println(LCS)
	}
}

C++: #include<stdio.h>
#include<string>
#include<algorithm>
using namespase 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 = 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 <bits/stdc++.h>

#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a,n) for(ll a = 0;a < n;a++)
#define repi(a,b,n) for(ll a = b;a < n;a++)

using namespace std;

static const ll INF = 1e15;

struct LongestCommonSubsequence {
    static const int max_length = 1001;
    std::vector<std::vector<int>> dp;
    std::vector<std::vector<int>> pre;
    std::string s, t;

    LongestCommonSubsequence(std::string left, std::string right) {
        s = "+" + left;
        t = "*" + right;
        dp.assign(max_length, std::vector<int>(max_length, 0));
        pre.assign(max_length, std::vector<int>(max_length, 0));
    }

    void lcs() {

        for (int si = 1; si < s.size(); si++) {
            for (int ti = 1; ti < t.size(); ti++) {
                if (s[si] == t[ti]) {
                    dp[si][ti] = dp[si - 1][ti - 1] + 1;
                    pre[si][ti] = 1;
                }
                else if (dp[si - 1][ti] > dp[si][ti - 1]) {
                    dp[si][ti] = dp[si - 1][ti];
                    pre[si][ti] = 2;
                }
                else {
                    dp[si][ti] = dp[si][ti - 1];
                    pre[si][ti] = 3;
                }
            }
        }
    }

    std::string solve() {
        std::string ans = "";

        int si = s.size() - 1;
        int ti = t.size() - 1;
        while (si >= 1 && ti >= 1) {
            if (pre[si][ti] == 1) {
                ans += s[si];
                si--, ti--;
            }
            else if (pre[si][ti] == 2) {
                si--;
            }
            else {
                ti--;
            }
        }

        reverse(ans.begin(), ans.end());
        return ans;
    }
};

int main() {
    ll q;
    cin >> q;
    rep(i, q) {
        string s, t;
        cin >> s >> t;
        LongestCommonSubsequence seq(s, t);
        seq.lcs();
        cout << seq.solve().size() << endl;
    }
    return 0;
}

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

int main(){

  int n;
  int dp[1111][1111];

  cin >> n;
  while(n--){
    string str1, str2;
    cin >> str1 >> str2;

    memset(dp,0,sizeof(dp));

    for(int i = 1; i <= str1.size(); i++){
      for(int j = 1; j <= str2.size(); j++){
        if(str1[i-1] == str2[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[str1.size()][str2.size()] << endl;

  }
}

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

using namespace std;
typedef long long ll;

int dp[1001][1001];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  vector<string> xs(q), ys(q);
  for (int i = 0; i < q; i++) {
    cin >> xs[i] >> ys[i];
  }

  for (int i = 0; i < q; i++) {
    fill(dp[0], dp[1001], 0);
    string x = xs[i], y = ys[i];
    for (int j = 0; j < x.size(); j++) {
      for (int k = 0; k < y.size(); k++) {
        if (x[j] == y[k]) {
          dp[j + 1][k + 1] = dp[j][k] + 1;
        } else {
          dp[j + 1][k + 1] = max(dp[j][k + 1], dp[j + 1][k]);
        }
      }
    }

    cout << dp[x.size()][y.size()] << endl;
    ;
  }
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <queue>
#include <cmath>
#include <set>
#include <iomanip>

using namespace std;

typedef long long ll;

int get_LCS(string x, string y){
  int nx = x.size(), ny = y.size();
  int dp[nx+1][ny+1];
  for(int i=0;i<=ny;i++) dp[0][i] = 0;
  for(int i=0;i<=nx;i++) dp[i][0] = 0;
  for(int i=1;i<=nx;i++){
    for(int j=1;j<=ny;j++){
      if(x[i] == y[j]) dp[i][j] = dp[i-1][j-1] + 1;
      else dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
    }
  }
  return dp[nx][ny];
}

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int q; cin >> q;
  for(int i=0;i<q;i++){
    string x, y; cin >> x >> y;
    cout << get_LCS(x, y) << endl;
  }
}

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

int lcs(string s1,string s2)
{
	int m = s1.size();
	int n = s2.size();
	int c[1005][1005];
	for (int i = 1; i <= m; i++)c[i][0] = 0;
	for (int j = 1; j <= n; j++)c[0][j] = 0;
	int maxz = 0;
	s1 = " " + s1;
	s2 = " " + s2;
	for(int i=1;i<=m;i++)
		for (int j = 1; j <= n; j++) {
			if (s1[i] == s2[j])
				c[i][j] = c[i-1][j-1] + 1;
			else {
				c[i][j] = max(c[i - 1][j], c[i][j - 1]);
			}
			maxz = max(maxz, c[i][j]);
		}
	return maxz;
}
int main()
{
	int N;
	scanf("%d", &N);
	while (N--) {
		string s1, s2;
		cin >> s1>>s2;
		cout << lcs(s1, s2) << endl;
	}
	return 0;
}

C++: /*
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C&lang=jp
*/
#include<iostream>
#include<string>
using namespace std;

//最長共通部分列
int LCS(string x , string y){
  int x_len=x.size();
  int y_len=y.size();
  int dp[x_len+1][y_len+1];
  //初期化
  for(int i=0;i<=x_len;i++){
    for(int j=0;j<=y_len;j++){
      dp[i][j]=0;
    }
  }
  for(int i=0;i<x_len;i++){
    for(int j=0;j<y_len;j++){
      if(x[i]==y[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]);
      }
    }
  }
  return dp[x_len][y_len];
}
int main(){
  int q;
  cin>>q;
  for(int i=0;i<q;i++){
    string str1,str2;
    cin>>str1>>str2;
    cout<<LCS(str1,str2)<<"\n";
  }
}