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;

static const int N=1000;
int lcs(string X,string Y){
	int c[N+1][N+1];
	int m=X.length();
	int n=Y.length();
	int maxl=0;
	X= " " +X;
	Y= " " +Y;
	for (int i = 0; i <= m; ++i) {c[i][0]=0;	}
	for (int j = 0; 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;
	cin>>s2;
	cout<<lcs(s1,s2)<<endl;
}
	return 0;
}

C++: #include<iostream>
using namespace std;
int solve(string x, string y){
	x=' '+x;
	y=' '+y;
	int n=x.length();
	int m=y.length();
	int dp[n+1][m+1];
	int k=0;
	for(int i=0; i<n+1; i++)
		dp[i][0]=0;
	for(int i=0; i<m+1; i++)
		dp[0][i]=0;
	for(int i=1; i<n+1; i++){
		for(int j=1; j<m+1; 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]);
			k=max(k, dp[i][j]);
		}
	}
	return k-1;
}
int main(){
	int n;
	cin>>n;
	while(n--){
		string x,y;
		cin>>x>>y;
		cout<<solve(x, y)<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
	#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
	#define dump(x)  ;
#endif

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

const int MOD = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    REP(i,q){
        string X,Y; cin >> X >> Y;
        int dp[1005][1005] = {};
        for(int i = 0; i < X.length(); i++){
            for(int j = 0; j < Y.length(); j++){
                if(X[i] == Y[j]){
                    dp[i+1][j+1] = dp[i][j] + 1;
                }
                else{
                    dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
                }
            }
        }
        cout << dp[X.length()][Y.length()] << endl;
    }

}

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 q;
  cin >> q;

  for ( int i = 0; i < q; i++ ) {
    cin >> s1 >> s2;
    cout << lcs(s1, s2) << endl;
  }

  return 0;
}

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

int lcs(std::string str1, std::string str2) {
    std::vector<std::vector<int> > table(str1.size(), std::vector<int>(str2.size(), 0));
    // Initialization
    for (int i=0; i<str1.size(); i++) {
        if ( str1[i] == str2[0] ) {
            table[i][0] = 1;
        } else {
            table[i][0] = 0;
        }
    }
    for (int j=0; j<str2.size(); j++) {
        if ( str2[j] == str1[0] ) {
            table[0][j] = 1;
        } else {
            table[0][j] = 0;
        }
    }
    // Calculate LCS
    for (int i=1; i<str1.size(); i++) {
        for (int j=1; j<str2.size(); j++) {
            table[i][j] = std::max( table[i-1][j], table[i][j-1] );
            if ( str1[i] == str2[j] ) {
                table[i][j]++;
            }
        }
    }
    return table[str1.size()-1][str2.size()-1];
}

int main() {
    int n;
    std::cin >> n;
    std::string str1, str2;
    std::vector<int> res(n);
    for (int i=0; i<n; i++) {
        std::cin >> str1 >> str2;
        res[i] = lcs(str1, str2); 
    }
    for (int i=0; i<n; i++) {
        std::cout << res[i] << std::endl;
    }
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int LCS[1010][1010];

void solve(string x, string y)
{
	for (int i = 0; i != x.size(); i++)
	{
		for (int j = 0; j != y.size(); j++)
		{
			if (x[i] == y[j])
			{
				LCS[i + 1][j + 1] = LCS[i][j] + 1;
			}
			else
			{
				LCS[i + 1][j + 1] = max(LCS[i][j + 1], LCS[i + 1][j]);
			}
		}
	}

	cout << LCS[x.size()][y.size()] << endl;
}

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

	for (int i = 0; i < 1000; i++)
	{
		LCS[0][i] = 0;
		LCS[i][0] = 0;
	}

	for (int i = 0; i < q; i++)
	{
		string x, y;
		cin >> x >> y;
		solve(x, y);
	}
	return 0;
}

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

int	lcs(const string &X, const string &Y)
{
	int	len_X = X.size(), len_Y = Y.size();
	int	len_lcs[1001][1001] = {};
	for (int i = 0; i < len_X; ++i) {
		for (int j = 0; j < len_Y; ++j) {
			int	prev_X = len_lcs[i][j + 1];
			int	prev_Y = len_lcs[i + 1][j];
			int	prev = max(prev_X, prev_Y);	
			if (X.at(i) == Y.at(j)) {
				len_lcs[i + 1][j + 1] = max(len_lcs[i][j] + 1, prev);
			} else {
				len_lcs[i + 1][j + 1] = prev;
			}
		}
	}
	return len_lcs[len_X][len_Y];
}

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