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<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
#define abs(x) (x >= 0 ? x : -(x))
#define ceil(a, b) a / b + !!(a % b)
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }
template<typename T> T pow(T a, int b) { return b ? pow(a * a, b / 2) * (b % 2 ? a : 1) : 1; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }


typedef unsigned long long ull;
ull B = 1e8 + 7;
const int maxn = 4001;
vector<ull> v(maxn);
vector<ull> v2(maxn);
string ss, tt;
ull t[maxn];

void init() {
    t[0] = 1;
    rep(i, maxn - 1) t[i + 1] = t[i] * B;
}
void build(string a) {
    int al = (int)a.length();
    
    rep(i, al) {
        v[i + 1] = v[i] + t[al - 1 - i] * a[i];
    }
}


void build2(string a) {
    int al = (int)a.length();
    
    rep(i, al) {
        v2[i + 1] = v2[i] + t[al - 1 - i] * a[i];
    }
    // rep(i, al) cout << v2[i] << endl;
}

bool check(int x) { // 長さxの文字列ありますか
    set<ull> s; // 文字列管理
    
    for (int i = 0; i + x <= sz(ss); ++i) {
        ull tmp = (v2[i + x] - v2[i]) * t[i];
        // cout << tmp << endl;
        s.insert(tmp);
    }
    
    for (int i = 0; i + x <= sz(tt); ++i) {
        ull tmp = (v[i + x] - v[i]) * t[i];
        // cout << i << " " << x << " " << tmp << endl;
        if (s.find(tmp) != s.end()) {
            // cout << x << " " << i << " " << tmp << endl;
            return 1;
        }
    }
    
    return 0;
}

int binary_search(int ok, int ng) {
    // assert(check(ok) == true);
    // assert(check(ng) == false);
    while (abs(ng - ok) > 1) {
        int mid = (ng + ok) / 2;
        (check(mid) ? ok : ng) = mid;
        // cout << "ok " << ok << " " << ng << endl;
    }
    return ok;
}


int main() {
    init();
    while (cin >> ss >> tt) {
        // cout << sz(ss) << " " << sz(tt) << endl;
        int nn = abs(sz(ss) - sz(tt));
        // cout << nn << endl;
        if (sz(ss) > sz(tt)) { // ここ間違えてた!!
            rep(i, nn) tt += '1';
        } else {
            rep(i, nn) ss += '1';
        }
        // cout << sz(ss) << " " << sz(tt) << endl;
        
        build(tt);
        build2(ss);
        // cout << v[1] << " " << v2[1] << endl;
        // cout << v2[1] << " " << (v[3] - v[2]) * t[2] << endl;
        cout << binary_search(0, sz(ss) + 1) << endl;
        
        ss = ""; tt = "";
    }
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef tuple<int, int> duo;
const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int Mod = 1000000000 + 0;
//{{{ templates
#define TT_ template<typename T>inline
#define TTF_ template<typename T,typename F>inline
TT_ T sq(T x){return x*x;}
TT_ T In(){T x;cin>>x;return x;}
TT_ void Out(T&x){cout<<x;}
TT_ void sort(T&v){sort(begin(v),end(v));}
TT_ void revs(T&v){reverse(begin(v),end(v));}
TT_ void uniq(T&v){sort(v);v.erase(unique(begin(v),end(v)),end(v));}
TT_ int ubnd(T&v,typename T::value_type&x){return upper_bound(begin(v),end(v),x)-begin(v);}
TT_ int lbnd(T&v,typename T::value_type&x){return lower_bound(begin(v),end(v),x)-begin(v);}
TTF_ void inpt(T&v,int n,F f){for(v.reserve(n);n--;v.emplace_back(f()));}
TTF_ void show(T&v,F f,string d=" ",string e="\n"){int i=0;for(auto&x:v)i++&&(cout<<d),f(x);cout<<e;}
TT_ typename T::iterator minel(T&v){return min_element(begin(v),end(v));}
TT_ typename T::iterator maxel(T&v){return max_element(begin(v),end(v));}

inline void fast_io(){ios::sync_with_stdio(0);cin.tie(0);}
inline int in(){int x;scanf("%d",&x);return x;}
inline ll pow_mod(ll a,ll k,ll m){ll r=1;for(;k>0;a=a*a%m,k>>=1)if(k&1)r=r*a%m;return r;}
inline ll mod_inv(ll a,ll p){return pow_mod(a,p-2,p);}
//}}} priority_queue queue deque front stringstream max_element min_element insert count make_tuple

void z_algo(const string& s, vector<int>& z)
{
  const int n = s.size();
  z.assign(n, 0);
  int R, L;
  R = L = 0;
  for (int i = 1; i < n; i++){
    if (i > R){
      L = R = i;
      while (R < n && s[R - L] == s[R]) R++;
      z[i] = R - L;
      R--;
    }
    else {
      int k = i - L;
      if (z[k] < R - i + 1) z[i] = z[k];
      else {
        L = i;
        while (R < n && s[R - L] == s[R]) R++;
        z[i] = R - L;
        R--;
      }
    }
  }
}


int main()
{
  string s, t;
  while (cin >> s >> t){
    const string S = s + '$' + t;
    const int sl = s.size();
    int maxi = 0;
    for (int i = 0; i < sl; i++){
      vector<int> z;
      z_algo(S.substr(i), z);
      maxi = max(maxi, *max_element(begin(z) + (sl - i), end(z)));
    }
    cout << maxi << endl;
  }
  return 0;
}

C++: #include<stdio.h>
#include<string.h>
unsigned short d[4000][4000];
char a[4001],b[4001];
int i,j;

int max(int a,int b){
	if(a>=b)return a;
	else return b;
}

int main(){
	while(scanf("%s",a)!=EOF){
		scanf("%s",b);
		int al=strlen(a);
		int bl=strlen(b);
		printf("%d %d\n",al,bl);
		for(i=0;i<al;i++)for(j=0;j<bl;j++)d[i][j]=0;
		for(i=0;i<al;i++)for(j=0;j<bl;j++){
			if(a[i]==b[j]){
				if(i>0&&j>0)d[i][j]=d[i-1][j-1]+1;
				else d[i][j]=1;
			}
			else{
				d[i][j]=0;
			}
		}
		/*for(i=0;i<strlen(a);i++){
			for(j=0;j<strlen(b);j++)printf("%d ",d[i][j]);
			printf("\n");
		}
		printf("\n");*/
		int ans=0;
		for(i=0;i<al;i++)for(j=0;j<bl;j++)ans=max(ans,d[i][j]);
		printf("%d\n",ans);
	}
	return 0;
}