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 <stdlib.h>
#include <string.h>
#define MAX 150
#define L 1000

int LCSLength(char *X, char *Y){
  int m = strlen(X), n = strlen(Y);
  int i,j;
  int c[m+1][n+1];

  for(i=1; i<=m; i++) c[i][0] = 0;
  for(j=0; j<=n; j++) c[0][j] = 0;

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

  return c[i-1][j-1];
}

int main(void){
  int q,i,j;
  int LCS[MAX];
  char X[MAX][L], Y[MAX][L];

  // input & calculate
  scanf("%d",&q);
  for(i=0; i<q; i++){
    scanf("%s", &X[i]);
    scanf("%s", &Y[i]);
    LCS[i] = LCSLength(X[i],Y[i]);
  }

  // output
  for(i=0; i<q; i++) printf("%d\n", LCS[i]);

  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define REPR(i, m, n) for (int i = (int)(m); i >= (int)(n); i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const long long INF = 1LL << 60;

int q;
int dp[1100][1100];

int main(){
  cin >> q;
  rep(k, q){
    string x, y;
    cin >> x >> y;
    int n = x.size();
    int m = y.size();

    memset(dp, 0, sizeof(dp));
    rep(i, n){
      rep(j, m){
        if(x[i] == y[j]) chmax(dp[i+1][j+1], dp[i][j]+1);
        else{
          chmax(dp[i+1][j+1], dp[i][j+1]);
          chmax(dp[i+1][j+1], dp[i+1][j]);
        }
      }
    }

    cout << dp[n][m] << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <cmath>
#include <vector>

constexpr int MAX = 1000;
char ls[MAX];
char rs[MAX];
int table[MAX + 1][MAX + 1];


void calc_lcs() {
    scanf("%s", ls);
    scanf("%s", rs);
    int l_len = int(strlen(ls));
    int r_len = int(strlen(rs));
    for (int i = 0; i < l_len; i++) {
        table[i][0] = 0;
    }
    for (int i = 0; i < r_len; i++) {
        table[0][i] = 0;
    }

    for (int l_i = 1; l_i < l_len + 1; l_i++) {
        for (int r_i = 1; r_i < r_len + 1; r_i++) {
            if (ls[l_i - 1] == rs[r_i - 1]) {
                table[l_i][r_i] = table[l_i - 1][r_i - 1] + 1;
            } else {
                table[l_i][r_i] = std::max(table[l_i][r_i - 1], table[l_i - 1][r_i]);
            }
        }
    }
    std::cout << table[l_len][r_len] << std::endl;
}

int main() {
    int N;
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
        calc_lcs();
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define r(i,n) for(auto i=0;i<n;i++)
#define s(c) static_cast<int>((c).size())

int dp[1002][1002];

int main(){
	int n;
	cin >> n;
	int x, y;
	string X, Y;
	r(k, n){
		cin >> X >> Y;
		x = X.size();
		y = Y.size();
		X = ' ' + X;
		Y = ' ' + Y;

		r(i, x+1)
			r(j, y+1)
				dp[i][j] = 0;

		for (int i = 1; i <= x; i++) {
			for (int j = 1; j <= y; j++) {
				if (X[i] == Y[j]) {
					dp[i][j] = dp[i - 1][j - 1] + 1;
				} else {
					dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
				}
			}
		}

		cout << dp[x][y] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vin=vector<int>;
using vll=vector<long long>;
using vvin=vector<vector<int>>;
using vvll=vector<vector<long long>>;
using vstr=vector<string>;
using vvstr=vector<vector<string>>;
using vch=vector<char>;
using vvch=vector<vector<char>>;
using vbo=vector<bool>;
using vvbo=vector<vector<bool>>;
using vpii=vector<pair<int,int>>;
using pqsin=priority_queue<int,vector<int>,greater<int>>;
#define mp make_pair
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep2(i,s,n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(),v.end()
#define decp(n) cout<<fixed<<setprecision((int)n)
#define _GLIBCXX_DEBUG
int main(){
    int q;cin>>q;
    vstr x(q),y(q);
    rep(i,q)cin>>x[i]>>y[i];
    vin ans(q);
    rep(i,q){
        int sx=x[i].size();
        int sy=y[i].size();
        vvin dp(sx+1,vin(sy+1,0));
        rep(j,sx){
            rep(k,sy){
                if(x[i][j]==y[i][k])dp[j+1][k+1]=max(dp[j][k]+1,max(dp[j+1][k],dp[j][k+1]));
                else dp[j+1][k+1]=max(dp[j+1][k],dp[j][k+1]);
            }
        }
        ans[i]=dp[sx][sy];
    }
    rep(i,q)cout<<ans[i]<<endl;
}

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 = 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 NIL -1
#define INF 1000000001

using namespace std;


int main(){
    int num_dataset;
    char temp[1000];
    scanf("%d",&num_dataset);
    int numSubSeq[1001][1001];
    for(int i = 0;i < 1001;i++) numSubSeq[i][0] = 0; 
    for(int i = 0;i < 1001;i++) numSubSeq[0][i] = 0;

    for(int i = 0;i < num_dataset;i++){
        scanf("%s",temp);
        string str_A = temp;
        int size_A = (int)str_A.size();
        str_A = ' ' + str_A;
  
        scanf("%s",temp);
        string str_B = temp;
        int size_B = (int)str_B.size();
        str_B = ' ' + str_B;
     
        for(int j = 1;j <= size_A;j++){
            for(int k = 1;k <= size_B;k++){    
                if(str_A[j] == str_B[k]){
                    numSubSeq[j][k] = numSubSeq[j-1][k-1] + 1;
                }else{
                    numSubSeq[j][k] = max(numSubSeq[j-1][k],numSubSeq[j][k-1]);
                }
            }
        }
        printf("%d\n",numSubSeq[size_A][size_B]);
    }
    return 0;
}