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>
#include <math.h>

using namespace std;

long long inf=1000000007;

long long modpow(long long n,long long p,long long mod);

int main(){
	long long n;
	cin>>n;
	for(long long i=0;i<n;i++){
		string s,t;
		cin>>s>>t;
		long long ss=s.size(),ts=t.size();
		vector<vector<long long>> dp(ss+1,vector<long long>(ts+1,-1));
		dp.at(0).at(0)=0;

		for(long long j=0;j<=ss;j++){
			for(long long k=0;k<=ts;k++){
				if(j==0 || k==0){
					dp.at(j).at(k)=0;
					continue;
				}

				if(s.at(j-1)==t.at(k-1)){
					dp.at(j).at(k)=dp.at(j-1).at(k-1)+1;
				}else{
					dp.at(j).at(k)=max(dp.at(j-1).at(k),dp.at(j).at(k-1));
				}
			}
		}

		cout<<dp.at(ss).at(ts)<<endl;
	}

	return 0;
}

long long modpow(long long n,long long p,long long mod){
	if(p==0) return 1;
	if(p%2==0) return (modpow(n,p/2,mod)*modpow(n,p/2,mod))%mod;
	if(p%2==1) return (modpow(n,p-1,mod)*n)%mod;
	cerr<<"ERROR"<<endl;
	return 1;
}

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

using namespace std;

//L[i][j] X[0]..X[i]??¨X[0]..Y[j]???LCS
int L[1001][1001]={};
int main(){
  int testnum;
  cin>>testnum;
  for(int t=0;t<testnum;++t){
    string X,Y;
    cin>>X>>Y;

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

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

  return 0;
}

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

using namespace std;

#define rep(i,x) for(int i=0;i<x;++i)
#define rrep(i,x) for(int i=x-1;i>=0;--i)

int dp[1010][1010];

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

    rep(_, q) {
        memset(dp, -1, sizeof(dp));
        dp[0][0] = 0;

        string a,b; cin >> a>>b;

        int n = a.size(), m = b.size();
        rep(i, n+1) {
            rep(j, m+1) {
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
                dp[i][j + 1] = max(dp[i][j + 1], dp[i][j]);

                if (a[i] == b[j]) {
                    dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
                }
            }
        }

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

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

void fnInput(vector<string>& rvsStrX, vector<string>& rvsStrY)
{
  int nMaxSiz;
  cin >> nMaxSiz;
  rvsStrX.resize(nMaxSiz);
  rvsStrY.resize(nMaxSiz);

  for (int i = 0; i < nMaxSiz; ++i)
  {
    cin >> rvsStrX[i] >> rvsStrY[i];
    rvsStrX[i] = ' ' + rvsStrX[i];
    rvsStrY[i] = ' ' + rvsStrY[i];
  }
}

int fnLngCmnSubseq(const vector<string>& cnrvsStrX, const vector<string>& cnrvsStrY)
{
  for (int i = 0; i < cnrvsStrX.size(); ++i)
  {
    const int nMAXNX = cnrvsStrX[i].size();
    const int nMAXNY = cnrvsStrY[i].size();
    vector<vector<int>> vvnLcsDP(nMAXNX, vector<int>(nMAXNY, 0));

    for (int nx = 1; nx < nMAXNX; ++nx)
      for (int ny = 1; ny < nMAXNY; ++ny)
        if (cnrvsStrX[i][nx] == cnrvsStrY[i][ny])
          vvnLcsDP[nx][ny] = vvnLcsDP[nx - 1][ny - 1] + 1;
        else
          vvnLcsDP[nx][ny] = max(vvnLcsDP[nx - 1][ny], vvnLcsDP[nx][ny - 1]);

    cout << vvnLcsDP[nMAXNX - 1][nMAXNY - 1] << endl;
  }
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<string> vsStrX, vsStrY;

  fnInput(vsStrX, vsStrY);
  fnLngCmnSubseq(vsStrX, vsStrY);
  return 0;
}

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

string X, Y;
int v[1001][1001];

int  dp(string X, string Y) {
	int n = X.size();
	int m = Y.size();

	for (int i = 0; i <= n; i++) {
		v[i][0] = 0;											///０のテーブル
		for (int j = 0; j <= m; j++) {
			v[0][j] = 0;
		}
	}

	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			if (X[i-1] == Y[j-1]) {
				v[i][j] = v[i - 1][j - 1] + 1;
			}
			else {
				v[i][j] = max(v[i - 1][j], v[i][j - 1]);
			}
		}
	}
	return v[n][m];
}

