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;

int lcs(string str1, string str2)
{
  int m = str1.size(), n = str2.size();
  int c[1001][1001];
  str1 = ' ' + str1;
  str2 = ' ' + str2;
  for (int i = 1; i <= m; i++) c[i][0] = 0;
  for (int i = 1; i <= n; i++) c[0][i] = 0;

  for (int i = 1; i <= m; i++){
    for (int j = 1; j <= n; j++){
      if (str1[i] == str2[j]) c[i][j] = c[i - 1][j - 1] + 1;
      else if (c[i - 1][j] > c[i][j - 1]) c[i][j] = c[i - 1][j];
      else  c[i][j] = c[i][j - 1];
    }
  }
  return (c[m][n]);
}
int main()
{
  int q;
  cin >> q;
  while (q--){
    string str1, str2;
    cin >> str1 >> str2;
    cout << lcs(str1, str2) << endl;
  }
}

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

int max(int a,int b);

int main() {
  int q;
  cin >> q;
  for (int i=0;i<q;++i) {
    string str1,str2;
    cin >> str1 >> str2;
    int size1 = str1.size();
    int size2 = str2.size();
    int matrix[size1+1][size2+1];
    for (int j=0;j<=size1;++j)
      matrix[j][0] = 0;
    for (int j=0;j<=size2;++j)
      matrix[0][j] = 0;
    for (int j=0;j<=size1;++j) {
      for (int k=0;k<=size2;++k) {
	if (j<=0||k<=0) {
	  matrix[j][k] = 0;
	}
	else if (str1[j]==str2[k]) {
	  matrix[j][k] = 1 + matrix[j-1][k-1];
	}
	else {
	  matrix[j][k] = max(matrix[j][k-1],matrix[j-1][k]);
	}
      }
    }
    cout << matrix[size1][size2] << endl;
  }
}

int max(int a,int b) {
  if (a>b)
    return a;
  else
    return b;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
usig namespace std;
static const int N=1000;
int lcs(string X,string Y){
  int c[N+1][N+1];
  int m=X.size();
  int n=Y.size();
  int maxl=0;
  X=' '+X;
  Y=' '+Y;
  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]);
        }  
      maxl=max(maxl,c[i][j]);
     }
   }
  return maxl;
}

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<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(n,a) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265


void f(){
  string x,y;
  cin>>x>>y;
  int dp[1100][1100]={};
  
  rep(i,x.length()+1){
    rep(j,y.length()+1){
      if(i==0||j==0) continue;
      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[(int)x.length()][(int)y.length()]<<endl;
}


int main(){
  int q;
  cin>>q;
  while(q--){
    f();
  }
}

C++: #include <iostream>
#include <string>
using namespace std;
static const int MAX = 1001;

void lcs(string X, string Y) {
	int dp[MAX][MAX];  
	int i,j;

	int xLength = X.size();
	int yLength = Y.size();


	// 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>
#include<algorithm>
using namespace std;
static const int N = 1000;
int lcs(string X, string Y) {
  int c[N + 1][N + 1];
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  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] = mx(c[i - 1][j], c[i][j = 1]);
      }
      maxl = max(mxl, c[i][j]);
    }
  }
  return maxl;
}
int main(){
  string s1, s2;
  int n; cin > n;
  for ( int i = 0; i < n; i++ ){
    cin >> s1 >>s2;
    cout << lcs8s1, s2) << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
#include <array>
#include <complex>
#include <utility>
#include <map>
#include <inttypes.h>

int LCS(std::string s,std::string t){
    int n,m,c[1001][1001]={0};
    n=s.size();
    m=t.size();
    int maxlen=0;
    for(int i=1;i<=n;i++){
        c[i][0]=0; 
    }
    for(int i=1;i<=m;i++){
        c[0][i]=0;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(s[i-1]==t[j-1]){
                c[i][j]=c[i-1][j-1]+1;
            }else{
                c[i][j]=std::max(c[i-1][j],c[i][j-1]);
            }
            maxlen=std::max(maxlen,c[i][j]);
        }
    }
    return maxlen;
}
int main(){
	int N;
    
    std::cin >> N;
    std::string s1,s2;
    for(int i=0;i<N;i++){
        std::cin >> s1 >> s2;
        std::cout << LCS(s1,s2) <<std::endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>

using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define INT(a) int((a)+1e-9)
#define UNDEFCHAN -1
#define SUPL 1002
#define MISERU(name,value) cout<<name<<": "<<value<<endl;

int xl,yl;
char xs[SUPL],ys[SUPL];
int rss[SUPL][SUPL];

int r(int xi, int yi){
	if (rss[xi][yi]!=UNDEFCHAN) return rss[xi][yi];
	int re;
	if(xi==0) re=0;
	else if(yi==0) re=0;
	else if(xs[xi-1]==ys[yi-1]) re=r(xi-1,yi-1)+1;
	else re=max(r(xi-1,yi),r(xi,yi-1));
	rss[xi][yi]=re;
	return re;
}

int solve(){
	REP(xi,xl+1) REP(yi,yl+1) rss[xi][yi]=UNDEFCHAN;
	return r(xl,yl);
}

int main(){
	int ol;
	cin>>ol;
	REP(oi,ol){
		cin>>xs;
		cin>>ys;
		xl=strlen(xs);
		yl=strlen(ys);
		cout<<solve()<<endl;
	}
	return 0;
}