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

void LCS(string X, string Y){
    X = ' ' + X; //＋演算子でX[0]に空白を挿入
    Y = ' ' + Y;
    int C[X.length()][Y.length()];
    
    for (int i = 0; i < X.length(); i++){
        C[i][0] = 0;
    }
    for (int i = 0; i < Y.length(); i++){
        C[0][i] = 0;
    }
    
    for (int i = 1; i < X.length(); i++){
        for (int j = 1; j < Y.length(); j++){
            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]);
        }
    }
    cout << C[X.length()-1][Y.length()-1] << endl;
    
}

int main(){
    int n;
    string X, Y;
    cin >> n;
    
    for (int i = 0; i < n; i++ ){
        cin >> X;
        cin >> Y;
        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 = 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 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 <iostream>
#include <algorithm>
using namespace std;

#define REP(i, n) for(int (i) = 0; (i) < (n); (i)++)

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

int solve(int i, int j)
{
  if(dp[i][j] >= 0)
    return dp[i][j];
  if(i >= str1.size() || j >= str2.size()){
    if(i < str1.size()){
      dp[i][j - 1] = 0;
      return solve(i + 1, k);
    }else{
      return 0;
    }
  }
  if(str1[i] == str2[j]){
    k = j + 1;
    int n, m = 0;
    for(int l = i + 1; l < str1.size(); l++){
      n = solve(l, j + 1);
      m = (n > m) ? n : m;
    }
    dp[i][j] = m + 1;
    return dp[i][j];
  }else{
    return solve(i, j + 1);
  }
}
  
int main()
{
  int q, n, m, ans = 0;
  cin >> q;
  cin.ignore();
  REP(i, q){
    getline(cin, str1);
    getline(cin, str2);
    if(str1.size() < str2.size())
      swap(str1, str2);
    fill(&dp[0][0], &dp[1001][0], -1);
    REP(j, str1.size()){
      n = solve(j, 0);
      ans = (n > ans) ? n : ans;
    }
    cout << ans << endl;
    ans = 0;
  }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define N 1000

int lcs(string X,string Y){
	int c[1001][1001];
	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<iostream>
#include<vector>
#include<string>
using namespace std;
const int NIL = -1;
void SearchMaxSubString(string & a,string & b,
						vector<vector<int> > & maxlen,
						int s,
						int t)
{
	int lengthAB = 0;
	if (a[s] == b[t]) {
		lengthAB++;
		if ((s+1) < (int)a.size() && (t+1) < (int)b.size()) {
			if (maxlen[s+1][t+1] == NIL) {
				SearchMaxSubString(a,b,maxlen,s+1,t+1);
			}
			lengthAB += maxlen[s+1][t+1];
		}
		maxlen[s][t] = lengthAB;
	}
	else {
		int lengthA = 0;
		int lengthB = 0;
		if ((s+1) < (int)a.size()) {
			if (maxlen[s+1][t] == NIL) {
				SearchMaxSubString(a,b,maxlen,s+1,t);
			}
			lengthA = maxlen[s+1][t];
		}
		if ((t+1) < (int)b.size()) {
			if (maxlen[s][t+1] == NIL) {
				SearchMaxSubString(a,b,maxlen,s,t+1);
			}
			lengthB = maxlen[s][t+1];
		}
		maxlen[s][t] = std::max(lengthA,lengthB);
	}
}

int main(){
	int n;
	cin >> n;
	for(; n > 0; --n) {
		string a,b;
		cin >> a >> b;
		vector<vector<int> > maxlen;
		maxlen.resize(a.size());
		for(unsigned int i=0;i<a.size();++i) {
			maxlen[i].resize(b.size());
			for(unsigned j=0;j<maxlen[i].size();++j){
				maxlen[i][j] = NIL;
			}
		}
		SearchMaxSubString(a,b,maxlen,0,0);
		cout << maxlen[0][0] << std::endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string>
int main(){
  int q;
  std::cin>>q;
  int i,j,k;
  std::string x,y;
  int a[1001][1001];
  int knum[q];
  for(i=0; i<1001; i++){
    for(j=0; j<1001; j++){
      a[i][j]=0;
    }
  }
  for(k=0; k<q; k++){
    std::cin>>x;
    std::cin>>y;
    for(i=1; i<x.size()+1; i++){
      for(j=1; j<y.size()+1; j++){
	if(x[i-1]==y[j-1]){
	  a[i][j]=a[i-1][j-1]+1;
	}else{
	  if(a[i-1][j]>a[i][j-1]){
	    a[i][j]=a[i-1][j];
	  }else{
	    a[i][j]=a[i][j-1];
	  }
	}
      }	
    }
    knum[k]=a[x.size()][y.size()];
  }
  for(k=0; k<q; k++){
    std::cout<<knum[k]<<"\n";
  }
  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 = 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 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;
typedef long long ll;
//template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
//template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
 
/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
*/

int lcm(string s, string t, vector<vector<int>> dp){
    int m = s.size();
    int n = t.size();
    int maxl = 0;
    s = " " + s;
    t = " " + t;
    for(int i=0; i<=m; i++) dp[i][0] = 0;
    for(int j=0; j<=n; j++) dp[0][j] = 0;

    for(int i=1; i<=m; i++){
        for(int j=1; j<=n; j++){
            if(s[i] == t[j]) dp[i][j] = dp[i-1][j-1]+1;
            else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
            maxl = max(maxl, dp[i][j]);
        }
    }

    return maxl;
}

int main(){
    vector<vector<int>> dp(1001, vector<int>(1001, 0));

    int n; cin >> n;
    for(int i=0; i<n; i++){
        string s, t; cin >> s >> t;
        cout << lcm(s, t, dp) << endl;
    }
}