int main(void)
{
	int x;
	cin >> x;
	
	for (int i = 0; i < x; i++) {
		cin >> X >> Y;
		int ans = dp(X, Y);
		cout << ans << endl;
	}
	
	
}

C++: #include <iostream>
#include <string.h>
#include <algorithm>
#define MAX 1000

using namespace std;

string s,t;
int dp[MAX + 1][MAX + 1];

int LCS(void){

  for( int i = 0 ; i < s.size() ; i++ ){
    for( int j = 0 ; j < t.size() ; 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]);
      }
    }
  }
  return dp[s.size()][t.size()];
}


int main(void){

  int n;
  cin >> n;
  while(n--){

    memset(dp, 0 , sizeof(dp) );

    cin >> s >> t;
    cout << LCS() << endl;

  }
  return 0;
}

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

int lcs( string X , string Y) {
int c[ N + 1 ][ N + 1 ] ; 
int m = X.size() ; 
int n = Y.size() ; 
int max1 = 0 ;
X = ' ' + X[0] ;
Y = ' ' + Y[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 <iostream>
#include <vector>
#include <string>

using namespace std;

// 多次元配列の初期化方法確認
// 出力確認
//
void traceback (string x, string y, vector<vector<int>> L) {
	string lcs;
	int i = x.length();
	int j = y.length();
	while(i != 0 && j != 0) {
		if (L[i][j] == L[i-1][j-1] + 1) {
			lcs = x[i-1] + lcs;
			i--;
			j--;
		} else if (L[i][j-1] >= L[i-1][j]) {
			j--;
		} else if (L[i][j-1] <= L[i-1][j]) {
			i--;
		}
	}
	
	cout << lcs << endl;
}

int main(void) {
	int q;
	cin >> q;
	string x, y;
	
	while(q--) {
		cin >> x >> y;

	//	vector<vector<int>> L(max(x.length(), y.length()), 0);
		vector<vector<int>> L(x.length() + 1, vector<int>(y.length() + 1, 0));
		for (int i = 0; i <= x.length(); i++) {
			for (int j = 0; j <= y.length(); j++) {
				if (i == 0 || j == 0) L[i][j] = 0;
				else if (x[i-1] == y[j-1]) {
					L[i][j] = L[i-1][j-1] + 1;
				} else if (x[i-1] != y[j-1]) {
					L[i][j] = max(L[i-1][j], L[i][j-1]);
				}
			}
		}
		//出力がよくない状況
		cout << L[x.length()][y.length()] << endl;

		//traceback(x, y, L);
	}

}
// xi = yj
// Lij = Li-1j-1 + 1
// Lij = max(Li-1j, Lij-1)

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



int lcs(string X,string Y){
	int dp[1001][1001];
  	int maxl=0; 
  	int m=X.size();int n=Y.size();
  	rep(i,m+1){dp[i][0]=0;}
  	rep(i,n+1){dp[0][i]=0;}
  	X=' '+X;Y=' '+Y;
  	for(int i=1;i<=m;i++){
    	for(int j=1;j<=n;j++){
        	if(X[i]==Y[j]){
            	dp[i][j]=dp[i-1][j-1]+1;            	
            }
        	else {
            dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            }   
          maxl=max(maxl,dp[i][j]);
          
        }
    } 
  return maxl;
}

int main() {
	int q;
  	cin >>q;
  	rep(k,q){
    	string x,y;
    	cin >>x>>y;
      	cout <<lcs(x,y)<<endl;
      	
    }  
}