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 <cstdio>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

template <class RandomIt>
size_t lcsubsequence(
    RandomIt first1, RandomIt last1, RandomIt first2, RandomIt last2
) {
    vector<vector<size_t>> dp(last1-first1+1, vector<size_t>(last2-first2+1));
    for (RandomIt it=first1; it<last1; ++it)
        for (RandomIt it2=first2; it2<last2; ++it2) {
            if (*it == *it2) {
                dp[it-first1+1][it2-first2+1] = dp[it-first1][it2-first2] + 1;
            } else {
                dp[it-first1+1][it2-first2+1] = max(
                    dp[it-first1][it2-first2+1], dp[it-first1+1][it2-first2]
                );
            }
        }

    return dp[last1-first1][last2-first2];
}

int main() {
    size_t q;
    scanf("%zu", &q);

    for (size_t i=0; i<q; ++i) {
        char buf[1<<10];
        scanf("%s", buf);
        string X(buf);

        scanf("%s", buf);
        string Y(buf);

        printf("%zu\n", lcsubsequence(X.begin(), X.end(), Y.begin(), Y.end()));
    }

    return 0;
}

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

using namespace std;

string s1, s2;

int lcs() {
  int i, j;
  int dp[s1.length()+1][s2.length()+1] = {};
  
  for(i = 1; i <= s1.length(); i++) {
    for(j = 1; j <= s2.length(); j++) {
      if(s1[i-1] == s2[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[s1.length()][s2.length()];
}

int main(){
  int n;
  cin >> n;
  while(n--) {
    cin >> s1 >> s2;
    cout << lcs() << endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
#include <tuple>

#define REP(i, n) for(int i=0;i<(int)(n);i++)
#define ALL(x) (x).begin(),(x).end()
const int INF = 1e9;

using namespace std;
int dp[1005][1005];

void dfs(int i, int j, string q, string b){
    int x = j;
    string tar = b.substr(x, 1);
    while(x < b.length() && q != tar){
        x++;
        tar = b.substr(x, 1);
    }
    if(i == 0){
        while(x < b.length()){
            x++;
            dp[i][x] = 1;
        }
        return;
    }
    while(x < b.length()){
        x++;
        dp[i][x] = max(dp[i-1][j] + 1, dp[i][x]);
    }
    dp[i][j] = max(dp[i][j], dp[i-1][j]);
    return;
}

int main() {
    int n; cin >> n;
    REP(k, n){
        REP(i, 1005) REP(j, 1005) dp[i][j] = 0;
        string a, b;
        cin >> a >> b;
        REP(i, a.length()){
            string q = a.substr(i, 1);
            REP(j, b.length()+1) dfs(i, j, q, b);
        }
        cout << dp[a.length()-1][b.length()] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    string s,t;
    int n,m,q;
    cin >> q;
    rep(_,q){
        cin >> s >> t;
        int n = s.size();
        int m = t.size();
        // dp[i][j] := sをi文字目まで見てtをj文字目まで見た時の最大の共通部分列の長さ
        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] = dp[i][j] + 1;
                }
                chmax(dp[i+1][j+1],max(dp[i][j+1],dp[i+1][j]));
            }
        }
        cout << dp[n][m] << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int N=1001;
int lcs(string X,string Y){
  int c[N+1][N+1];
  int m=X.length();
  int n=Y.length();

  int max1=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]);
      }
      max1=max(max1,c[i][j]);
    }
  }
  return max1;
}

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 <cstdio>
#include <iostream>

using namespace std;

int main(){
	int q;
	scanf(" %d", &q);
	for(int i=0; i<q; ++i){
		string a, b;
		cin >> a >> b;
		
		int m=a.size(), n=b.size();
		
		string x=" "+a;
		string y=" "+b;
		
		int c[1001][1001]={0};
		for(int i=1; i<=m; ++i) c[i][0]=0;
		for(int i=1; i<=n; ++i) c[0][n]=0;
		
		int ans=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][j-1], c[i-1][j]);
				
				ans=max(ans,c[i][j]);		
			}
		}
		
		printf("%d\n", ans);
	}
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <climits>
#include <tuple>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef tuple< int,int,int > T;
const int MAX_N=55555;
bool is_prime[MAX_N];
int prime[MAX_N];
int main(){
  int n;
  cin>>n;
  int dp[1001][1001];
  for(int nn=0;nn<n;nn++){
    string s1,s2;
    cin>>s1>>s2;
    for(int i=0;i<1001;i++)fill(dp[i],dp[i]+1001,0);
    for(int i=0;i<s1.size();i++){
      for(int j=0;j<s2.size();j++){
        if(s1[i]==s2[j]) dp[i+1][j+1]=max(dp[i][j]+1,max(dp[i][j+1],dp[i+1][j]));
        else dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
      }
    }
    cout<<dp[s1.size()][s2.size()]<<endl;
  }
  return 0;
}

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

vector<vector<int>> c(1001,vector<int>(1001));

int lcs(string &X,string &Y){
    int m=X.size();
    int n=Y.size();
    X=' '+X;
    Y=' '+Y;
    for(int i=0;i<m+1;i++) c[i][0]=0;
    for(int i=1;i<n+1;i++) c[0][i]=0;
    for(int i=1;i<m+1;i++){
        for(int j=1;j<n+1;j++){
            if(X[i]==Y[j]){
                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][n];
}

int main(){
    int q;
    cin>>q;
    for(int i=0;i<q;i++){
        string X,Y;
        cin>>X>>Y;
        cout<<lcs(X,Y)<<endl;
    }
}