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 "iostream"
#include "string.h"

using namespace std;

int L[1001][1001];

int lcs( char s1[], char s2[], int l1, int l2 ) {

	if (L[l1][l2] != 0)return L[l1][l2];

	if (l1 == 0 || l2 == 0) return 0;
	else if (s1[l1-1] == s2[l2-1]) L[l1][l2] = lcs(s1, s2, l1 - 1, l2 - 1) + 1;
	else {
		int lsc1 = lcs(s1, s2, l1 - 1, l2);
		int lsc2 = lcs(s1, s2, l1, l2 - 1);

		if (lsc1 > lsc2) L[l1][l2] = lsc1;
		else L[l1][l2] = lsc2;
	}

	return L[l1][l2];
}

int main() {

	int n;
	char s1[1000], s2[1000];

	cin >> n;

	int ans[150];

	for (int i = 0; i < n; i++) {
		cin >> s1;
		cin >> s2;

		int l1 = strlen(s1), l2 = strlen(s2);

		ans[i] = lcs(s1, s2, l1, l2);

		for (int j = 0; j <= l1; j++) {
			for (int k = 0; k <= l2; k++) {
				L[j][k] = 0;
			}
		}
	}

	for (int i = 0; i < n; i++) {
		cout << ans[i] << endl;
	}

	return 0;
}

C++: #include <iostream>

using namespace std;

int lcs(string X, string Y) {
  int memo[1010][1010];
  int maxl = 0;

  // 初期化、境界条件を考慮して場合分け
  for (int i = 0; i < X.size(); i++) {
    if (X[i] == Y[0]) {
      memo[i][0] = 1;
    } else {
      memo[i][0] = 0;
    }
  }

  for (int j = 0; j < Y.size(); j++) {
    if (X[0] == Y[j]) {
      memo[0][j] = 1;
    } else {
      memo[0][j] = 0;
    }
  }

  for (int i = 0; i < X.size(); i++) {
    for (int j = 0; j < Y.size(); j++) {
      if (X[i] == Y[j]) {
        memo[i][j] = memo[i-1][j-1] + 1;
      } else {
        memo[i][j] = max(memo[i-1][j], memo[i][j-1]);
      }
      maxl = max(maxl, memo[i][j]);
      //cout << i << "," << j << "," << X[i] << "," << Y[j] << "," << maxl << endl;
    }
  }
  return maxl;
}

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

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

// ???????????§???????????§?????§??????
int DP[1024][1024] = {};

int main(){
    int N;
    cin >> N;
    for(int c=0; c<N; ++c){
        string X,Y;
        cin >> X >> Y;
        int xlen = (int)X.size();
        int ylen = (int)Y.size();

        for(int i=0;i<=xlen;++i){
          DP[i][0] = 0;
        }
        for(int i=0;i<=ylen;++i){
          DP[0][i] = 0;
        }

        for(int i=1;i<=xlen;++i){
            for(int j=1;j<=ylen;++j){
                if(X[i] == Y[j]){ // 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]);
                }
            }
        }
        cout << DP[xlen][ylen] << endl;
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  for(int I=0;I<n;I++){
    string a=" ";
    string b=" ";
    string c;
    cin >> c;
    a+=c;
    cin >> c;
    b+=c;
  
    int dp[a.size()][b.size()];
    for(int i=0;i<a.size();i++){
      for(int j=0;j<b.size();j++){
	if(i==0||j==0)dp[i][j]=0;
	else if(a[i]==b[j])dp[i][j]=dp[i-1][j-1]+1;
	else dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
      }
    }
    cout << dp[a.size()-1][b.size()-1] << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int dp[1005][1005];

int main()
{
	ios_base::sync_with_stdio(0);
	int N; cin >> N;
	while (N-- ){
		memset (dp, 0, sizeof (dp ) );
		string s = "", t = "";
		cin >> s >> t;
		int n = s.length();
		int m = t.length();
		
		dp[0][0] = (s[0] == t[0] );

		for (int i = 1; i < n; i++ )
			if (s[i] == t[0] )
				dp[i][0] = 1;
			else
				dp[i][0] = max (dp[i][0], dp[i-1][0] );

		for (int j = 1; j < m; j++ )
			if (s[0] == t[j] )
				dp[0][j] = 1;
			else
				dp[0][j] = max (dp[0][j], dp[0][j-1] );

		for (int i = 1; i < n; i++ ){
			for (int j = 1; j < m; j++ ){
				dp[i][j] = max (dp[i][j], dp[i-1][j-1] + (s[i] == t[j] ) );
				dp[i][j] = max (dp[i][j], dp[i-1][j] );
				dp[i][j] = max (dp[i][j], dp[i][j-1] );
			} // end for
		} // end for
#if (DEBUG )
		rep (i, n ){
			rep (j, m ){
				printf ("%3d", dp[i][j] );
			} // end rep
			printf ("\n" );
		} // end rep
#endif		
		int res = 0;
		rep (i, n ) rep (j, m ) res = max (res, dp[i][j] ); 		
		cout << res << endl;
	} // end loop
		
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
const int mod = 1000000007;
const int max_n = 200005;
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main(){
    ll n;cin >> n;
    rep(i,n){
        string s,t;cin >>s >>t;
        vvl dp(s.size()+1,vl(t.size()+1,0));
        rep(j,s.size()){
            rep(k,t.size()){
                if(s[j]==t[k]){
                    dp[j+1][k+1]=dp[j][k]+1;
                }
                else{
                    dp[j+1][k+1]=max(dp[j][k+1],dp[j+1][k]);
                }
            }
        }
        cout << dp[s.size()][t.size()] <<endl;
    }
}