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
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <tuple>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s)
{
    int v;
    istringstream sin(s);
    sin >> v;
    return v;
}
template <class T>
inline string toString(T x)
{
    ostringstream sout;
    sout << x;
    return sout.str();
}

//math
//-------------------------------------------
template <class T>
inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0, sizeof(a))

//debug
// #define dump(x) cerr << #x << " = " << (x) << endl;
// #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
//                       << " " << __FILE__ << endl;

class LCS_Solver
{
    vector<VL> vl;
    string &s1;
    string &s2;

public:
    LCS_Solver(size_t s, string &s1, string &s2)
        : vl(s, VL(s, -1)), s1(s1), s2(s2)
    {
        vl[0][0] = 0;
        REP(i, s)
        {
            vl[0][i] = 0;
            vl[i][0] = 0;
        }
    }

    LL solve_memoize(int s1n, int s2m);
    LL solve_dp(int s1n, int s2m) const;
};

LL LCS_Solver::solve_memoize(int s1n, int s2m)
{
    // cout << s1n << ", " << s2m << endl;
    if (vl[s1n][s2m] >= 0)
    {
        return vl[s1n][s2m];
    }
    else
    {
        LL ans;
        if (s1[s1n - 1] == s2[s2m - 1])
        {
            ans =
                solve_memoize(s1n - 1, s2m - 1) + 1;
            vl[s1n][s2m] = ans;
            return ans;
        }
        else
        {
            ans = max(
                solve_memoize(s1n - 1, s2m),
                solve_memoize(s1n, s2m - 1));
            vl[s1n][s2m] = ans;
            return ans;
        }
    }
}

const size_t MAX_SIZE = 2000;

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

    LL ans;
    REP(_, n)
    {
        string s1, s2;
        cin >> s1;
        cin >> s2;
        // cout << s1.size() << " " << s2.size() << endl;

        ans = LCS_Solver(MAX_SIZE, s1, s2)
                  .solve_memoize(s1.size(), s2.size());
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define  all(x) (x).begin(),(x).end()
typedef long long ll;

int c[1005][1005];

int lcs(string s1, string s2){
    int n = s1.size();
    int m = s2.size();
    s1 = " " + s1;
    s2 = " " + s2;
    for(int i = 0; i <= n; i++) c[i][0] = 0;
    for(int i = 0; i <= n; i++) c[0][i] = 0;

    int ans = 0;
    for (int i1 = 1; i1 <= n; i1++){
        for(int i2 = 1; i2 <= m; i2++){
            if(s1[i1] == s2[i2]){
                c[i1][i2] = c[i1-1][i2-1] + 1;
            }else{
                c[i1][i2] = max(c[i1-1][i2], c[i1][i2-1]);
            }
        }
        ans = max(ans, c[i1][m]);
    }
    return ans;
}

int main(){
    int q;
    cin >> q;
    vector<int> ans(q);
    for(int i = 0; i < q; i++){
        string s1, s2;
        cin >> s1 >> s2;
        ans[i] = lcs(s1, s2);
    }
    rep(i,q) cout << ans[i] << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

int main(){
    int T;
    cin >> T;
    while(T--){
        string a, b;
        cin >> a >> b;
        int M = sz(a), N = sz(b);
        int dp[M+1][N+1];
        rep(i, M+1) dp[i][0] = 0;
        rep(j, N+1) dp[0][j] = 0;
        rep(i, M){
            rep(j, N){
                if(a[i] == b[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[M][N] << 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], maxl = 0, i, j;
  int m = x.size();
  int n = y.size();

  x = ' ' + x;
  y = ' ' + y;
 
  for(i = 1; i <= m; i++) c[i][0] = 0;
  for(i = 1; i <= n; i++) c[0][i] = 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 - 1][j], c[i][j - 1]);
      maxl = max(maxl, c[i][j]);
    }
  }
  return maxl;
}

int main(){
  string x, y;
  int q, i;

  cin >> q;
  
  for(i = 0; i < q; i++){
    cin >> x >> y;
    cout << lcs(x, y) << endl;
  }
  return 0;
}
  
  for(i = 0; i < q; i++){
    cin >> x >> y;
    cout << lcs(x, y) << endl;
  }
  return 0;
}

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

using namespace std;

int q;
char X[1000], Y[1000], dp[1001][1001];

int main(void) {

    scanf("%d", &q);

    for (int i = 0; i < q; ++i) {
        scanf("%s %s", X, Y);
        int xlen = strlen(X), ylen = strlen(Y);
        for (int x = 0; x < xlen + 1; ++x) {
            for (int y = 0; y < ylen + 1; ++y) {
                if (X[x] == Y[y]) {
                    dp[x + 1][y + 1] = dp[x][y] + 1;
                }
                else {
                    if (dp[x + 1][y] > dp[x][y]) {
                        dp[x + 1][y + 1] = dp[x + 1][y];
                    }
                    else {
                        dp[x + 1][y + 1] = dp[x][y + 1];
                    }
                }

            }
        }
        printf("%d\n", dp[xlen][ylen]);
    }

    return 0;
}