Go: ///
// File:  0528.go
// Author: ymiyamoto
//
// Created on Thu Mar 22 01:52:09 2018
//
package main

import (
	"bufio"
	"fmt"
	"os"
)

var S, T string
var dp [4001][4001]int

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	for {
		scanner.Scan()
		S = scanner.Text()
		if len(S) == 0 {
			return
		}

		scanner.Scan()
		T = scanner.Text()

		for i := range dp {
			for j := range dp[i] {
				dp[i][j] = 0
			}
		}

		dp[0][0] = 0
		for i := range S {
			for j := range T {
				if S[i] == T[j] {
					dp[i+1][j+1] = max(dp[i+1][j], max(dp[i][j+1], dp[i][j]+1))
				} else {
					dp[i+1][j+1] = 0
				}
			}
		}
		var m int
		for i := range dp {
			for j := range dp[i] {
				m = max(m, dp[i][j])
			}
		}
		fmt.Println(m)
	}
}

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

using namespace std;

const int MAX = 4001;

int dp[2][MAX];
string in[2];

void solve(){

  for(int i = 0; i < in[0].length(); i++){
    for(int j = 0; j < in[1].length(); j++){
      if(in[0][i] == in[1][j]) dp[(i+1)&1][j+1] = dp[i&1][j]+1;
    }
  }
  int out = 0;
  //for(int i = 1; i <= in[0].length(); i++)
  for(int j = 1; j < MAX; j++){
    out = max(out,dp[1][j]);
    out = max(out,dp[0][j]);
  }
  cout << out << endl;
}

