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>
#include <vector>

using namespace std;

void
chmax(int& aa, int bb)
{
  if (aa < bb) {
    aa = bb;
  }
}

int
main()
{
  int   qq;

  cin >> qq;

  
  for (int qi = 0; qi < qq; qi++) {
    string      xx;
    string      yy;
    vector<vector<int> > dp;

    cin >> xx;
    cin >> yy;

//    cout << xx << endl;
//    cout << xx.length() << endl;

    dp.assign(xx.length() + 1, vector<int>(yy.length() + 1, 0));

    for (int xi = 0; xi < xx.length(); ++xi) {
      for (int yi = 0; yi < yy.length(); ++yi) {
        //cout << yi << endl;
        if (xx[xi] == yy[yi]) {
          dp[xi + 1][yi + 1] = dp[xi][yi] + 1;
        }
        chmax(dp[xi + 1][yi + 1], dp[xi][yi + 1]);
        chmax(dp[xi + 1][yi + 1], dp[xi + 1][yi]);
      }
    }
    cout << dp.back().back() << endl;
  }
}

C++: #include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
#include<functional>
#include<stack>
#include<tuple>
#include<cassert>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using P = pair<int, int>;

int n;
int dp[1001][1001];

int main() {
  cin >> n;
  rep(i, n) {
    string x, y;
    cin >> x >> y;
    dp[0][0] = 0;
    rep(i, y.size()) {
      dp[0][i+1] = 0;
    }
    rep(i, y.size()) {
      dp[i+1][0] = 0;
    }
    rep(i, x.size()) {
      rep(j, y.size()) {
        dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
        if(x[i] == y[j]) {
          dp[i+1][j+1] = dp[i][j] + 1;
        }
      }
    }
    cout << dp[x.size()][y.size()] << endl;
  }


  return 0;
}

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

using namespace std;

int lcs(string x,string y){
    int m = x.size();
    int n = y.size();
    int lcs_lst[m+1][n+1];
    x = ' '+x;
    y = ' '+y;
    for(int i=0;i<=m;i++) lcs_lst[i][0] = 0;
    for(int i=1;i<=n;i++) lcs_lst[0][i] = 0;

    int ans = 0;
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            if(x[i] == y[j])
                lcs_lst[i][j] = lcs_lst[i-1][j-1]+1;
            else{
                lcs_lst[i][j] = max(lcs_lst[i][j-1],lcs_lst[i-1][j]);
            }
            ans = max(ans,lcs_lst[i][j]);

        }
    }

    return ans;
}

int main(){
    int n; cin >> n;
    string a,b;
    for(int i=0;i<n;i++){
        cin >> a >> b;
        cout << lcs(a,b) << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
using ll=long long;
using vi=vector<int>;
using vl=vector<long long>;
using pii=pair<int,int>;
using pll=pair<long long,long long>;
#define ITR(i,c) for(auto i=begin(c);i!=end(c);++i)
#define FORE(x,c) for(auto &x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define REPW(i,n) for(i=0;i<(int)(n);++i)
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define SZ(c) ((int)c.size())
#define CONTAIN(c,x) (c.find(x)!=end(c))
#define OUTOFRANGE(y,x,h,w) (y<0||x<0||y>=h||x>=w)
#define dump(...)
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
#define INF (1001001001)
#define INFLL (1001001001001001001ll)
template<class T> ostream& operator << (ostream &os,const vector<T> &v) {
    ITR(i,v) os << *i << (i==end(v)-1 ? "" : "\n"); return os; }
template<class T> istream& operator >> (istream &is,vector<T> &v) {
    ITR(i,v) is >> *i; return is; }
template<class T> istream& operator >> (istream &is, pair<T,T> &p) {
        is >> p.first >> p.second; return is; }
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
//------------------------------------------------------------------------------
struct before_main_function {
    before_main_function() {
        #ifdef int
            #undef INF
            #define INF INFLL
            #define stoi stoll
        #endif
        cin.tie(0);ios::sync_with_stdio(false);
        cout<<setprecision(15)<<fixed;
        #define endl "\n"
    }
} before_main_function;
//------------------8<------------------------------------8<--------------------

signed main() {
    int q;
    cin>>q;
    while(q--) {
        string x,y;
        cin>>x>>y;
        int n=SZ(x),m=SZ(y);
        vector<vector<int>> dp(n+1,vector<int>(m+1,0));
        REP(i,n) {
            REP(j,m) {
                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[n][m]<<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 = 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 x, y;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> x >> y;

		cout << lcs(x, y) << endl;
	}
	return 0;
	
}

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

#define LOOP(val,times) for(val = 0;val < (times);++val)


int lcs(string X, string Y){
    int dp[1010][1010];
    int i,j;
    LOOP(i,1010){
        dp[0][i] = 0;
        dp[i][0] = 0;
    }
    LOOP(i,Y.size()){
        LOOP(j,X.size()){
            if(X[j] == Y[i]){
                dp[i+1][j+1] = dp[i][j] + 1;
            }
            else{
                dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
            }
        }
    }
    return dp[Y.size()][X.size()];    
}

int main(){
    int i,q;
    cin >> q;
    LOOP(i,q){
        string X,Y;
        cin >> X >> Y;
        cout << lcs(X,Y) << endl;
    }
    int k;cin >> k;
}

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

using namespace std;

int main() {
  int q;
  cin >> q;
  string s, t;
  int ls, lt;
  for (int p=0; p<q; ++p){
    cin >> s >> t;
    ls = s.length();
    lt = t.length();
    int dp[ls+1][lt+1];
    for(int i=0; i<=ls; ++i){
      for(int j=0; j<=lt; ++j){
        if(i == 0 || j == 0){
          dp[i][j] = 0;
        }
        else {
          dp[i][j] = (s[i-1] == t[j-1]) ? max(max(dp[i-1][j-1] + 1, dp[i-1][j]), dp[i][j-1]) : max(dp[i-1][j], dp[i][j-1]);
        }
      }
    }
    cout << dp[ls][lt] << endl;
  }
}