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>
int main()
{
  int n;
  int an[1000];
  int l=0;
  std::cin>>n;
  std::string x;
  std::string y;
  int ans[1001][1001];
  int j,k;
  for(int i=0;i<n;i++){
    std::cin>>x;
    std::cin>>y;
    for(int j=0;j<=1000;j++){
      for(int k=0;k<=1000;k++){
	ans[j][k]=0;
      }
    }
    for(int j=0;j<=x.size();j++){
      for(int k=0;k<=y.size();k++){
	if(x[j]==y[k]){
	  ans[j][k]=ans[j-1][k-1]+1;
	}else{
	  if(ans[j][k-1]>ans[j-1][k]){
	    ans[j][k]=ans[j][k-1];
	  }else{
	    ans[j][k]=ans[j-1][k];
	  }
	}
      }
    }
    std::cout<<ans[x.size()-1][y.size()-1]<<'\n';  
  }
  return 0;
}

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

int main() {
  int q; cin >> q;
  string A, B; cin >> A >> B;
  for (int i=0; i<q; i++) {
    vector<vector<int>> LCS(A.length()+1, vector<int>(B.length()+1,0));
    for (int j=1; j<=A.length(); j++) {
      for (int k=1; k<=B.length(); k++) {
        if(A[j]==B[k]) LCS[j][k]=LCS[j-1][k-1]+1;
        else LCS[j][k]=max(LCS[j-1][k],LCS[j][k-1]);
      }
    }
    cout << LCS[A.length()][B.length()] << "\n";
  }
}

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

#define N 1000

int lcs(string X,string Y){
	int c[N+1][N+1];
	int m = X.size();
	int n = Y.size();
	X = ' ' + X;
	Y = ' ' + Y;
	for(int i=0;i<=m;i++)c[i][0]=0;
	for(int i=1;i<=n;i++)c[0][i]=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 if(c[i][j-1]>=c[i-1][j]){
				c[i][j] = c[i][j-1];
			}else{
				c[i][j] = c[i-1][j];
			}
		}
	}
	return c[m][n];
}

int main(){
	int n;
	string X,Y;
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> X >> Y;
		printf("%d\n",lcs(X,Y));
	}
	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=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<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[1000][1000];

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 <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>

using namespace std;

string s1,s2;

int L(int i,int j){
	if(i<=0||j<=0)return 0;
	else if(s1.at(i)==s2.at(j))return 1+L(i-1,j-1);
	else{
		return L(i,j-1)>L(i-1,j)?L(i,j-1):L(i-1,j);
	}
}

int main(){
	int q;
	cin>>q;
	for(int i=0; i<q; i++){
		cin>>s1>>s2;
		cout<<L(s1.size()-1,s2.size()-1)+1<<endl;
	}
}

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

static const int N = 100;

int main() {
  int n, p[N + 1], m[N + 1][N + 1];
  cin >> n;
  for ( int i = 1; i <= n; i++ ) {
    cin >> p[i - 1] >> p[i];
  }

  for ( int i = 1; i <= n; i++ ) m[i][i] = 0;

  for ( int l = 2; l <= n; l++ ) {
    for ( int i = 1; i <= n - l + 1; i++ ) {
      int j = i + l - 1;
      m[i][j] = (1 << 21);
      for ( int k = i; k <= j - 1; k++ ) {
        m[i][j] = min(m[i][j], m[i][k] + m[k + 1][j] + p[i - 1] * p[k] * p[j]);
      }
    }
  }

  cout << m[1][n] << endl;

  return 0;
}

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

using namespace std;

int main() {
	int q;
	string strX, strY;

	cin >> q;
	for (int n = 0; n < q; n++) {
		cin >> strX;
        cin >> strY;
		char X[strX.length() + 1];
		for (int x = 0; x < strX.length(); x++) {//文字列を配列に入れる
			X[x + 1] = strX[x];
		}

        char Y[strY.length() + 1];
		for (int y = 0; y < strY.length(); y++) {//文字列を配列に入れる
			Y[y + 1] = strY[y];
		}

        int c[strX.length()+1][strY.length()];
        int maxLen = 0;

		for (int i = 0; i<=strX.length(); i++){
            for(int j = 0; j<=strY.length(); j++){
                if(i == 0 || j == 0){
                    c[i][j] = 0;
                }
                else if(X[i] == Y[j]){
                    c[i][j] = c[i-1][j-1] + 1;
                } else {
                    c[i][j] = max(c[i][j-1], c[i-1][j]);
                }

                if(c[i][j] > maxLen){
                    maxLen = c[i][j];
                }
            }
        }
        cout << maxLen << endl;
	}

    return 0;
}

C++: #include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;

int dp[1001][1001];

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

	vector<int> res;
	for(int i = 0; i < q; i++){
		string s, t;
		cin >> s >> t;

		fill(dp[0], dp[1000] + 1000, 0);
		for(int j = 0; j < s.size(); j++){
			for(int k = 0; k < t.size(); k++){
				if(s[j] == t[k]) dp[j+1][k+1] = dp[j][k] + 1;
				else dp[j+1][k+1] = max(dp[j+1][k], dp[j][k+1]);
			}
		}

		res.push_back(dp[s.size()][t.size()]);
	}

	for(int i = 0; i < q; i++) cout << res[i] << '\n';

	return 0;
}

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

int dp[1001][1001];
string str1, str2;
int n;

int solve(string str1, string str2) {
  memset(dp, 0, sizeof(dp));
  for(int r = 0; r < str2.size(); r++) {
    for(int c = 0; c < str1.size(); c++) {
      if(str2[r] == str1[c]) {
        dp[r + 1][c + 1] = dp[r][c] + 1;
      }
      else {
        dp[r + 1][c + 1] = max(dp[r + 1][c], dp[r][c + 1]);
      }
    }
  }
  return dp[str2.size()][str1.size()];
}

int main() {
  cin >> n;
  for(int i = 0; i < n; i++) {
    cin >> str1 >> str2;
    cout << solve(str1, str2) << endl;
  }
}