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<stdio.h>
#include<string.h>

char A[4002],B[4002];

//BのN番目の文字がA中の複数の文字と同じになることがある
int seach(int atop,int lenA,int lenB){
	int i,j,count,max = 0;
	int acor;
	for(i = 0;i < lenB && max < lenA-atop;){
		if(B[i] != A[atop])
			continue;
		for(j = i;j < lenB && max < lenA-atop;){
			acor = 0;
			count = 0;
			for(;j < lenB && count < lenA-atop;j++){
				if(A[acor+atop] == B[j]){
					count++;
					acor++;
				}
				else{
					if(j == 0)
						j++;
					break;
				}
			}
			if(max < count)
				max = count;
		}
		return max;
	}
}
int main(){
	int i,j,lenA,lenB,kyotu;
	int max[1000] = {0},mcor = 0;
	while(1){
		if(scanf("%s%s",A,B) == -1){
			break;
		}
		lenA = strlen(A);
		lenB = strlen(B);
		for(i = 0;i < lenA;i++){
			kyotu = seach(i,lenA,lenB);
			printf("kyotu = %d\n",kyotu);
			if(max[mcor] < kyotu)
				max[mcor] = kyotu;
		}
		mcor++;
	}
	for(i = 0;i < mcor;i++){
		printf("%d\n",max[i]);
	}
	return 0;
}

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

int main(){
	string str1, str2;
	while(cin >> str1 >> str2){
		int count = 0;
		for(int i = 0; i < str1.size(); i++){
			for(int j = 0; j < str2.size(); j++){
				int k = 0;
				while(str1[i+k] == str2[j+k] && i+k <= str1.size()-1 && j+k <= str2.size()-1){
					//if(i + k > str1.size()-1 || j + k > str2.size()-1) break;
					k++;
					count = max(count, k);
					//cout << str2.substr(i, k) << endl;
				}
			}
		}
		cout << count << endl;
	}
}

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

using namespace std;
int len[4001][4001];

int main(void){
	int a,b;
	while(1){
		memset(len,0,sizeof(len));
		string str;
		string str2;
		cin >> str;
		cin >> str2;
		for(a=0;a<str.size();a++){
			for(b=0;b<str2.size();b++){
				if(str[a]==str2[b]){
					len[a+1][b+1]=len[a][b]+1;
				}else{
					len[a+1][b+1]=max(len[a+1][b],len[a][b+1]);
				}
			}
		}
		cout << len[a][b] << endl;
	}
	return 0;
}

C++: //AOJ 0528
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

const int NMAX = 20000;

int n, k;
int Rank[NMAX + 1];
int tmp[NMAX + 1];

bool compare_sa(int i, int j) {
  if (Rank[i] != Rank[j]) return Rank[i] < Rank[j];
  else {
    int ri = i + k <= n ? Rank[i + k] : -1;
    int rj = j + k <= n ? Rank[j + k] : -1;
    return ri < rj;
  }
}

void construct_sa(string S, int *sa) {
  n = S.length();

  for (int i = 0; i <= n; i++) {
    sa[i] = i;
    Rank[i] = i < n ? S[i] : -1;
  }

  for (k = 1; k <= n; k *= 2) {
    sort(sa, sa + n + 1, compare_sa);

    tmp[sa[0]] = 0;
    for (int i = 1; i <= n; i++) {
      tmp[sa[i]] = tmp[sa[i - 1]] + 
        (compare_sa(sa[i - 1], sa[i]) ? 1 : 0);
    }

    for (int i = 0; i <= n; i++) {
      Rank[i] = tmp[i];
    }
  }
}

void construct_lcp(string S, int *sa, int *lcp) {
  int n = S.length();
  for (int i = 0; i <= n; i++) Rank[sa[i]] = i;

  int h = 0;
  lcp[0] = 0;
  for (int i = 0; i < n; i++) {
    int j = sa[Rank[i] - 1];

    if (h > 0) h--;
    for (; j + h < n and i + h < n; h++) {
      if (S[j + h] != S[i + h]) break;
    }

    lcp[Rank[i] - 1] = h;
  }
}

string S, T;

const int LMAX = 20000;
int sa[LMAX], lcp[LMAX];

int solve() {
  int sl = S.length();
  S += '_' + T;

  construct_sa(S, sa);
  construct_lcp(S, sa, lcp);

  int ans = 0;
  for (int i = 0; i < S.length(); i++) {
    if ((sa[i] < sl) != (sa[i + 1] < sl)) {
      ans = max(ans,lcp[i]);
    }
  }
  return ans;
}

int main() {
  while (cin >> S >> T) {
    cout << solve() << endl;
  }
}

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

signed main() {
	string s, t;
	while(cin >> s >> t){
	ll n = s.size(), m = t.size(), ans = 0;
	rep(h, n) {
		ll cnt = 0;
		rep(i, n + m) {
			if (h + i == n || i == m) { break; }
			if (s[h + i] == t[i]) { cnt++; }
			else { ans = max(ans, cnt); cnt = 0; }
		}
		ans = max(ans, cnt);
	}
	rep(h, m) {
		ll cnt = 0;
		rep(i, n + m) {
			if (i == n || h + i == m) { break; }
			if (s[i] == t[h + i]) { cnt++; }
			else { ans = max(ans, cnt); cnt = 0; }
		}
		ans = max(ans, cnt);
	}
	cout << ans << endl;
    }
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <iterator>
#include <queue>
#include <stack>
#include <deque>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <string>
#include <cmath>
#include <complex>
#include <functional>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <cstdlib>
#include <cctype>

#define REP(i,m,n) for(int i=m;i<(int)n;i++)
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define rrep(i,n) for(int i = 1; i <= n; i++)
#define drep(i,n) for(int i = n; i >= 0; i--)
#define rp(i,c) rep(i,(int)c.size())
#define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)
#define mp make_pair
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define rall(u) (u).rbegin(),(u).rend()
#define dbg(x) cerr<<#x<<" = "<<(x)<<endl
#define v_delete(a,b) (a).erase(remove((a).begin(), (a).end(), b), (a).end())
#define v_unique(a) (a).erase(unique((a).begin(), (a).end()), (a).end())
#define VV(T) vector<vector< T > >
#define init(a,b) memset((a), (b), sizeof((a)))
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
const int inf=1<<28;
const double INF=1e10,EPS=1e-9;


template<class T>int SIZE(T a){return a.size();}
template<class T>string IntToString(T num){string res;stringstream ss;ss<<num;return ss.str();}
template<class T>T StringToInt(string str){T res=0;for(int i=0;i<SIZE(str);i++)res=(res*10+str[i]-'0');return res;}
template<class T>T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template<class T>T lcm(T a,T b){return a/gcd(a,b)*b;}

template <class T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }
template <class T> void input(T* a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; }


int dp[4002][4002]={0};
int main(){
	string a,b;
	while(cin >> a >> b){
	int ans=0;
	for(int i=0;i<a.size()-1;i++){
		for(int j=0;j<b.size()-1;j++){
			if(a[i+1]==b[j+1]){
				dp[i+1][j+1]=dp[i][j]+1;
			}else{
				dp[i+1][j+1]=0;
			}/*
			if(dp[i+1][j+1]){
				cout << dp[i+1][j+1]<<endl;
				cout << i << "," << j << endl;
			}*/
			ans=max(ans,dp[i+1][j+1]);
		}
	}
	cout << ans << endl;
	}
	return 0;
}