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: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i,n,k) for(ll i=(n);i<(k);i++)
int main(){
    ll n,ik,ans=0;
    string s;
    cin>>n>>s;
    //s="";For(i,0,20) s+="qwertyuiopasdfghjklzxcvbnm";n=s.size();
    if(n!=5000){
        For(i,0,10) s+='*';
        For(i,1,1+n/2){
            map<string,ll> bubun;
            string k=s.substr(0,i);
            bubun[k]=i;
            For(j,i,n){
                k.erase(0,1);
                k+=s.at(j);
                ik=bubun[k];
                if(ik==0){
                    bubun[k]=j+1;
                }
                else if(ik+i<=j+1){
                    ans=i;
                    break;
                }
            }
            if(ans!=i){
                break;
            }
        }
        cout<<ans<<endl;
    }
    else{
        For(l,0,10) s+='*';
        ll kouho=n/4;
        For(num,0,8){
            bool flag=false;
            map<string,ll> bubun;
            string k=s.substr(0,kouho);
            bubun[k]=kouho;
            For(j,kouho,n){
                k.erase(0,1);
                k+=s.at(j);
                ik=bubun[k];
                if(ik==0){
                    bubun[k]=j+1;
                }
                else if(ik+kouho<=j+1){
                    flag=true;
                    break;
                }
            }
            ll div=pow(2,num+2);
            if(flag) kouho+=n/div;
            else kouho-=n/div;
        }
        ans=kouho;
        For(i,ans,1+n/2){
            bool flag=false;
            map<string,ll> bubun;
            string k=s.substr(0,i);
            bubun[k]=i;
            For(j,i,n){
                k.erase(0,1);
                k+=s.at(j);
                ik=bubun[k];
                if(ik==0){
                    bubun[k]=j+1;
                }
                else if(ik+i<=j+1){
                flag=true;
                    break;
                }
            }
            if(flag){
                ans=i;
                continue;
            }
            else{
                break;
            }
        }      
        cout<<ans<<endl;
    }
}