Go: // AOJ ALDS1_10_C: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C

package main

import (
	"fmt"
)

func main() {
	var dataNum int
	var s1, s2 string

	fmt.Scanf("%d", &dataNum)

	for i := 0; i < dataNum; i++ {
		fmt.Scanf("%s", &s1)
		fmt.Scanf("%s", &s2)

		lcs := make([][]int, len(s1)+1)
		for j := 0; j < len(s1)+1; j++ {
			lcs[j] = make([]int, len(s2)+1)
		}

		for m, vs1 := range s1 {
			for n, vs2 := range s2 {
				if vs1 == vs2 {
					lcs[m+1][n+1] = lcs[m][n] + 1
				} else if lcs[m+1][n] > lcs[m][n+1] {
					lcs[m+1][n+1] = lcs[m+1][n]
				} else {
					lcs[m+1][n+1] = lcs[m][n+1]
				}
			}
		}

		// Answer
		fmt.Println(lcs[len(s1)][len(s2)])
	}
}

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

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    int n;
    std::cin >> n;
    std::string s1, s2;
    for (int q=0; q<n; q++) {
        std::cin >> s1 >> s2;
        int m1 = s1.size()+1;
        int m2 = s2.size()+1;
        int* dp = (int*)malloc(m1*m2*sizeof(int));
        for (int i=0; i<m1; i++) {
            dp[i*m2]=0;
        }
        for (int i=0; i<m2; i++) {
            dp[i]=0;
        }
        for (int i=1; i<m1; i++) {
            int leftmax=0;
            for (int j=1; j<m2; j++) {
                dp[i*m2+j] = leftmax;
                if (s1[i-1] == s2[j-1]) {
                    dp[i*m2+j]+=1;
                }
                if (dp[(i-1)*m2+j] > leftmax) {
                    leftmax = dp[(i-1)*m2+j];
                }
                if (dp[i*m2+j] < dp[(i-1)*m2+j]) {
                    dp[i*m2+j] = dp[(i-1)*m2+j];
                }
            }
        }
        int max=0;
        for (int i=0; i<m2; i++) {
            if (dp[(m1-1)*m2+i] > max) {
                max = dp[(m1-1)*m2+i];
            }
        }
        std::cout << max << "\n";
        free(dp);
    }

    return 0;
}

C++: #include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn = 1005;

int dp[maxn][maxn];
int len1, len2;
string s1, s2;

int lcs()
{
	memset(dp, 0, sizeof(dp));
	for (int i=1;i<=len1;i++)
		for (int j = 1; j <= len2; j++) {
			if (s1[i] == s2[j]) {
				dp[i][j] = dp[i - 1][j - 1] + 1;
			}
			else
			{
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
			}
		}
	return dp[len1][len2];
}

int main()
{
	int n;
	cin >> n;
	while (n--)
	{
		cin >> s1 >> s2;
		len1 = s1.size(), len2 = s2.size();
		cout << lcs() << endl;
	}
	return 0;
}

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

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

// 最長共通部分列 ... len(s) == m, len(t) == n とすると, O(mn)
int lcs(std::string s, std::string t) {
  std::vector<std::vector<int> > dp(s.size() + 1, std::vector<int>(t.size() + 1, 0));

  for (int i = 0; i < s.size(); ++i) {
    for (int j = 0; j < t.size(); ++j) {
      if (s[i] == t[j]) {
        dp[i][j] = (i == 0 ? (j == 0 ? 1 : 0) : ;
      }
      else if (i >= 0 && j >= 0 && dp[i - 1][j] >= dp[j][i - 1]) {
        dp[i][j] = dp[i - 1][j];
      }
      else if (i >= 0 && j >= 0) {
        dp[i][j] = dp[i][j - 1];
      }
    }
  }

  return dp[s.size()][t.size()];
}

int main() {
  int q;
  std::string s, t;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> s >> t;
    cout << lcs(s, t) << endl;
  }

  return 0;
}

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

#define ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(int i = 0; i < (n); i++)
#define Rep(i, n) for(int i = 1; i < n; i++)

const int mod = 1000000007;
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vii = vector<pair<int, int>>;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return (a * b) / GCD(a, b); }
int round_int(int a, int b) { return (a + (b - 1)) / b; }

// 素因数分解; 返り値は(素数, 乗数)のvector配列
vector<pair<ll, int>> factorize(ll n) {
	vector<pair<ll, int>> res;
	for (ll i = 2; i * i <= n; ++i) {
		if (n % i) continue;
		res.emplace_back(i, 0);
		while (n % i == 0) {
			n /= i;
			res.back().second++;
		}
	}
	if (n != 1) res.emplace_back(n, 1);
	return res;
}

constexpr array<int, 9> dx = { 0, 1, 0, -1, -1, 1, 1, -1, 0 };
constexpr array<int, 9> dy = { 1, 0, -1, 0, 1, 1, -1, -1, 0 };
constexpr auto MAX = 100000;

// ──────────────────────────────────────────────────────────────────

int dp[1010][1010];

int main() {
	int n;
	cin >> n;

	rep(i, n) {
		string s, t;
		cin >> s >> t;

		memset(dp, 0, sizeof(dp));
		rep(i, s.size()) {
			rep(j, t.size()) {
				if (s[i] == t[j]) dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
				dp[i + 1][j + 1] = max({ dp[i + 1][j + 1], dp[i + 1][j], dp[i][j + 1] });
			}
		}
		cout << dp[s.size()][t.size()] << endl;
	}
}

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

using namespace std;

int main(){
  int n;
  int k;
  int count;
  int min;
  cin>>n;
  char X[1001];
  char Y[1001];
  for(int i=0;i<n;i++){
    cin>>X;
    cin>>Y;
    k=0;
    count=0;
    min=100000;
    for(int a=0;X[a]!='\0';a++){
      for(int i=a;X[i]!='\0';i++){
	for(int j=k;Y[j]!='\0';j++){
	  if(X[i]==Y[j]){
	    count++;
	    k=j;
	    break;
	  }
	}
      }
      if(min>count)min=count;
    }
    cout<<min<<endl;
    
  }
  return 0;
}

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

using namespace std;

int main() {
    int a;
    string s,t;
    cin >> a;

    for(int ai = 0; ai < a; ai++) {
        cin >> s >> t;
        int n = s.length(), m = t.length();

        int dp[n+1][m+1];
        fill_n(dp[0], (n+1)*(m+1), 0);
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                if(s[i] == t[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]);
                }
            }
        }

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

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

using namespace std;

int larger(int x, int y){
	if(x > y){
		return x;
	}
	return y;
}

int main(){
	int Q;
	cin >> Q;
	for(int i = 0; i < Q; i++){
		string X, Y;
		cin >> X >> Y;
		int lenX = X.size();
		int lenY = Y.size();
		int dp[lenX+1][lenY+1];
		for(int j = 0; j <= lenX; j++){
			for(int k = 0; k <= lenY; k++){
				dp[j][k] = 0;
			}
		}
		for(int j = 0; j < lenX; j++){
			for(int k = 0; k < lenY; k++){
				if(X[j] == Y[k]){
					dp[j+1][k+1] = dp[j][k] + 1;
				}
				else{
					dp[j+1][k+1] = larger(dp[j+1][k], dp[j][k+1]);
				}
			}
		}
		cout << dp[lenX][lenY] << "\n";
	}
}