Go: package main

import (
	. "fmt"
    "math"
)

// func init() { Println("----------------------------------------") }

func main() {
    const L_MAX = 1003

	var q int
	Scanf("%d", &q)
	X := make([]string, 0, 150)
	Y := make([]string, 0, 150)
	for i := 0; i < 2 * q; i++ {
	    var tmp string
	    Scanf("%s", &tmp)

	    if i % 2 == 0 {
	        X = append(X, tmp)
	    } else {
	        Y = append(Y, tmp)
	    }
	}

    for i := 0; i < q; i++ {
        dp := make([][]int, L_MAX)
        for k, _ := range dp {
            dp[k] = make([]int, L_MAX)
        }

        x, y := X[i], Y[i]

        // init
        isXFound := false
        for c := 0; c < len(x); c++ {
            if isXFound || y[0] == x[c] {
                dp[0][c] = 1
                isXFound = true
            }
        }
        isYFound := false
        for r := 0; r < len(y); r++ {
            if isYFound || x[0] == y[r] {
                dp[r][0] = 1
                isYFound = true
            }
        }

        for r := 1; r < len(y); r++ {
            for c := 1; c < len(x); c++ {
                // dp[r][c]
                if x[c] == y[r] {
                    dp[r][c] = int(math.Max(float64(dp[r - 1][c - 1] + 1), float64(dp[r][c])))
                }

                dp[r][c] = int(math.Max(float64(dp[r][c]), math.Max(float64(dp[r - 1][c]), float64(dp[r][c - 1]))))
            }
        }
        Println(dp[len(y) - 1][len(x) - 1])

        // LCSも求めておく
        // ans := ""
        // r, c := len(y) - 1, len(x) - 1
        // for r >= 0 && c >= 0 {
        //     if y[r] == x[c] {
        //         ans = string(x[c]) + ans
        //         r--
        //         c--
        //     } else {
        //         if dp[r - 1][c] > dp[r][c - 1] {
        //             r--
        //         } else {
        //             c--
        //         }
        //     }
        // }
        // Println(ans)
    }
}

C++: #include <stdio.h>
#include <string.h>

void CSLength(char *,char *);

int c[1000][1000],a[1000];

int main() {
  int i,j=0,q;
  char s[1000],x[1000],y[1000];

  scanf("%d",&q);

  for(i=0;i<q;i++){

    scanf("%s",s);
    strcpy(&x[1],s);

    scanf("%s",s);
    strcpy(&y[1],s);

    CSLength(x,y);
    a[i]=c[strlen(x)-1][strlen(y)-1];
  }

  for(i=0;i<q;i++){
    printf("%d\n",a[i]);
  }

  return 0;
}

void CSLength(char *x,char *y){
  int m,n,i,j;

  m = strlen(x);
  n = strlen(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 if(c[i-1][j] >= c[i][j-1]){
	c[i][j] = c[i-1][j];
      }
      else c[i][j] = c[i][j-1];
    }
  }
}

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();

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (X[i] == Y[j]) 
        if (i == 0 || j == 0) 
          c[i][j] = 1;
        else 
          c[i][j] = c[i - 1][j - 1] + 1;
      else
        c[i][j] = max(c[i][j - 1], c[i - 1][j]);
    }
  }
  return c[m - 1][n - 1];
}

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>
using namespace std;
int C[1001][1001];
int lc(string F, string S){
    fill(C[0],C[1000],0);
    int m = F.size(), n = S.size();
    int len=0;
    for(int i=0; i<m; i++){
        for (int j = 0; j < n; ++j) {
            if (F[i] == S[j]) {
                if (i - 1 >= 0 && j - 1 >= 0)
                    C[i][j] = C[i - 1][j - 1] + 1;
                else
                    C[i][j] = 1;
            } else if (i - 1 >= 0 && j - 1 >= 0) {
                C[i][j] = max(C[i][j - 1], C[i][j - 1]);
            }
            len = max(len, C[i][j]);
        }
    }
    return len;
}

int main() {
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    string ar[n*2];
    for (int i = 0; i < n * 2; ++i) {
        cin >> ar[i];
    }
    for (int j = 0; j < n*2; j+=2) {
        cout << lc(ar[j],ar[j+1]) << endl;
    }
    return 0;
}

