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 <cstdio>
#include <cstring>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;
const int maxn=2e4+10;
const int N=0x3f3f3f3f;
 
int ary[maxn],rnk[maxn],hgt[maxn],t1[maxn],t2[maxn],sum[maxn],sa[maxn];
 
void getsa(int *r,int *sa,int n,int m)
{
    int *x,*y,*t;
    int i,j,p;
    x=t1,y=t2;
    for(i=0;i<m;i++) sum[i]=0;
    for(i=0;i<n;i++) sum[x[i]=r[i]]++;
    for(i=1;i<m;i++) sum[i]+=sum[i-1];
    for(i=n-1;i>=0;i--) sa[--sum[x[i]]]=i;
    for(p=1,j=1;p<n;j*=2)
    {
        for(p=0,i=n-j;i<n;i++) y[p++]=i;
        for(i=0;i<n;i++) if(sa[i]>=j) y[p++]=sa[i]-j;
        for(i=0;i<m;i++) sum[i]=0;
        for(i=0;i<n;i++) sum[x[y[i]]]++;
        for(i=1;i<m;i++) sum[i]+=sum[i-1];
        for(i=n-1;i>=0;i--) sa[--sum[x[y[i]]]]=y[i];
        t=x,x=y,y=t;
        x[sa[0]]=0;
        for(p=1,i=1;i<n;i++) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+j]==y[sa[i]+j]?p-1:p++;
        m=p;
    }
    return;
}
 
void getht(int *r,int n)
{
    int i,j,k=0;
    for(i=1;i<=n;i++) rnk[sa[i]]=i;
    for(i=0;i<n;i++)
    {
        if(k) k--;
        else k=0;
        j=sa[rnk[i]-1];
        while(r[i+k]==r[j+k]) k++;
        hgt[rnk[i]]=k;
    }
    return;
}
 
bool judge(int k,int n)
{
    int maxx,minn,i;
    maxx=-N,minn=N;
    for(i=2;i<=n;i++)
    {
        if(hgt[i]>=k)
        {
            maxx=max(maxx,max(sa[i-1],sa[i]));
            minn=min(minn,min(sa[i-1],sa[i]));
            if(maxx-minn>k) return true;
        }
        else maxx=-N,minn=N;
    }
    return false;
}
string ss; 
int main()
{
    int t,n,i,l,r,m,ans;
    while(scanf("%d",&n)!=EOF)
    {
        // if(n==0) break;
        cin >> ss;
        for(i=0;i<n;i++) ary[i] = ss[i] - 'a';
        n--;
        for(i=0;i<n;i++) ary[i]=ary[i+1]-ary[i]+88;
        ary[n]=0;
        getsa(ary,sa,n+1,256);
        getht(ary,n);
        l=0,r=n/2,ans=0;
        while(l<=r)
        {
            m=(l+r)/2;
            if(judge(m,n)) l=m+1,ans=m;
            else r=m-1;
        }
        if (ans <= 0) cout << 0 << endl;
        else cout << ans + 1 << endl;
    }
    return 0;
}