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;
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;//??¨X[0]????????\?????????
	Y=' '+Y;//??¨Y[0]????????\?????????
	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;
	for(int i=0;i<n;i++)
	{
		cin>>s1>>s2;
		cout<<lcs(s1,s2)<<endl;
	}
}

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

using namespace std;

class Dataset{
private:
 string X;
 string Y;
public:
 Dataset(){
  string tmp = "";
  cin >> tmp; X = tmp; X = " " + X;
  cin >> tmp; Y = tmp; Y = " " + Y;
 };

 unsigned long getLongestCommonLength(){
  unsigned long Xsize = X.size();
  unsigned long Ysize = Y.size();
 
  vector< vector<unsigned long> > LC(Xsize, vector<unsigned long>(Ysize, 0));

  for(unsigned long X_i = 1; X_i < Xsize; ++X_i){
   for(unsigned long Y_i = 1; Y_i < Ysize; ++Y_i){
    unsigned long tmp = LC[X_i][Y_i-1];
    if(X[X_i] == Y[Y_i]) ++tmp;
    if(tmp < LC[X_i-1][Y_i]) tmp = LC[X_i-1][Y_i];
    LC[X_i][Y_i] = tmp;
   }
  }

  return LC[Xsize-1][Ysize-1];
 };


protected:
};

int main(){
 long datasetNum = 0; cin >> datasetNum;
 for(unsigned long dataset_i = 0; dataset_i < datasetNum; ++dataset_i){
  Dataset data;
  cout << data.getLongestCommonLength() << endl;
 }

 return 0;
};

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

int main(){
    int q;
    cin >> q;
    rep(ss,q){
        string x,y;
        cin >> x >> y;
        int xlen = x.size();
        int ylen = y.size();
        int dp[1001][1001];//dp[i][j]xi,yjまででのlcs
        memset(dp,0,sizeof(dp));
        rep(i,xlen)rep(j,ylen){
            if(x[i]==y[j]){
                dp[i+1][j+1] = max(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[xlen][ylen] << endl;
    }
}

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

int lcs(string &x,string &y);

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

int lcs(string &x,string &y)
{
	const int maxlen=1000+24;
	vector<vector<int>> dp(maxlen,vector<int>(maxlen));

	int lenx=x.size();
	int leny=y.size();
	for (int i=0;i<=lenx;++i)
	{
		dp[i][0]=0;
	}
	for (int j=0;j<=leny;++j)
	{
		dp[0][j]=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]);
			}
		}
	}

	int maxdp=0;
	for (int j=1;j<=leny;++j)
	{
		maxdp=max(maxdp,dp[lenx][j]);
	}
	return maxdp;
}

C++: #include <bits/stdc++.h>
#define r(i,a,n) for(int i=a;i<n;i++)
using namespace std;
int main(){
  int n;
  cin>>n;
  while(n--){
    string s,ss;
    cin>>s>>ss;
    int a[ss.size()+2][s.size()+2];
    r(i,0,ss.size()+2)r(j,0,s.size()+2)a[i][j]=0;
    ss=' '+ss;
    s=' '+s;
    r(i,1,ss.size()+1){
      r(j,1,s.size()+1){
        if(ss[i]==s[j])a[i][j]=a[i-1][j-1]+1;
        else a[i][j]=max(a[i-1][j],a[i][j-1]);
      }
    }
    cout<<a[ss.size()-1][s.size()-1]<<endl;
  }
}

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

int calcLCS(string s, string t) {
    if (s.length() < t.length()) {
        string temp = s;
        s = t;
        t = temp;
    }

    int table[s.length()+1][t.length()+1];
    for (int i=0; i<=s.length(); i++) {
        table[i][0] = 0;
    }
    for (int j=0; j<=t.length(); j++) {
        table[0][j] = 0;
    }

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

    return table[s.length()][t.length()];
}

int main() {
    int n;
    cin >> n;
    for (int i=0; i<2*n; i+=2) {
        string x, y;
        cin >> x >> y;

        cout << calcLCS(x, y) << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
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=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 <string>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    cin >> n;
    while(n)
    {
        n--;
        string X,Y;
        int i=0,j=0;
        cin >> X;
        cin >> Y;
        int L[1001][1001];
        for(i=0;i<1001;i++)
            for(j=0;j<1001;j++)
               L[i][j]=0;
        for(i=1;i<=X.size();i++)
            for(j=1;j<=Y.size();j++)
            {
                if(X[i-1]==Y[j-1])
                    L[i][j]=1+L[i-1][j-1];
                else    L[i][j]=max(L[i][j-1],L[i-1][j]);
            }
        cout << L[X.size()][Y.size()] << endl;
    }
    return 0;
}

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

using namespace std;
static const int N = 1000;

int lenCommon(string s1, string s2)
{
	int len = max(int(s1.size()), int(s2.size()));
	vector<vector<int> > dp(N+1, vector<int>(N+1, 0));

	s1 = " " + s1;
	s2 = " " + s2;

	for (int i = 0; i <= len; ++i) dp[i][0] = 0;
	for (int i = 0; i <= len; ++i) dp[0][i] = 0;

	int ret = 0;
	for (int i = 1; i <= len; ++i)
	{
		for (int j = 1; j <= len; ++j)
		{
			if (s1[i] == s2[j])
				dp[i][j] = dp[i - 1][j - 1] + 1;
			else
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);

			ret = max(ret, dp[i][j]);
		}
	}
	return ret;
}

int main()
{
	string s1, s2;
	int num; cin >> num;
	for (int i = 0; i < num; ++i)
	{
		cin >> s1 >> s2;
 		int res = lenCommon(s1, s2);
		cout << res << endl;
	}
}

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

using namespace std;

#define max(a, b) a > b ? a : b;

int main() {
  int q;
  int i, j, k;
  string x, y;
  cin >> q;
  for (i = 0; i < q; i++) {
    int maxVal = 0;
    cin >> x;
    cin >> y;
    int lenx = x.size();
    int leny = y.size();
    x = ' ' + x;
    y = ' ' + y;
    int c[lenx + 1][leny + 1];
    for (j = 0; j <= lenx; ++j) {
      c[j][0] = 0;
    }
    for (j = 0; j <= leny; ++j) {
      c[0][j] = 0;
    }
    for (j = 1; j <= lenx; ++j) {
      for (k = 1; k <= leny; ++k) {
        if (x[j] == y[k]) {
          c[j][k] = c[j - 1][k - 1] + 1;
        } else {
          c[j][k] = max(c[j - 1][k], c[j][k - 1]);
        }
        maxVal = max(maxVal, c[j][k]);
      }
    }
    cout << maxVal << '\n';
  }

  return 0;
}