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 <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;
static const int N=1000;
int lcs(string X, string Y){
    int m=X.size();
    int n=Y.size();
    int c[N+1][N+1];
    int maxl=0;
    X=' '+X;
    Y=' '+Y;
    int i,j;
    for(i=1;i<=m;i++)c[i][0]=0;
    for(j=1;j<=n;j++)c[0][j]=0;

    for(i=1;i<=m;i++){
        for(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][j-1],c[i-1][j]);
            }
            maxl=max(maxl,c[i][j]);
        }
    }
    return maxl;
}
int main()
{
    int n;
    string s1,s2;
    cin>>n;
    for(int i=0;i<n-1;i++){
        cin>>s1>>s2;
        cout<<lcs(s1,s2)<<endl;
    }
    cin>>s1>>s2;
    cout<<lcs(s1,s2);
    return 0;
}

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

using namespace std;

/**
 *  lcs(m,n) = 
 *  if x[m] == y[n] lcs[m-1,n-1]+1
 *  max(lcs[m-1,n],lcs[m,n-1])
 *
 */

string x, y;

const int N = 1000;

int c[N][N];

int lcs(int m, int n)
{
	if (m == -1 || n== -1) return 0;
	if (x[m] == y[n]) return lcs(m-1,n-1) + 1;
	int & cc = c[m][n];
	if (cc != -1) return cc;
	int xx = lcs(m-1,n);
	int yy = lcs(m,n-1);
	cc = xx > yy ? xx : yy;
	return cc;
}

int main()
{
	int n = 0;
	x.reserve(N);
	y.reserve(N);
	cin >> n; 
	for (int i=0;i<n;i++)
	{
		memset(c, -1, sizeof(c));
		cin >> x;
		cin >> y;
		cout << lcs(x.size()-1, y.size()-1) << endl;
		x.clear();
		y.clear();
	}
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>

int find_lcs(std::string s1, std::string s2)
{
  // DP table
  int s1_len = s1.size();
  int s2_len = s2.size();

  int dp[s1_len+1][s2_len+1];
  std::memset(dp, 0, sizeof(dp));

  for ( int i = 0; i < s1_len; i++ ) {
    for ( int j = 0; j < s2_len; j++ ) {
      if ( s1[i] == s2[j] ) {
        dp[i+1][j+1] = dp[i][j] + 1;
      } else {
        dp[i+1][j+1] = std::max(dp[i][j+1], dp[i+1][j]);
      }
    }
  }

  return dp[s1_len][s2_len];
}

int main(void)
{
  int q;
  std::cin >> q;
  for ( int i = 0; i < q; i++ ) {
    std::string s1, s2;
    std::cin >> s1 >> s2;
    std::cout << find_lcs(s1, s2) << std::endl;
  }
  return 0;
}

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

using namespace std;
using ll = long long;

void init();
void solve();

#define rep(i, a) for (int i = 0; i < (int) (a); i++)
#define reps(i, a, b) for (int i = (int) (a); i < (int) (b); i++)
#define rrep(i, a) for (int i = (int) a-1; i >= 0; i--)
#define rreps(i, a, b) for (int i = (int) (a)-1; i >= (int) (b); i--)
#define MP(a, b) make_pair((a), (b))
#define PB(a) push_back((a))
#define all(v) (v).begin(), (v).end()
#define PRINT(f) if((f)){cout << (TRUE__) << endl;}else{cout << FALSE__ << endl;}
#define TRUE__  "Yes"
#define FALSE__ "No"
#define PERM(v) next_permutation(all(v))
#define UNIQUE(v) sort(all(v));(v).erase(unique(all(v)), v.end())
#define CIN(type, x) type x;cin >> x

#ifdef LOCAL
#define lcout(a) cout << a
#define lcoutln(a) cout << a << endl
#define lcerr(a) cerr << a
#define lcerrln(a) cerr << a << endl
#else
#define lcout(a) 
#define lcoutln(a) 
#define lcerr(a) 
#define lcerrln(a) 
#endif
#define MOD 1000000007

signed main()
{
	init();
	solve();
}

int q, xl, yl;
string x, y;

void init()
{
	cin >> q;
}

int dp[1010][1010];

void reset()
{
	rep(i, xl+2) rep(j, yl+2) dp[i][j] = 0;
}


void solve()
{
	rep(cnt, q) {
		cin >> x >> y;
		xl = x.length();
		yl = y.length();
		reps(i, 1, xl+1) reps(j, 1, yl+1) {
			if (x[i-1] == y[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
			else dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
		}
		cout << dp[xl][yl] << endl;
		reset();
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n) ; i++)
#define rrep(i,n) for(int i = n-1; i >= 0 ; i--)
constexpr auto INF = 2147483647;
typedef long long ll;

int main(){

  int q;
  cin >> q;
  string x, y;
  int len_x, len_y;
  rep(i,q){
    cin >> x >> y;
    len_x = x.size()+1; len_y = y.size()+1;
    vector<vector<int>> lscDP(len_x, vector<int>(len_y));
    rep(i,len_x)rep(j,len_y){
      if(i==0 || j == 0) lscDP[i][j] = 0;
      else if(x[i-1] == y[j-1]) lscDP[i][j] = max(lscDP[i-1][j-1] +1, max(lscDP[i-1][j], lscDP[i][j-1]));
      else lscDP[i][j] = max(lscDP[i-1][j], lscDP[i][j-1]);
    }

    cout << lscDP[len_x-1][len_y-1] << endl;
  }


  return 0;
}

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

using namespace std;

int sets;
string sentences[500];
int dp[500][1001][1001];

int main(){
    cin >> sets;
    for(int i = 0; i < sets*2; i++){
        cin >> sentences[i];
    }

    for(int i = 0; i < sets*2; i+=2){
        for(int n = 0; n < sentences[i].size(); n++){
            for(int m = 0; m < sentences[i+1].size(); m++){
                if(sentences[i][n] == sentences[i+1][m]){
                    dp[i][n+1][m+1]= dp[i][n][m] + 1;
                }else{
                    dp[i][n+1][m+1] = max(dp[i][n+1][m],dp[i][n][m+1]);
                }
            }
        }
    }

    for(int i = 0; i<sets*2; i+=2){
        cout << dp[i][sentences[i].size()][sentences[i+1].size()] << endl;
    }
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>
 
#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) static_cast<bitset<16> >(x);
#define if_range(x, w, h) if (w <= x && x <= h)
#define BitCheck(a,b)   (a >> b) & 1
#define BitSet(a,b)       a |= (1 << b)
#define BitunSet(a,b)    a &= ~(1 << b)
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
 
int compare_int(const void* a, const void* b) { return *(int*) a - *(int*) b; }
 
int main() {
    int n, m, a, dp[1002][1002] = { 0 };
    char s[1001], t[1001];
    scanf("%d", &a);
    for (int k = 0; a > k; k++) {
        scanf("%s %s", s, t);
        n = strlen(s);
        m = strlen(t);
        for (int i = 1; n >= i; i++) {
            for (int j = 1; m >= j; j++) {
                if (s[i - 1] == t[j - 1])dp[i][j] = dp[i - 1][j - 1] + 1;
                else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
        printf("%d\n", dp[n][m]);
    }
    return 0;
}