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 <iomanip>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <cstring>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<int, PII> PPII;
typedef pair<int, PPII> PPPII;

class Union_find{
private:
	vector<int> par;
	vector<int> rank;
	int n;

public:
	Union_find(int a){
		n = a;
		for(int i = 0;i < n;i++){
			par.push_back(i);
			rank.push_back(0);
		}
	}

	int find(int x){
		if(par[x] == x){
			return x;
		}else{
			return par[x] = find(par[x]);
		}
	}

	void unite(int x, int y){
		x = find(x);
		y = find(y);
		if(x == y) return;

		if(rank[x] < rank[y]){
			par[x] = y;
		}else{
			par[y] = x;
			if(rank[x] == rank[y]) rank[x]++;
		}
	}

	bool same(int x, int y){
		return find(x) == find(y);
	}
};

int main(void){
	string s, t;
	while(cin >> s && s.size()){
		int res = 0;
		cin >> t;
		REP(i, s.size()){
			REP(j, t.size()){
				int cnt = 0;
				if(s[i] == t[j]){
					cnt++;
					while(s[i+cnt] == t[j+cnt] && i+cnt < s.size() && j+cnt < t.size()) cnt++;
					res = max(res, cnt);
				}
			}
		}
		REP(i, t.size()){
			REP(j, s.size()){
				int cnt = 0;
				if(s[j] == t[i]){
					cnt++;
					while(s[j+cnt] == t[i+cnt] && j+cnt < s.size() && i+cnt < t.size()) cnt++;
					res = max(res, cnt);
				}
			}
		}
		cout << res << endl;
	}
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x);

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

char str1[4010];
char str2[4010];

int main(){
  while(~scanf("%s %s",str1,str2)){
    int idx1 = 0, idx2 = 1;
    int ans = 0;
    while(str1[idx1] != '\0'){
      char tmp = str1[idx2];
      str1[idx2] = '\0';
      bool ok = (NULL != strstr(str2, &str1[idx1]));
      str1[idx2] = tmp;

      if(ok){
        ans = max(ans, idx2 - idx1);
        if(str1[idx2] != '\0')
          idx2++;
        else
          idx1++;
      }else{
        idx1++;
        if(idx1 == idx2) idx2++;
      }
    }
    print(ans);
  }
  return 0;
}

C++: 
//=====================================================
// 
// (setq backup-inhibited t)
// (setq auto-save-default nil)
// 
// (column-number-mode t)
// (setq-default tab-width 2 indent-tabs-mode nil)
// 
// (global-set-key "\C-h" 'delete-backward-char)
// 
// (setq indent-line-function 'indent-relative-maybe)
// (global-set-key "\C-m" 'newline-and-indent)
// 
//=====================================================

#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int main() {
  while(1){
    string s;
    string t;
    cin >> s;
    cin >> t;
    vector<vector<int>> dp(4000, vector<int>(4000));
    if(!cin) break;
    set<string> st;
    int ml = 0;
    for (int i = 0; i < s.size(); ++i) {
      for (int j = 0; j < t.size(); ++j) {
        if (i > 0 && j > 0 && s[i] == t[j])
          dp[i][j] = dp[i-1][j-1] + 1;
        else if(s[i] == t[j])
          dp[i][j] = 1;
        else
          dp[i][j] = 0;
        ml = max(ml, dp[i][j]);
      }
    }
    cout << ml << endl;
  }
  return 0;
}

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

main() {
  while(1) {
    char str[2][4001];
    int state;
    state = scanf("%s\n", str[0]);
    if(state == EOF) break;
    scanf("%s\n", str[1]);

    int data[strlen(str[0])+1][strlen(str[1])+1];
    int maxL = 0;
    for(int i = 0; i <= strlen(str[0]); i++) {data[i][0] = 0;}
    for(int j = 0; j <= strlen(str[1]); j++) {data[0][j] = 0;}

    for(int i = 1; i <= strlen(str[0]); i++) {
      for(int j = 1; j <= strlen(str[1]); j++) {
	if(str[0][i-1] == str[1][j-1]) data[i][j] = data[i-1][j-1]+1;
	else     data[i][j] = 0;

	if(data[i][j] > maxL) maxL = data[i][j];
      }
    }
    cout << maxL << endl;
  }
}

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

int main()
{
        char r[2][4002] = {};
        while (gets (r[0])) {
                gets (r[1]);
                int d[2][4002] = {}, le, ri, a = 0;
                le = strlen (r[0]), ri = strlen (r[1]);
                for (int i=1; i<=le; i++) {
                        for (int j=1; j<=ri; j++) {
                                if (r[0][i-1] == r[1][j-1]) d[i&1][j] = d[1-(i&1)][j-1] + 1;
                                else d[i&1][j] = 0;
                                a = a>d[i&1][j] ? a:d[i&1][j];
                        }
                }
                printf ("%d\n", a);
        }
        return 0;
}

C++: #include <iostream>
using namespace std;
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional> //sort(A,A.end(),greater<int>());???????????????
#include <queue>
#include <stack>
#include <vector>

#define INF 10 << 10

string str1;

string Find(string str) {
	if (str1.find(str) != -1) {
		return str;
	}
	if (str.size() == 1) { return ""; }
	int maxi = 0; string ans;
	string s = str; s.erase(s.begin());
	string ss = Find(s);
	maxi = ss.size(); ans = ss;
	s = str; s.erase(s.begin() + s.size() - 1);
	ss = Find(s);
	if(maxi < ss.size()){
		maxi = ss.size();
		ans = ss;
	}
	return ans;
}

int main() {
	//string str1;
	string str2;

	while (cin >> str1) {
		cin >> str2;
		//?????????????????? str1 ???
		if (str1.size() < str2.size()) {
			swap(str1, str2);
		}
		int ans = Find(str2).size();
		cout << ans << endl;
	}

	return 0;
}

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

#define MAX 4000

using namespace std;

int dp[MAX + 2][MAX + 2];

int main(void){

  string s,t;
  while(getline(cin, s)){
    getline(cin, t);

    fill(dp[0], dp[MAX + 1] + MAX + 2, 0);

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