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>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

const int MAX_SIZE = 52;

int LCS(const string& a, const string& b){
    int n = (int)a.size(), m = (int)b.size();
    vector<int> x(n), y(m);
    rep(i,n){
        x[i] = a[i]-'a';
        if(x[i] < 0){
            x[i] = a[i]-'A'+26;
        }
    }
    rep(i,m){
        y[i] = b[i]-'a';
        if(y[i] < 0){
            y[i] = b[i]-'A'+26;
        }
    }
    vector<vector<int> > vec(MAX_SIZE);
    for(int j = m-1; j >= 0; --j){
        vec[y[j]].push_back(j);
    }
    vector<int> xs(n+1, INF);
    xs[0] = -1;
    vector<P> nx(n+1,P(-INF,-INF));
    rep(i,n){
        vector<int>& ys = vec[x[i]];
        rep(j,(int)ys.size()){
            int k = lower_bound(xs.begin(),xs.end(),ys[j]) - xs.begin();
            xs[k] = ys[j];
            nx[k] = P(y[ys[j]], k-1);
        }
    }
    int l = lower_bound(xs.begin(),xs.end(),INF-1) - xs.begin();
    int id = l-1;
    int ans = 0;
    while(true){
        id = nx[id].second;
        if(id == -INF) break;
        ans++;
    }
    return ans;
}


int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int q;
    cin >> q;
    rep(i,q){
        string a,b;
        cin >> a >> b;
        cout << LCS(a,b) << "\n";
    }
    return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
 
void lcs(string X, string Y) {
    int xLength = X.size();
    int yLength = Y.size();
 
    int dp[xLength + 10][yLength + 10];  
    int i,j;

    // initialize dp[][]
    for (i = 1; i <= xLength; i++)
        dp[i][0] = 0;
    for (i = 1; i <= yLength; i++)
        dp[0][i] = 0;
 
    for (i = 1; i <= xLength; i++){
        for (j = 1; j <= yLength; j++){
            // Because the original string is 0-based , so subtracted from 1
            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]);
            }
        }
    }
 
    // testing code: print out the dp[][]
    // cout << "j";
    // for (j = 1; j <= yLength; j++)
    //  cout << ' ' << j ;
    // cout << endl;
         
 
    // for (i = 1; i <= xLength; i++){
    //  cout << i << '.';
    //  for (j = 1; j <= yLength; j++){
    //      cout << dp[i][j] << ' ';
    //  }
    //  cout << endl;
    // }
 
    cout << dp[xLength][yLength] << endl;
}
 
int main() {
    int n , i;
    string X, Y;
 
    cin >> n;
    for (i = 0 ; i < n; i++ ) {
        cin >> X;
        cin >> Y;
        lcs(X, Y);
    }
 
    return 0;
 
}

C++: #include <iostream>
#include <string>
using namespace std;
int main(void){
    int n;
    cin >> n;
    for(int i = 0;i < n;i++){
        string X,Y;
        cin >> X >> Y;
        int L[X.length()+10][Y.length()+10];
        for(int j = 0;j < X.length()+10;j++){
            for(int k = 0;k < X.length()+10;k++){
                L[j][k] = 0;
            }
        }
        for(int j = 1;j<=X.length();j++){
            for(int k = 1;k<=Y.length();k++){
                if(Y[k] == X[j]){
                    L[j][k] = 1+L[j-1][k-1];
                }
                else{
                    L[j][k] = max(L[j-1][k],L[j][k-1]);
                }
            }
        }
        cout << L[X.length()][Y.length()] << endl;
    }
}

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

int main(){
    int q;
    cin >> q;
    int dp[1001][1001] = {0};
    for(int i = 0; i < q; i++){
        bool flagx[1001] = {0}, flagy[1001] = {0};
        string S1, S2;
        cin >> S1 >> S2;
        const int m = S1.size();
        const int n = S2.size();
        for(int j = 0; j < m; j++){
            for(int k = 0; k < n; k++){
                if(j == 0 && k == 0) dp[j][k] = 0;
                else if(j == 0) dp[j][k] = dp[j][k-1];
                else if(k == 0) dp[j][k] = dp[j-1][k];
                else{
                    dp[j][k] = max(dp[j-1][k],dp[j][k-1]);
                }
                if(S1[j] == S2[k] && !flagx[j] && !flagy[k]) {
                    //cout << j << " " << k << " " << dp[j][k]++ <<endl;
                    dp[j][k]++;
                    flagx[j] = 1;
                    flagy[k] = 1;
                }
            }

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

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

int q;
string X, Y;


int main(void) {
  int dp[1000][1000];
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> X >> Y;

    dp[0][0] = X[0] == Y[0];
    for (int j = 1; j < Y.size(); j++) dp[0][j] = dp[0][j-1] || X[0] == Y[j];
    for (int j = 1; j < X.size(); j++) dp[j][0] = dp[j-1][0] || X[j] == Y[0];

    for (int j = 1; j < X.size(); j++) {
      for (int k = 1; k < Y.size(); k++) {
        if (X[j] == Y[k]) {
          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.size()-1][Y.size()-1] << endl;
  }
  return 0;
}