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 <vector>
using namespace  std;

void fnInput(vector<string>& rvsStrX, vector<string>& rvsStrY)
{
  int nMaxSiz;
  cin >> nMaxSiz;
  rvsStrX.resize(nMaxSiz);
  rvsStrY.resize(nMaxSiz);

  for (int i = 0; i < nMaxSiz; ++i)
  {
    cin >> rvsStrX[i] >> rvsStrY[i];
    rvsStrX[i] = ' ' + rvsStrX[i];
    rvsStrY[i] = ' ' + rvsStrY[i];
  }
}

int fnLngCmnSubseq(const vector<string>& cnrvsStrX, const vector<string>& cnrvsStrY)
{
  vector<vector<int>> vvnLcsDP;
  for (int i = 0; i < cnrvsStrX.size(); ++i)
  {
    const int nMAXNX = cnrvsStrX[i].size();
    const int nMAXNY = cnrvsStrY[i].size();

    vvnLcsDP.clear();
    vvnLcsDP.resize(nMAXNX, vector<int>(nMAXNY, 0));

    for (int nx = 1; nx < nMAXNX; ++nx)
      for (int ny = 1; ny < nMAXNY; ++ny)
        if (cnrvsStrX[i][nx] == cnrvsStrY[i][ny])
          vvnLcsDP[nx][ny] = vvnLcsDP[nx - 1][ny - 1] + 1;
        else
          vvnLcsDP[nx][ny] = max(vvnLcsDP[nx - 1][ny], vvnLcsDP[nx][ny - 1]);

    cout << vvnLcsDP[nMAXNX - 1][nMAXNY - 1] << endl;
  }
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<string> vsStrX, vsStrY;

  fnInput(vsStrX, vsStrY);
  fnLngCmnSubseq(vsStrX, vsStrY);
  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
using namespace std;

int main(){
    int q;
    cin>>q;
    string s1,s2;
    
    rep(i,q){
        cin>>s1>>s2;
        vector< vector< int > > c(1010, vector< int >(1010, 0));
        c[0][0]=0;
        rep(j,s1.length()+1)c[j][0]=0;
        rep(k,s2.length()+1)c[0][k]=0;
        for(int n=1;n<=s1.length();n++){
            for(int m=1;m<=s2.length();m++){
                if(s1[n-1]==s2[m-1])c[n][m]=c[n-1][m-1]+1;
                else {
                    c[n][m]=max(c[n-1][m],c[n][m-1]);
                }
            }
        }
        cout<<c[s1.length()][s2.length()]<<endl;
    }
}

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

int main() {

  int q;
  cin >> q;
  
  while (0 < q) {
    
    string X, Y;
    cin >> X >> Y;

    int x = X.size();
    int y = Y.size();

    // X_0 ... X_i と Y_0 ... Y_j に対するLCSの長さ
    vector<vector<int>> dp(x+1, vector<int>(y+1));

    int answer = 0;

    for (int i = 0; i < x; i++) {
      for (int j = 0; j < y; j++) {
        if (X[i] == Y[j]) {
          // 同じ文字列の場合は、
          // X_0 ... X_i と Y_0 ... Y_j に対するLCSに1加える
          dp[i+1][j+1] = dp[i][j] + 1;
        } else {
          // 違う場合は、
          // X_0 ... X_i と Y_0 ... Y_j+1 に対するLCSと
          // X_0 ... X_i+1 と Y_0 ... Y_j に対するLCSで
          // 大きい方
          dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
        }
        answer = max(answer, dp[i+1][j+1]);
      }
    }

    cout << answer << endl;

    q--;
  }

  return 0;

}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstdio>
#include<queue>
#include<deque>
#include<map>
#include<stack>
#include<set>
#include<utility>
using namespace std;
typedef pair<int,int> ii;
typedef long long ll;
typedef pair<ll,ii> P;
typedef unsigned long long int ull;
const int MOD=1e9+7;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
const int MAXN=100000;
const int MAXE=100000;
const int MAXV=10000;
const ll INF=2e18;

int main(){
    int q;cin>>q;
    int dp[1001][1001];
    for(int k=0;k<q;k++){
        string s,t;cin>>s>>t;
        int n=(int)s.size();
        int m=(int)t.size();
        for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                dp[i][j]=0;
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
                if(s[i]==t[j]) dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+1);
            }
        }
        cout<<dp[n][m]<<endl;
    }
    return 0;
}

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

