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

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()

		dp := make([][]int, len(S)+1)
		for i := range dp {
			dp[i] = make([]int, len(T)+1)
		}

		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 <string>
#include <algorithm>
using namespace std;
int main() {
	string s, t; int roop1, roop2 = 0, save[10000], ans = 0;
	while(1){
	cin >> s >> t;
	if(s=='')
	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 <iostream>
#include <string>
#include <algorithm>
using namespace std;

string s,t;
bool memo[4003][4003];
int main() {
	while(cin>>s>>t){
		for (int i = 0; i < 4003; i++){
			for (int j = 0; j < 4003; j++){
				memo[i][j] = false;
			}
		}
		
		for(int i=0;i<s.size();i++){
			for(int j=0;j<t.size();j++){
				if(s[i]==t[j]){
					memo[i][j]=true;
				}
			}
		}
		
		int ans=0;
		for(int i=0;i<s.size();i++){
			for(int j=0;j<t.size();j++){
				if(memo[i][j]){
					int sum=1;
					memo[i][j]=false;
					for(int k=1;k<max(s.size()-i,t.size()-j);k++){
						if(memo[i+k][j+k]){
							sum++;
							memo[i+k][j+k]=false;
						} else{
							ans=max(ans,sum);
							break;
						}
					}
				}
			}
		}
		
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
string s, t; int dp[4009];
int main() {
	while(cin >> s >> t) {
		int n = s.size(), m = t.size(), ret = 0;
		fill(dp, dp + m + 1, 0);
		for(int i = 0; i < n; i++) {
			for(int j = m - 1; j >= 0; j--) {
				if(s[i] == t[j]) {
					dp[j + 1] = dp[j] + 1;
					ret = max(ret, dp[j + 1]);
				}
				else dp[j + 1] = 0;
			}
		}
		cout << ret << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define FORR(i, m, n) for(int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)

int main(){
  string s, t;
  int dp[4001][4001];

  while(cin >> s >> t){
    int ans = 0;
    int sl = s.size(), tl = t.size();

    REP(i, sl){
      REP(j, tl) dp[i][j] = 0;
    }

    REP(i, sl){
      REP(j, tl){
        if(s[i] == t[j]){
          dp[i + 1][j + 1] = dp[i][j] + 1;
          ans = max(ans, dp[i][j] + 1);
        }
      }
    }
    cout << ans << endl;
  }
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define ALL(a) (a).begin(),(a).end()
#define INF (1<<29)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ull B =100000007;
typedef pair<int,int> P;
bool contain(string a,string b){
      int al=a.length(),bl=b.length();
      if(al>bl)return false;
      ull t=1;
      REP(i,al)t*=B;
      ull ah=0,bh=0;
      REP(i,al)ah=ah*B+a[i];
      REP(i,al)bh=bh*B+b[i];
      for(int i=0;i+al<=bl;i++){
            if(ah==bh)return true;
            if(i+al<bl)bh=bh*B+b[i+al]-b[i]*t;
      }
      return false;
}
int main(){
      string a,b;
      while(cin>>a>>b){
            int ans=0;
            if(a.length()>b.length())a.swap(b);
            string tmp;
            for(int i=0;i<a.size();i++){
                  for(int j=1;i+j<a.size();j++){
                        tmp.assign(a,i,j);
                        if(contain(tmp,b))ans=max(ans,j);
                  }
            }
            cout<<ans<<endl;
      }
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int N = 4001;
int dp[N][N];

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	string s, t;
	while(1){
		cin >> s;
		if(cin.eof() || s == "0"){ break; }
		cin >> t;
		
		for(size_t i = 0; i < s.size(); ++i){
			for(size_t j = 0; j < t.size(); ++j){
				dp[i][j] = 0;
			}
		}
		
		int ans = 0;
		for(size_t i = 0; i < s.size(); ++i){
			for(size_t j = 0; j < t.size(); ++j){
				if(s[i] == t[j]){
					dp[i + 1][j + 1] = dp[i][j] + 1;
					ans = max(ans, dp[i + 1][j + 1]);
				}
				else
					dp[i + 1][j + 1] = 0;
			}
		}
		
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}

class RollingHash{
private:
    int M = 1e9+7;
    int base = 1213;
    string str;
    int n;
    vector<int> pow,sum;

    void build(){
        n = str.size();
        sum = pow = vector<int> (n+1);
        sum[0] = 0;
        pow[0] = 1;
        for(int i = 0; i < n; i++){
            sum[i+1] = (str[i] + sum[i] * base) % M;
            pow[i+1] = pow[i] * base % M;
        }
    }


public:
    RollingHash(){}
    RollingHash(string in){
        str = in;
        build();
    }
    RollingHash(string in, int p, int mod){
        M = mod;
        base = p;
        str = in;
        build();
    }

    int calc(int l, int r){//[l,r)のハッシュ値を計算
        int len = r - l;
        int ret = sum[r] + M;
        ret -= sum[l] * pow[len] % M;
        return ret % M;
    }

};

signed main(void) {
    string s,t;
    while(cin >> s){
        cin >> t;
        RollingHash rs(s);
        RollingHash rt(t);
        int n = s.size();
        int m = t.size();
        int ok = 0, ng = min(n,m)+1;
        while(ng - ok > 1){
            int mid = (ok + ng)/2;
            set<int> st;
            rep(i,n-mid){
                st.insert(rs.calc(i,i+mid));
            }
            bool c = false;
            rep(i,m-mid){
                if(st.count(rt.calc(i,i+mid)))c = true;
            }
            if(c)ok = mid;
            else ng = mid;
        }
        cout << ok << endl;
    }

}