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 <algorithm>
#include <bitset>
#include <cmath>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define Rep(i, b, e) for(int (i) = (b); (i) < (e); ++(i))
#define Repr(i, b, e) for(int (i) = (b) - 1; (i) >= (e); --(i))
#define Print(x) cout << (x) << endl 
#define Min2(a, b) ((a) < (b)) ? (a) : (b)
#define Max2(a, b) ((a) > (b)) ? (a) : (b)

typedef long long ll;

using namespace std;

string X, Y;
int dp[1001][1001];

int max3(int a, int b, int c){
    return Max2(a, (Max2(b, c)));
}

int lcs(){
    int lenX = (int)X.length();
    int lenY = (int)Y.length();
    Rep(i, 0, lenX + 1) Rep(j, 0, lenY + 1) dp[i][j] = 0;
    Rep(i, 0, lenX){
        Rep(j, 0, lenY){
            dp[i + 1][j + 1] = max3(dp[i][j] + (X[i] == Y[j]), dp[i][j + 1], dp[i + 1][j]);
        }
    }
    return dp[lenX][lenY];
}

int main(){
    int q;
    cin >> q;
    Rep(i, 0, q){
        cin >> X >> Y;
        cout << lcs() << endl;
    }
    return 0;
}

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

using namespace std;
const int dx[]={1,0,-1,0,1,-1,-1,1,0};
const int dy[]={0,1,0,-1,1,1,-1,-1,0};
const int INF = 1e9+101;
const long long LINF = 1e18;
const double EPS = 1e-8;
#define pb push_back
#define mk make_pair
#define fr first
#define sc second
#define reps(i,j,k) for(ll i = (j); i < (k); ++i)
#define rep(i,j) reps(i,0,j)
#define all(a) (a).begin(),(a).end()
#define MOD 1000000007
typedef long long ll;
typedef pair<ll,ll> Pii;
typedef pair<Pii,int> P;
typedef vector<int> vi;
typedef vector<ll> vll;
template<class T>
ostream& operator<<(ostream &out, const vector<T> &v){
	out << "{";
	rep(i,v.size()){
		out << v[i] <<", ";
	}
	return out << "}" << endl;
}

int main() {
    int N;
    cin >> N;
    while(N--) {
        int dp[1024][1024] = {{}};
        string a,b;
        cin >> a >> b;
        int ans = 0;
        reps(i,1,a.size()+1){
            reps(j,1,b.size()+1) {
                dp[i][j] = max(dp[i-1][j],max(dp[i][j-1], dp[i-1][j-1]+(a[i-1]==b[j-1])));
                ans = max(ans, dp[i][j]);
            }
        }
        cout << dp[a.size()][b.size()] << endl;
    }
    return 0;  
}

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

int main(){
    int q;
    cin >> q;
    while(q-- > 0){
        string s, t;
        cin >> s >> t;
        s = " " + s; // padding
        t = " " + t; // padding
        int h = s.length(), w = t.length();
        vector<vector<int>> dp(h, vector<int>(w,0));
        for(int i = 1; i < h; i++){
            for(int j = 1; j < w; j++){
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
                if(s[i] == t[j])    dp[i][j] = max(dp[i][j], dp[i-1][j-1]+1);
            }
        }
        cout << dp[h-1][w-1] << endl;
    }
    return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std;
#define MAX 1000

int main(){
  int n;
  cin>>n;
  string a,b;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    int C[MAX+1][MAX+1]={0};
    for(int i=1;i<=a.size();i++){
      for(int j=1;j<=b.size();j++){
        if(a[i]==b[j]) C[i][j]=C[i-1][j-1]+1;
        else{
          C[i][j]=max(C[i-1][j],C[i][j-1]);
        }
      }
    }
    cout<<C[a.size()][b.size()]<<endl;
  }
  
}

C++: //最长公共子序列 
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int LCS(string s1,string s2)
{
	int c[1005][1005];
	int m1=s1.length();
		int m2=s2.length();
		int maxx=0;
		s1=' '+s1;
		s2=' '+s2;
		for(int i=1;i<=m1;i++)
			c[i][0]=0;
		for(int j=1;j<=m2;j++)
			c[0][j]=0;
		int i,j;
		for(i=1;i<=m1;i++)
		{
			for(j=1;j<=m2;j++)
			{
				if(s1[i]==s2[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];
				}
				maxx=max(maxx,c[i][j]);
			}
			
		}
		return maxx;	
	
}
int main()
{
	int n;
	cin>>n;
	
	while(n--)
	{
		string s1,s2;	
		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 <= 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<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 <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<string> S(2*n);
    for (auto i = 0; i < 2*n; i++)
    {
        string s;
        cin >> s;
        S[i] = s;
    }

    for (auto i = 0; i < 2*n; i += 2)
    {
        string x = S[i];
        string y = S[i + 1];
        int sizex = x.size();
        int sizey = y.size();

        vector<vector<int>> m(sizex+1, vector<int>(sizey+1));

        for (auto j = 0; j < sizex+1; j++)
        {                
                m[j][0] = 0;   
        }

        for (auto j = 0; j < sizey+1; j++)
        {
                m[0][j] = 0;
        }


        for (auto j = 1; j < sizex+1; j++)
        {
            for (auto k = 1; k < sizey+1; k++)
            {
                if(x[j-1] == y[k-1]){
                    m[j][k] = m[j - 1][k - 1] + 1;
                }else{
                    m[j][k] = max(m[j - 1][k], m[j][k - 1]);
                }
            }
        }

        cout << m[sizex][sizey] << endl;

    }   
    
}