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 <bits/stdc++.h>
using namespace std;
 
#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define FOR(e, c) for (auto &e : c)
#define SORT(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define rvisort(v) sort(v.begin(), v.end(), greater<int>());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define rv(s) reverse(s.begin(),s.end());
 
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vs = vector<string>;
using vd = vector<double>;
using ul = unsigned long;
 
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; }

int GCD(int a, int b) { return b ? GCD(b, a%b) : a; }

int main()
{
  cin.tie( 0 );
  ios::sync_with_stdio( false );
  
  int q;cin>>q;
  
  rep(ki,q){
    string x,y;
    cin>>x>>y;
    int m=x.size();
    int n=y.size();
    int dp[1001][1001];
    
    rep(i,m)dp[i][0]=0;
    rep(i,n)dp[0][i]=0;
    
    for(int i=1;i<=m;i++){
      for(int j=1;j<=n;j++){
        if(x[i-1]==y[j-1]){
          dp[i][j]=dp[i-1][j-1]+1;
        }else{
          dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
        }
      }
    }
    
    cout(dp[m][n]);
  }
}

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

int main(){
	string X, Y;
	int q;
	vector<vector<int> > dp;

	cin>>q;
	for(int i=0;i<q;i++){
		cin>>X;
		cin>>Y;

		dp.resize(X.length()+1);
		for(int j=0;j<=X.length();j++)
			dp[j].resize(Y.length()+1);

		for(int j=0;j<=X.length();j++)
			dp[j][0] = 0;
		for(int k=0;k<=Y.length();k++)
			dp[0][k] = 0;

		for(int j=1;j<=X.length();j++){
			for(int k=1;k<=Y.length();k++){
				if(X[j-1] == Y[k-1])
					dp[j][k] = dp[j-1][k-1] + 1;
				else
					dp[j][k] = max(dp[j-1][k], dp[j][k-1]);
			}
		}

		cout<<dp[X.length()][Y.length()]<<endl;
	}

	return 0;
}

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

static const int N = 1000;

int lcs(string X, string Y) {
	int i, j, c[N+1][N+1];
	int m = X.size(), n = Y.size(), max1 = 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-1][j], c[i][j-1]);
			max1 = max(max1, c[i][j]);
		}
	}
	return max1;
}

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

	return 0;
}

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

int solve(string s, string t) {
  int ss = s.size();
  int tt = t.size();
  vector<vector<int> > dp(ss+1, vector<int>(tt+1, 0));
  s.insert(0, " ");
  t.insert(0, " ");
  
  int ret = 0;
  rep(i, 1, ss+1) {
    rep(j, 1, tt+1) {
      if(s[i] == t[j])
        dp[i][j] = dp[i-1][j-1] +1;
      else
        dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
      ret = max(ret, dp[i][j]);
    }
  }
  return ret;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  int q;
  cin >> q;
  rep(aa, 0, q) {
    string x, y;
    cin >> x >> y;
    cout << solve(x, y) << "\n";
  }
}

C++: #if 0
PDFに記載されている方針がわかりやすかったので、比較的簡単に解くことができた。
#endif

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int Q;
int L[1000+10][1000+10];

int main() {
  cin >> Q;
  string X, Y;
  for (int i = 0; i < Q; i++) {
    for (int j = 0; j < 1000+10; j++) {
      L[0][j] = 0; L[j][0] = 0;
    }
    cin >> X >> Y;
    for (int x = 1; x <= X.length(); x++) {
      for (int y = 1; y <= Y.length(); y++) {
        if (X[x-1] == Y[y-1])
          L[x][y] = L[x-1][y-1] + 1;
        else
          L[x][y] = max(L[x-1][y], L[x][y-1]);
      }
    }
    cout << L[X.length()][Y.length()] << endl;
  }
}

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

#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
template<class T> using V = vector<T>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
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; }


int lcs(string s, string t) { // 最長共通部分列長
  int n = s.size();
  int m = t.size();
  vector<vector<int>> dp(n+1,vector<int>(m+1));
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
      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+1][j+1] = max(dp[i+1][j+1], dp[i][j+1]);
    }
  }
  return dp[n][m];
}


int main(){
    int n;
    cin >> n;
    rep(i,n) {
        string s, t;
        cin >> s >> t;
        int ans = lcs(s,t);
        cout << ans << endl;
    }
}

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

using namespace std;

int dp[1010][1010];

int main() {
    int q;  cin >> q;
    int *ans = new int[q];
    string s, t;
    int ls, lt, i, j;
    for (int k = 0; k < q; k++) {
        cin >> s >> t;
        ls = s.size(), lt = t.size();

        for (i = 0; i <= ls; i++) {
            for (j = 0; j <= lt; j++)
                dp[i][j] = 0;
        }

        for (i = 0; i < ls; i++) {
            for (j = 0; j <= lt; j++) {
                if (s[i] == t[j])
                    dp[i+1][j+1] = max(dp[i][j]+1, max(dp[i+1][j], dp[i][j+1]));
                else
                    dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
            }
        }   
        ans[k] = dp[ls][lt];
    }
    for (int k = 0; k < q; k++)
        cout << ans[k] << endl;
    return 0;
}