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

int dp[1000+1][1000+1];

int main() {
    int n; 
    cin >> n;
    while(n--) {
        string s, t; cin >> s >> t;
        int N = s.length();
        int M = t.length();
        dp[0][0] = 0;
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < M; j++) {
                if(s[i] == t[j]) dp[i + 1][j + 1] = dp[i][j] + 1;
                else dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
            }
        }
        cout << dp[N][M] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstring>
#include <algorithm>
#include <climits>
#include <string>
#include <vector>

using namespace std;
int q;
string text;
int ans[160];
int dp[1100][1100];

int main(){
    cin >> q;
    vector<string> texts;

    for(int i=0;i<q*2;i++){
         cin >> text;
         texts.push_back(text);
    }
    for(int k=0;k<q;k++){
        memset(dp, 0, sizeof(dp));
        for(int i=0;i<texts[2*k].size();i++){
            for(int j=0;j<texts[2*k+1].size();j++){
                if(texts[2*k][i]==texts[2*k+1][j]) dp[i+1][j+1] = max(dp[i][j]+1, max(dp[i+1][j], dp[i][j+1]));
                else dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
            }
        }
        ans[k]=dp[texts[2*k].size()][texts[2*k+1].size()];
    } 
    for(int i=0;i<q;i++)cout << ans[i] << endl;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
static const int N = 1000;

int lcs(string, string);

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;
}

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;
}

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

int LCS(string p, string q) {
	vector<vector<int> > f(p.size()+1, vector<int>(q.size()+1,0));
	int t;
	for (int i = 1; i <= p.size(); i++) {
		for (int j = 1; j <= q.size(); j++) {
			if (p[i-1] == q[j-1])
				f[i][j] = f[i - 1][j - 1] + 1;
			else if (f[i][j - 1] > f[i - 1][j])
				f[i][j] = f[i][j - 1];
			else
				f[i][j] = f[i - 1][j];
		}
	}
	return f[p.size()][q.size()];
}

int main() {
	int n;
	string p[150];
	cin >> n;
	for (int i = 0; i < n; i ++) {
		cin >> p[2*i] >> p[2*i + 1];
	}
	for (int i = 0; i < n; i++) {
		cout << LCS(p[2*i], p[2 * i + 1])<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
    using namespace std;
int lcs(string x,string y){
    int c[1010][1010];
    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(){
    int q;
    string x;
    string y;
    scanf("%d",&q);
    for (int i=0; i<q; i++) {
        cin>>x>>y;
        cout<<lcs(x, y)<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include <cstdlib>
#include<list>
#include<stdio.h>
#include<algorithm>
#include<string>
#include<stack>
#define MAX 1001
using namespace std;
int main() {
	int n; cin >> n;
	string x, y; int data[MAX][MAX] = {0,0};
	for (int d = 0; d <n; d++)
	{
		cin >> x >> y;
		for (int i = 0; i < y.size(); i++)
		{
			for (int j = 0; j < x.size(); j++)
			{
				if (y[i] == x[j]) {
					data[j + 1][i + 1] = data[j][i] + 1;
				}
				else {
					data[j + 1][i + 1] = max(data[j + 1][i], data[j][i + 1]);
				}
			}
		}
		cout << data[x.size()][y.size()]<<endl;
	}
	
	return 0;
}

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

string s, t;
int dp[1001][1001];
int main()
{
    int p;
    cin >> p;
    while(p>0)
    {
        p--;
        cin >> s >> t;
        int n = s.size();
        int m = t.size();
        for (int i = 1; i <= n; i++)
            dp[i][0] = 0;
        for (int i = 1; i <= m; i++)
            dp[0][i] = 0;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                if (s[i - 1] == t[j - 1]) {
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                }
                else if (dp[i - 1][j] >= dp[i][j - 1]) {
                    dp[i][j] = dp[i - 1][j];
                }
                else {
                    dp[i][j] = dp[i][j - 1];
                }
            }
        }
        cout << dp[n][m] << endl;
    }

}

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

#define MAX 1024

int LCS[MAX][MAX];

int solve(string s1,string s2){
  if(s1.length() > s2.length())
	swap(s1,s2);
  for(int i=1;i<=s1.length();i++){
    for(int j=1;j<=s2.length();j++){
      if(s1[i-1]==s2[j-1])
	LCS[i][j]=LCS[i-1][j-1]+1;
      else
	LCS[i][j]=max(LCS[i][j-1],LCS[i-1][j]);
    }
  }
  return LCS[s1.length()][s2.length()];
}


int main(){
  string s1, s2;
  int n;
  cin >> n;
  for(int i=0;i<n;i++){
    for(int i=0;i<MAX;i++){
      for(int j=0;j<MAX;j++){
        LCS[i][j] = 0;
      }
    }
    cin>>s1>>s2;
    cout<<solve(s1,s2)<<endl;
    /*
    for(int i=0;i<=s1.length();i++){
      for(int j=0;j<=s2.length();j++){
	cout << LCS[i][j] << "  " ;
      }
      cout << endl;
    }*/
  }

}

C++: #include <iostream>
#include <vector>
#define eol '\n'
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    string x,y;
    cin >> x >> y;

    int m,n;
    m = x.length();
    n = y.length();

    vector< vector<int> > mat(m+1, vector<int>(n+1,0));

    for (int sum = 2; sum <= m+n; sum++) {
      for (int j = 1; j <= min(sum-1,m); j++) {
        int k = sum - j;

        int plus = x[j-1] == y[k-1] ? 1 : 0;
        mat[j][k] = max(max(mat[j-1][k], mat[j][k-1]), mat[j-1][k-1] + plus);
      }
    }

    cout << mat[m][n] << eol;
  }
  
  return 0;
}