using namespace std;

int dp[1010][1010];



int solve() {

	string s,t;

	cin >> s >> t;


	int max_s = s.length();
	int max_t = t.length();

	memset(dp,0, sizeof(int) * 1010 * 1010);



	for (int i = 1; i <= max_s; i++) {
		for (int j = 1; j <= max_t; j++) {
			if (s.at(i - 1) == t.at(j -1)) {
				dp[i][j] = dp[i - 1][j - 1] + 1;
			} else {
				dp[i][j] = max(dp[i][j - 1],dp[i -1][j]);			
			}
		}
	}
	return dp[max_s][max_t];
}

int main() {
	int q;

	cin >> q;

	for (int i = 0; i  < q; i++) {
		cout << solve() << endl;
	}

}

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

using namespace std;
typedef long long int ll;

int dh[4] = {1,0,-1,0};
int dw[4] = {0,1,0,-1};

int main()
{
    int q;
    cin >> q;
    for(int i = 0; i < q; i++)
    {
        string X,Y;
        cin >> X >> Y;
        int lx = X.length();
        int ly = Y.length();
        vector<vector<int>>dp(lx+1,vector<int>(ly+1,0));
        for(int i = 1; i <= lx; i++)
        {
            for(int j = 1; j <= ly; j++)
            {
                if(X[i-1] == Y[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
                else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
            }
        }
        cout << dp[lx][ly] << endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cmath>
#include <cstdarg>
#include <cstdlib>
#include <string>

#include <vector>
#include <tuple>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;

int nxtx[] = {1, 0};
int nxty[] = {0, 1};

int main(int argc, char const *argv[]) {
    int q;
    cin >> q;
    string X, Y;
    typedef pair<int, int> pint;
    for (int k = 0; k < q; k ++) {
        cin >> X;
        cin >> Y;
        int lenX = X.size();
        int lenY = Y.size();
        int dp[lenX + 1][lenY + 1];
        for (int i = 0; i <= lenX; i ++) {
            dp[i][0] = 0;
        }
        for (int i = 0; i <= lenY; i ++) {
            dp[0][i] = 0;
        }

        for (int i = 1; i <= lenX; i ++) {
            for (int j = 1; j <= lenY; j ++) {
                if (X[i-1] == Y[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                } else {
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        cout << dp[lenX][lenY] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>

int main(){
  int q;
  scanf("%d",&q);
  std::vector<std::string > X(q);
  std::vector<std::string > Y(q);
  for (int i=0; i<q; i++){
    std::cin >> X[i];
    std::cin >> Y[i];
  }
  int val[1002][1002];
  for (int k=0; k<q; k++){
    for (int i=0; i<X[k].length(); i++){
      for (int j=0; j<Y[k].length(); j++){
        if (X[k][i]==Y[k][j]){
          if (i==0 && j==0){
            val[i][j]=1;
          }
          if (i>0 && j>0){
            val[i][j]=val[i-1][j-1]+1;
          } else {
            val[i][j]=1;
          }
        } else if (i==0 && j==0){
          val[i][j]=0;
        } else if (i==0){
          val[i][j]=val[i][j-1];
        } else if (j==0){
          val[i][j]=val[i-1][j];
        } else {
          val[i][j]=std::max(val[i-1][j], val[i][j-1]);
        }
      }
    }
    int length = val[X[k].length()-1][Y[k].length()-1];
    printf("%d\n", length);
  }
  return 0;
}