Problem:
Score : 500 points

Problem Statement

Given is a string S of length N.

Find the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping.

More formally, find the maximum positive integer len such that there exist integers l_1 and l_2 ( 1 \leq l_1, l_2 \leq N - len + 1 ) that satisfy the following:

l_1 + len \leq l_2

S[l_1+i] = S[l_2+i] (i = 0, 1, ..., len - 1)

If there is no such integer len, print 0.

Constraints

2 \leq N \leq 5 \times 10^3

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. If there is no such non-empty string, print 0 instead.

Sample Input 1

5
ababa

Sample Output 1

2

The strings satisfying the conditions are: a, b, ab, and ba. The maximum length among them is 2, which is the answer.
Note that aba occurs twice in S as contiguous substrings, but there is no pair of integers l_1 and l_2 mentioned in the statement such that l_1 + len \leq l_2.

Sample Input 2

2
xy

Sample Output 2

0

No non-empty string satisfies the conditions.

Sample Input 3

13
strangeorange

Sample Output 3

5

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <iostream>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <assert.h>
#include <deque>
using namespace std;

typedef unsigned long long UL;
typedef long long LL;
#define LP(i, a, b) for (int i = int(a); i < int(b); i++)
#define LPE(i, a, b) for (int i = int(a); i <= int(b); i++)
typedef pair<int, int> PII;
typedef vector<vector<PII> > WAL;
typedef vector<vector<int> > SAL;
#define Ep 1e-13

#define INF 1e9
const int MaxSize = 1050 + 10;
//const LL MOD = 1e9 + 7;
const LL MOD = 99999;
const int AS = 26;

string s;

/*
 https://atcoder.jp/contests/abc142/tasks/abc142_e
 E - Get Everything
 */
int N;

LL const BASE = 256;

//don't forget to lower case if you are doing string matching
LL sHash(string s) {
	LL h = 0;
	LP(i, 0, s.size())
	{
		h = (h * BASE) + (LL) s[i] - 'a'; //very similar idea to base number calculation
		h %= MOD;
	}

	return h;
}

bool good(int winL) {

	LL hash = 0;
	LL topBase = 1;
	SAL list(MOD);
	LP(i, 0, winL)
	{
		if (i > 0) {
			topBase *= BASE;
			topBase %= MOD;
		}

		hash *= BASE;
		hash %= MOD;
		hash += (LL) (s[i] - 'a');
		hash %= MOD;
	}
	list.at(hash).push_back(0);

	LP(i, winL, N)
	{
		//slide the hash
		hash -= topBase * (LL) (s[i - winL] - 'a');
		hash %= MOD; //watch out for the negative case!
		hash += MOD;
		hash %= MOD;

		hash *= BASE;
		hash %= MOD;
		hash += (LL) (s[i] - 'a');
		hash %= MOD;
		list.at(hash).push_back(i - winL + 1);
	}

	LP(slot, 0, MOD)
	{
		int ls = list.at(slot).size();
		if (ls < 2)
			continue;

		LP(i, 0, ls)
		{
			LP(j, i+1, ls)
			{
				int asi = list.at(slot)[i];
				int bsi = list.at(slot)[j];
				//cout << asi << " " << bsi << " " << winL << endl;
				if ((bsi - asi) < winL)
					continue;

				bool match = true;
				LP (k, 0, winL)
				{
					if (s[asi + k] != s[bsi + k]) {
						match = false;
						break;
					}
				}

				if (match) {
					return true;
				}
			}
		}
	}

	return false;
}

int main() {

	ios_base::sync_with_stdio(false);
	//freopen("/Users/georgeli/pycp/A_1.in", "r", stdin);
	cin >> N >> s;
	//check 1 case

	int low = 0;
	int high = N;

	while (low < high) {
		int mid = (low + high) / 2;
		if (good(mid)) {
			if (mid == low) {
				if (good(mid + 1)) {
					low = mid + 1;
				} else {
					low = mid;
				}
				break;
			} else {
				low = mid;
			}
		} else {
			high = mid - 1;
		}
	}

	cout << low;

	return 0;
}