int main(){
  while(cin >> in[0] >> in[1]){
    memset(dp,0,sizeof(dp));
    solve();
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
 
using namespace std;
 
 int dp[4002][4002] = {0};
 
 
int main(){
 
char a[4003];
 
char b[4003];
 
 while (scanf("%s%s",a,b)!=EOF) {
 
 
   memset(dp,0,sizeof(dp));
   // int dp[4002][4002] = {0};
 
   /*
    for(int i = 0;i < 10;i++){
      for(int j = 0;j < 10;j++){
        cout << dp[i][j];
      }
    }
   */
 
    int ans = 0;
    //  scanf("%s",a);
 
  int alen = strlen(a);
 
  //  if(alen == 0) break;
 
  //  scanf("%s",b);
 
 
 
  int blen = strlen(b);
 
  //  cout << "ans:" << ans << endl;
 
 
 
  for(int i = 1;i <= alen;i++){
    for(int j = 1;j <= blen;j++){
      if(a[i-1] == b[j-1]){
    dp[i][j] = dp[i-1][j-1]+1;
      }
      ans = max(ans,dp[i][j]);
    }
  }
 
  printf("%d\n",ans);
 
  }
 
  return 0;
 
}

C++: #include <iostream>
#include <vector>
#include <set>
#include <map>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstring>
#include <tuple>

using namespace std;

typedef long long LL;
typedef vector<int> Array;
typedef pair<int, int> P;

const int INF = 1 << 29;
const LL MOD = 1000000009;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define all(c) (c).begin(), (c).end()

int dp[4001][4001];
int main()
{
	string s;
	string t;
	while (cin >> s && s != "")
	{
		memset(dp, 0, sizeof(dp));
		cin >> t;

		for (int i = 0; i < s.size(); i++)
		{
			for (int j = 0; j < t.size(); 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[s.size()][t.size()] << endl;
	}
	return 0;
}

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

int main(){
  int i,j;
  char a[4001][4001];
  string str,str2;
  while(cin >> str >> str2){
    
    for(i=0;i<=str.size();i++) a[i][0] = 0;
    for(j=0;j<=str2.size();j++) a[0][j] = 0;

    for(i=1;i<=str.size();i++){
      for(j=1;j<=str2.size();j++){
	if(str[i-1] == str2[j-1]) a[i][j] = a[i-1][j-1] + 1;
	else a[i][j] = a[i-1][j];
      }
    }
    cout << a[str.size()][str2.size()] << endl;
    
  }
  return 0;
}

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

int main() {
	string s, t;
	while(cin >> s >> t){
		int n = s.size(), m = t.size(), ans = 0;
		for(int i = 0; i < n; ++i){
			int c = 0;
			for(int j = 0, k = i; j < m && k < n; ++j, k++){
				if(s[k] == t[j]){
					c++;
				}
				else{
					ans = max(ans, c);
					c = 0;
				}
			}
		}
		for(int i = 0; i < m; ++i){
			int c = 0;
			for(int j = 0, k = i; j < n && k < m; ++j, k++){
				if(t[k] == s[j]){
					c++;
				}
				else{
					ans = max(ans, c);
					c = 0;
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second

//a???i?????????????????????b???j???????????????????????????????????±?????¨???????????????
int dp[4000][4000];

int main()
{
    string a,b;
    while(cin >>a >>b)
    {
        //initialize
        memset(dp,0,sizeof(dp));

        int A=a.size(), B=b.size();

        rep(i,A) if(a[i]==b[0]) dp[i][0]=1;
        rep(i,B) if(a[0]==b[i]) dp[0][i]=1;

        for(int i=1; i<A; ++i)for(int j=1; j<B; ++j)
        {
            if(a[i]==b[j]) dp[i][j]=dp[i-1][j-1]+1;
        }

        int ans=0;
        rep(i,A)rep(j,B) ans=max(ans,dp[i][j]);

        cout << ans << endl;
    }
    return 0;
}

C++: //JOI 0528 Common Sub-String
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
	while(1){
		string s, t; int roop1=0, roop2 = 0, save[1000000]={}, ans = 0;
		cin>>s;
		cin>>t;
		if(cin.eof())
		break;
	for (int i = 0; i<s.size(); i++){
		for (int j = 0; j<t.size(); j++){
			if (s[i] == t[j]){
				roop1 = 0;
				save[roop2] = 0;
				while (1){
					if (s[i + roop1] == 0 || t[j + roop1] == 0){
						break;
					}
					else{
						if (s[i + roop1] == t[j + roop1]){
							save[roop2] += 1;
							roop1++;
						}
						else if (s[i + roop1] != t[j + roop1]){
							roop2++;
							break;
						}
					}
				}
			}
		}
	}
	sort(save, save + roop2 + 1);
	ans = save[roop2];
	cout << ans << endl;
	}
	return 0;
}

C++: #include <cstdio>
#include <cstring>

int main(void){
  char ra[4005]={0};
  char rb[4005]={0};
  while(scanf("%s%s",ra,rb)!=EOF){
    int alen=strlen(ra);
    int blen=strlen(rb);
    //printf("%d\n%d\n",alen,blen);
    int i=0;
    int j=0;
    int k=0;
    int ans=0;
    for(i=0;i<alen;i++){
      for(j=0;j<blen;j++){
	if(ra[i]==rb[j]){
	  k=0;
	  while(k<blen-j && k<alen-i && ra[i+k]==rb[j+k]){
	    k++;
	  }
	  ans=(ans<k)?k:ans;
	}
      }
    }
    printf("%d\n",ans);
  }
  return 0;
}

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

struct node{
  int value;
  node *next;
  node(int value, node *next):value(value),next(next){};
};
#define INF ( 1 << 28 )
string lcs(string &a, string &b){
  map< int , vector< int > > M;
  for(int i = b.size() - 1 ; i >= 0 ; i--){
    M[b[i]].push_back(i);
  }
  vector<int > HM(a.size() + 1, INF);
  HM[0] = -INF;
  vector<node*> link(a.size() + 1);
  for(int i = 0 ; i < a.size() ; i++ ){
    if(M.count(a[i])){
      vector< int > NEWHM = M[a[i]];
      for(int j = 0 ; j < NEWHM.size() ; j++ ){
        int k = distance( HM.begin(), lower_bound( HM.begin(), HM.end(), NEWHM[j]));
        HM[k] = NEWHM[j];
        link[k] = new node( b[NEWHM[j]], link[k - 1]);
      }
    }
  }
  string ans;
  for(node* p = link[distance(HM.begin(),lower_bound(HM.begin(),HM.end(),INF-1))-1];p;p = p->next){
    ans += (char)p -> value;
  }
  reverse(ans.begin(), ans.end());
  return ans;
}
int main(){
  string a, b;
  while(cin >> a >> b){
    cout << lcs( a, b).size() << endl;
  }
}

C++: #include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[4000][4000];
int main(){
	string s,t;
	while(cin>>s>>t){
		int ans=0;
		memset(dp,0,sizeof(dp));
		for(int i=0;i<s.size();i++){
			for(int j=0;j<t.size();j++){
				if(i==0||j==0){
					if(s[i]==t[j])dp[i][j]=1;
				}else{
					if(s[i]==t[j])dp[i][j]=dp[i-1][j-1]+1;
				}
				ans=max(ans,dp[i][j]);
			}
		}
		cout<<ans<<endl;
	}
}