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 <bits/stdc++.h>
typedef long long ll;
using namespace std;
ll a7=1000000007;
ll inf=1000000000000000;
typedef pair<ll,ll> l_l;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back


ll min(ll a,ll b){
    if(a<b)return a;
    return b;
}
ll max(ll a,ll b){
    if(a>b)return a;
    return b;
}

ll Max(ll a,ll b,ll c){
    if(a<0){
        a=-inf;
    }
    if(b<0){
        b=-inf;
    }
    if(c<0){
        c=-inf;
    }
    ll ans=max(max(a,b),c);
    if(ans<0){
        return -2;
    }else{
        return ans;
    }
}

int main(){
    ll q;cin>>q;
    string x,y;
    ll ans[q];
    ll dp[2000][2000];  //xのi文字目までと、yのj文字目までの最長の長さ
    rep(ii,0,q-1){
        cin>>x>>y;
        rep(i,0,y.size()){
            dp[0][i]=0;
        }
        rep(i,0,x.size()-1){
            rep(j,0,y.size()-1){
                if(x[i]==y[j]){
                    dp[i+1][j+1]=Max(dp[i][j]+1,dp[i+1][j],dp[i][j+1]);
                }else{
                    dp[i+1][j+1]=Max(dp[i][j],dp[i+1][j],dp[i][j+1]);
                }
                //cout<<dp[i+1][j+1]<<' ';
            }
            //cout<<endl;
        }
        ans[ii]=dp[x.size()][y.size()];
    }
    rep(i,0,q-1){
        cout<<ans[i]<<endl;
    }
    return 0;
}

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

string X;
string Y;

int calc(int x, int y, vector<vector<int> >& Memo){
    if (X[x] == '\0' || Y[y] == '\0') return 0;
    if (Memo[x][y] != -1) return Memo[x][y];

    int unuse = calc(x + 1, y, Memo);
    int use = 0;
    int y_target = Y.find(X[x], y);
    if (y_target == string::npos) {
        use = 0;
    } else {
        use = calc(x + 1, y_target + 1, Memo) + 1;
    }
    return Memo[x][y] = max(unuse, use);
}

int main(){
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> X;
        cin >> Y;
        vector<int> tmp(Y.size(), -1);
        vector<vector<int> > Memo(X.size(), tmp);
        cout << calc(0, 0, Memo) << endl;
    }

    return 0;
}

C++: #include<cstdio>
#include<cstring>
#define N 1000

int LCS[N][N];

int LCSLength(char X[], int m, char Y[], int n){
	for(int i=0;i<=m;i++)
		LCS[i][n] = 0;
	for(int j=0;j<=n;j++)
		LCS[m][j] = 0;
	for(int i=m-1;i>=0;i--){
		for(int j=n-1;j>=0;j--){
			LCS[i][j] = LCS[i+1][j+1];
			if(X[i] == Y[j])
				LCS[i][j]++;
			if(LCS[i][j+1] > LCS[i][j])
				LCS[i][j] = LCS[i][j+1];
			if(LCS[i+1][j] > LCS[i][j])
				LCS[i][j] = LCS[i+1][j];
		}
	}
	return LCS[0][0];
}

int main(){
	char X[N],Y[N];
	int num;
	scanf("%d",&num);
	for(int i=1;i<=num;i++){
		scanf("%s %s",X,Y);
		int m = strlen(X);
		int n = strlen(Y);
		printf("%d\n",LCSLength(X,m,Y,n));
	}
	return 0;
}

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

const int N = 1000;

int lcs(string s1, string s2) {
    int c[N + 1][N + 1];
    int m = s1.size();
    int n = s2.size();
    s1 = ' ' + s1;
    s2 = ' ' + s2;
    c[0][0]  = 0;
    for (int i = 1; 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 (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]);
            }
        }
    }
    return c[m][n];
}

int main() {
    int n;
    string s1, s2;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << "\n";
    }
    return 0;
}

C++: #include<iostream>
using namespace std;
#include<algorithm>
#include<string>
int n, m;
string s, t;
int dp[1001][1001];
int main()
{
    int p;
    cin >> p;
    while(p>0)
    {
        p--;
        cin >> s >> t;
        int n = s.size();
        int m = t.size();
        for (int i = 1; i <= n; i++)
            dp[i][0] = 0;
        for (int i = 0; i < m; i++)
            dp[0][i] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (s[i] == t[i]) {
                    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;
    }

}

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

#define MAX 1000

int lcs(string X, string Y) {
    int c[MAX+1][MAX+1];
    int m = X.size();
    int n = Y.size();
    int i, j;
    int maxlen = 0;
    X = " " + X;
    Y = " " + Y;

    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]);
            }
            maxlen = max(maxlen, c[i][j]);
        }
    }
    return maxlen;
}

int main (void) {
    int N, i;
    string x, y;

    cin >> N;

    for(i = 0 ; i < N ; i++) {
        cin >> x;
        cin >> y;
        printf("%d\n", lcs(x,y));
    }

    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 = 0;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 <cstring>
using namespace std;

#define MIN(X,Y) ((X)<(Y)?(X):(Y))
#define MAX(X,Y) ((X)>(Y)?(X):(Y))

int dp[1001][1001];

int main(void){
  char x[1001], y[1001];
  int xln, yln;
  int dataNum;
  
  cin >> dataNum;
  for (int did=0; did<dataNum; did++){
    cin >> &(x[1]) >> &(y[1]);
    xln = strlen(x+1); yln = strlen(y+1);
    // dp tableの初期化
    for (int i=0; i<=xln; i++) dp[i][0] = 0;
    for (int j=0; j<=yln; j++) dp[0][j] = 0;
    // dp
    for (int i=1; i<=xln; i++){
      for (int j=1; j<=yln; 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]);
        }
      }
    }
/*    for (int i=0; i<=yln; i++){
      for (int j=0; j<=xln;j++){
        cout << dp[j][i] << " ";
      }
      cout << endl;
    }*/
    cout << dp[xln][yln] << endl;
  }
  return 0;
}