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 <bits/stdc++.h>
using namespace std;

int lcs(const string& X,
    const string& Y) 
{
    int lenX = X.length();
    int lenY = Y.length();
    vector<vector<int>> dp(lenX + 1, vector<int>(lenY + 1));
    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][j-1], dp[i-1][j]);
            }
        }
    }
    return dp[lenX][lenY];
}

int main() {
    int q;
    cin >> q;
    vector<string> Xs(q);
    vector<string> Ys(q);
    for (int i = 0; i < q; ++i) {
        cin >> Xs[i];
        cin >> Ys[i];
    }
    for (int i = 0; i < q; ++i) {
        cout << lcs(Xs[i], Ys[i]) << endl;
    }



}

C++: // #include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <set>
#include <queue>
#include <cstdio>
using namespace std;
#define rep(i, n) for(int i=0;i<n;i++)
#define irep(i, n) for(int i=n-1;i>=0;i--)
#define lrep(i, n) for(long long i=0;i<n;i++)
typedef long long ll;
typedef vector<long long> llv;
typedef vector<vector<long long> > llvv;

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;
    rep(i,n){
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
    return 0;
}

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

using namespace std;

int lcslen[1001][1001]={0};
int compute_lcslen(const string &x,const string &y);

int main(){
  int q;
  cin >> q;
  for(int i=0;i<q;++i){
    string x,y;
    cin >> x;
    cin >> y;
    cout << compute_lcslen(x,y) << endl;
  }

  return 0;
}

int compute_lcslen(const string &x,const string &y){
  for(int i=1;i<=x.length();++i){
    for(int j=1;j<=y.length();++j){
      if(x[i-1]==y[j-1]){
        lcslen[i][j] = lcslen[i-1][j-1]+1;
      }else{
        lcslen[i][j] = max(lcslen[i-1][j],lcslen[i][j-1]);
      }
    }
  }
  return lcslen[x.length()][y.length()];
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int dp[111][111];
int lcs(string x,string y){
	for(int i=0;i<12321;i++)dp[i/111][i%111]=0;
	for(int i=0;i<=y.size();i++){
		for(int j=0;j<=x.size();j++){
			if(i==0 || j==0){dp[i][j]=0;}
			else if(x[j]==y[i]){dp[i][j]=dp[i-1][j-1]+1;}
			else{dp[i][j]=max(dp[i][j-1],dp[i-1][j]);}
		}
	}
	int MAXN=0;
	for(int i=0;i<=y.size();i++){
		for(int j=0;j<=x.size();j++){
			MAXN=max(MAXN,dp[i][j]);
		}
	}
	return MAXN;
}
int main(){
	int n;string S,T;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>S>>T;cout<<lcs(S,T)<<endl;
	}
	return 0;
}

C++: #include <stdio.h>
#include <string.h>
int main(){
  int k,m,n,i,j,q,c[1001][1001],max=0;
  char X[1000],Y[1000];
  scanf("%d",&q);
  for(i=0;i<q;i++){
    scanf("%s %s",X,Y);
    m=strlen(X);
    n=strlen(Y);
    for(j=m;j>=1;j--){
      X[j]=X[j-1];
    }
    X[0]=' ';
    X[m+1]='\0';
    for(j=n;j>=1;j--){
      Y[j]=Y[j-1];
    }
    Y[0]=' ';
    Y[m+1]='\0';
    for(j=1;j<=m;j++)c[j][0]=0;
    for(j=1;j<=n;j++)c[0][j]=0;
    for(j=1;j<=m;j++){
      for(k=1;k<=n;k++){
	if(X[j]==Y[k])c[j][k]=c[j-1][k-1]+1;
	else{
	  if(c[j][k-1]>c[j-1][k])c[j][k]=c[j][k-1];
	  else c[j][k]=c[j-1][k];
	}
	if(max<c[j][k])max=c[j][k];
      }
    }
    printf("%d\n",max);
    max=0;
  }
  return 0;
}

C++: #include <iostream>

using namespace std;

int lcs(string X, string Y) {
  int memo[1010][1010];
  // 初期化
  for (int i = 0; i < X.size(); i++) {
    memo[i][0] = 0;
  }
  for (int i = 0; i < Y.size(); i++) {
    memo[0][i] = 0;
  }
  int maxl = 0;
  for (int i = 0; i < X.size(); i++) {
    for (int j = 0; j < Y.size(); j++) {
      if (X[i] == Y[j]) {
        memo[i][j] = memo[i-1][j-1] + 1;
      } else {
        memo[i][j] = max(memo[i-1][j], memo[i][j-1]);
      }
      maxl = max(maxl, memo[i][j]);
    }
  }
  return maxl;
}

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

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

int lcs(string X,string Y){
	int x=X.size(),y=Y.size();
	X = ' '+X;
	Y= ' '+Y;
	int c[1000+10][1000+10];
	for(int i=0;i<1010;i++){
		c[i][0]=0;
		c[0][i]=0;
	}
	for(int i=1;i<=x;i++){
		for(int j=1;j<=y;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]);
			}
		}
	}
	return c[x][y];
}

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

C++: #include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int num;
string X;
string Y;
char c1,c2;
int longest;
int L[1010][1010];
int main()
{
    cin>>num;
    for(int i=0;i<num;i++)
    {
        longest=0;
        cin>>X>>Y;
        for(int m=0;m<=X.size();m++)
            for(int n=0;n<=Y.size();n++)
            {
                if(m<=0||n<=0)
                    L[m][n]=0;
                else if(X[m-1]==Y[n-1])
                    L[m][n]=1+L[m-1][n-1];
                else
                    L[m][n]=max(L[m][n-1],L[m-1][n]);
            }
        for(int a=0;a<=X.size();a++)
            for(int b=0;b<=Y.size();b++)
            {
                if(L[a][b]>longest)
                    longest=L[a][b];
            }
        cout<<longest<<endl;
    }
    return 0;
}

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 <algorithm>
#include <string>
#include <cmath>
using namespace std;

int main(){
  int q;
  cin >> q;
  for(int m=0; m<q; m++){
    string wordx, wordy;
    cin >> wordx >> wordy;
    int L[1010][1010]={0};
    
    for(int j=0; j<wordy.size(); j++){
      for(int i=0; i<wordx.size(); i++){
	if(wordx[i]==wordy[j]){
	  L[i+1][j+1] = 1+L[i][j];
	}
	else{
	  L[i+1][j+1] = max(L[i+1][j], L[i][j+1]);
	}
      }
    }
    cout << L[wordx.size()][wordy.size()] << endl;
    
    
  }
}