C++: // 最長共通部分列 問題
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<stack>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<vector>

using namespace std;

#define MAX_LEN 1000
// 無効な値の定義
const int INV = -1;

int longestCommonSubstring(char a[], char b[]);

int main(void)
{

    int q, len;
    char first_str[MAX_LEN], second_str[MAX_LEN];

    cin >> q;

    for(int i = 0; i < q; i++)
    {
        cin >> first_str;
        cin >> second_str;

        len = longestCommonSubstring(&first_str[0], &second_str[0]);
        printf("%d\n", len);
    }



    return 0;
}

// 最長共通部分列を算出する関数
int longestCommonSubstring(char a[], char b[])

{
    int dp[MAX_LEN + 1][MAX_LEN + 1];
    int a_len, b_len;

    a_len = strlen(a);
    b_len = strlen(b);

    // 配列の初期化
    for(int i = 0; i < a_len; i++)
    {
        for(int j = 0; j < b_len; j++)
        {
            dp[i][j] = INV;
        }
    }

    for(int i = 0; i < a_len; i++)
    {
        dp[i][0] = 0;
    }

    for(int j = 0; j < b_len; j++)
    {
        dp[0][j] = 0;
    }

    // 最長共通部分列の算出
    for(int i = 1; i <= a_len; i++)
    {
        for(int j = 1; j <= b_len; j++)
        {
            if( a[i - 1] == b[j - 1] )
            {
                // 最後の文字が一致している時
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }
            else
            {
                // 最後の文字が一致していない時
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }

	return dp[a_len][b_len];
}

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

#define STR_MAX 1000

int mat[STR_MAX+1][STR_MAX+1];

int common_subsequence(int len1, int len2, string str1, string str2){
  int i, j;

  for(i=1; i<=len1; i++){
    for(j=1; j<=len2; j++){
      if(str1[i-1] == str2[j-1]) mat[i][j]=mat[i-1][j-1]+1;
      else mat[i][j]=max(mat[i-1][j], mat[i][j-1]);
    }
  }

  return mat[len1][len2];
}

int main(void){
  int i, n;
  string str1, str2;

  scanf("%d", &n);
  for(i=0; i<n; i++){
    cin >> str1 >> str2;

    int len1=str1.size(), len2=str2.size();
    printf("%d\n", common_subsequence(len1, len2, str1, str2));
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

int main() {
  int q; cin >> q;
  vector<vector<string>> retsu(150, vector<string>(2));
  rep(i, q) {
    cin >> retsu[i][0] >> retsu[i][1];
//    cout << retsu[i][0] << retsu[i][1] << endl;
  }
  
  rep(i, q) {
    string a_1 = retsu[i][0], a_2 = retsu[i][1];
    int b_1 = (int)a_1.size(), b_2 = (int)a_2.size();
//    cout << b_1 << b_2 << endl;
    
    int dp[b_1 + 1][b_2 + 1];
    memset(dp, 0, sizeof(dp));
    rep(j, b_1) {
      rep(k, b_2) {
        if (a_1[j] == a_2[k]) {
          dp[j+1][k+1] = dp[j][k] + 1;
        }
        else {
          dp[j+1][k+1] = max(dp[j+1][k], dp[j][k+1]);
        }
      }
    }
    
    cout << dp[b_1][b_2] << endl;
    
  }

}

C++: #include <iostream>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <time.h>

using namespace std;

void solve(){
	int n, m;
	time_t start, end;

	cin >> n;
	string A;
	/*for (int i = 0; i < n; i++)*/ cin >> A;
	cin >> m;
	string B;
	cin >> B;
	vector<vector<int>> DP(n + 1, vector<int>(m + 1, 0));
	start = clock();
	
	//for (int i = 0; i < n; i++) DP[i][0] = 0;
	//for (int j = 0; j < m; j++) DP[0][j] = 0;

	for (int i = 1; i <= n; i++){
		for (int j = 1; j <= m; j++){
			if (A[i - 1] == B[j - 1]) DP[i][j] = DP[i - 1][j - 1] + 1;
			else DP[i][j] = max(DP[i - 1][j], DP[i][j - 1]);
		}
	}

	end = clock();

	cout << DP[n][m] << endl;

}

int main(){
	int n;
	cin >> n;
	for (int i = 0; i < n; i++){
		solve();
	}
}