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++: // C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>

#define ll long long
#define Int ll
#define loop(__x, __start, __end) for(Int __x = __start; __x < __end; __x++)
#define loopdown(__x, __start, __end) for(Int __x = __start; __x > __end; __x--)
#define rep(__n) for(Int __x = 0; __x < __n; __x++)
#define span(__a,__x,__y) __a.begin()+__x,__a.begin()+__y
#define span_all(__a) __a.begin(),__a.end()
#define len(__x) (__x.size())
#define last(__x) (__x[__x.size()-1])

using namespace std;

struct Point {
  Int x, y;
  Point(Int x, Int y): x(x), y(y) {}
  Point(): x(0), y(0) {}

  Point operator + (Point v) { return Point(x + v.x, y + v.y); }
  Point operator - (Point v) { return Point(x - v.x, y - v.y); }
  bool operator == (const Point &v) const {
    return x == v.x && y == v.y;
  }
  bool operator< (const Point &p1) const {
    return x < p1.x && y < p1.y;
  }
};
ostream & operator << (ostream & out, Point const & v) {
  out << "Point(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Point & v) {
  Int x, y;
  in >> x; in >> y; v.x = x; v.y = y;
  return in;
}

// Geometry

#define EPS 0.0000000001

class Vector2 {
public:
  double x, y;
  
  Vector2(double x = 0, double y = 0): x(x), y(y) {}
  
  Vector2 operator + (Vector2 v) { return Vector2(x + v.x, y + v.y); }
  Vector2 operator - (Vector2 v) { return Vector2(x - v.x, y - v.y); }
  Vector2 operator * (double k) { return Vector2(x * k, y * k); }
  Vector2 operator / (double k) { return Vector2(x / k, y / k); }
  
  double length() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  double dot (Vector2 v) { return x * v.x + y * v.y; }
  double cross (Vector2 v) { return x * v.y - y * v.x; }
  
  bool operator < (const Vector2 &v) const {
    return x != v.x ? x < v.x : y < v.y;
  }
  
  bool operator == (const Vector2 &v) const {
    return fabs(x - v.x) < EPS && fabs(y - v.y) < EPS;
  }
};

ostream & operator << (ostream & out, Vector2 const & v) { 
  out<< "Vector2(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Vector2 & v) { 
  double x, y;
  in >> x;
  in >> y;
  v.x = x;
  v.y = y;
  return in;
}

Int N;
string A, B;
void solve(string& a, string& b);

void input() {
  cin >> N;
  rep(N) {
    cin >> A >> B;
    solve(A, B);
  }
}

vector<vector<Int> > dp(1001, vector<Int>(1001, -1));

Int solveRec(Int x = 0, Int y = 0) {
  if (x == A.size() || y == B.size()) return 0; // どちらかが終了したらもう共通はありえない
  Int &cache = dp.at(x).at(y);
  if (~cache) return cache;
  Int count = max(
    solveRec(x, y+1),
    solveRec(x+1, y)
  );
  if (A.at(x) == B.at(y)) {
    count = max(count, 1 + solveRec(x+1, y+1));
  }
  cache = count;
  return count;
}

void initDP(Int x_, Int y_) {
  loop(x,0,x_) {
    loop(y,0,y_) {
      dp.at(x).at(y) = -1;
    }
  }
}

void solve(string& a, string& b) {
  initDP(a.size(), b.size());
  cout<< solveRec() << endl;
}

int main() {
  input();
  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++)	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]);
			}
			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<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=0;i<=m;i++) c[i][0]=0;
    for(int j=0;j<=m;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]);
            }
            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<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<list>
#include<string>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;

const int NIL = -1;
const int MAX = 2000001;
int c[1001][1001];

int main(void)
{
	int q, m, n;
	string a, b;
	cin >> q;
	for (int i = 0; i < q; i++) {
		cin >> a >> b;
		m = a.length();
		n = b.length();
		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 (a[i - 1] == b[j - 1])
					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];
			}
		}
		cout << c[m][n] << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#ifdef ENABLE_DEBUG_OUTPUT
#define DEBUG_LOG(s) cout << s << endl;
#else
#define DEBUG_LOG(s) void();
#endif

int lcs(string a, string b) {
    vector<vector<int>> dp(a.size(), vector<int>(b.size(), 0));
    //init
    dp[0][0] = (a[0] == b[0]);
    for (auto i = 1; i < (int)a.size(); i++) dp[i][0] = dp[i - 1][0] or (a[i] == b[0]);
    for (auto i = 1; i < (int)b.size(); i++) dp[0][i] = dp[0][i - 1] or (a[0] == b[i]);
    // dp
    for (auto i = 1; i < (int)a.size(); i++) {
        for (auto j = 1; j < (int)b.size(); j++) {
            if(a[i] == b[j]) dp[i][j] = dp[i - 1][j - 1] + 1;
            else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
    }

//    for (auto i = 0; i < (int)b.size(); i++) {
//        for (auto j = 0; j < (int)a.size(); j++) {
//            cout << dp[j][i] << " ";
//        }
//        cout << endl;
//    }
    return dp[a.size() - 1][b.size() - 1];
}

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

    for (auto i = 0; i < q; i++) {
        string x, y; cin >> x >> y;
        cout << lcs(x, y) << endl;
    }

    return 0;
}

C++: #include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
int main(){
  int n,dp[1010][1010],i,j,k,a,b;
  scanf("%d\n",&n);
  for(k=0;k<n;k++){
    string x,y;
    cin >> x >> y;
    a=x.size(),b=y.size();
    for(i=0;i<=a;i++){
      for(j=0;j<=b;j++){
        dp[i][j]=0;
      }
    }
    for(i=1;i<=a;i++){
      for(j=1;j<=b;j++){
        if(x[i-1]==y[j-1]){
          dp[i][j]=dp[i-1][j-1]+1;
        }
        else{
          if(dp[i-1][j]>dp[i][j-1]){
            dp[i][j]=dp[i-1][j];
          }
          else{
            dp[i][j]=dp[i][j-1];
          }
        }
      }
    }
      printf("%d\n",dp[a][b]);
    }
  return 0;
}