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 Fortran (gfortran v4.8.4) (judge verdict: Wrong Answer)

integer N
character(5000) S

integer,allocatable,dimension(:)::seen1,seen2,seen3,seen4
integer mod1,mod2,mod3
integer base1,base2,base3,base4
integer(16) rh1,rh2,rh3,rh4
integer(16),allocatable,dimension(:)::pow1,pow2,pow3,pow4

integer ok,ng,md
logical mdcheck

integer i

read*,N
read*,S
mod1=30203
mod2=30211
mod3=6871
allocate(seen1(0:mod1-1),seen2(0:mod2-1),seen3(0:mod3-1),seen4(0:mod3-1))
base1=353
base2=359
base3=139
base4=11
allocate(pow1(0:N),pow2(0:N),pow3(0:N),pow4(0:N))
pow1(0)=1;pow2(0)=1;pow3(0)=1;pow4(0)=1
do i=1,N
  pow1(i)=mod(pow1(i-1)*base1,mod1)
  pow2(i)=mod(pow2(i-1)*base2,mod2)
  pow3(i)=mod(pow3(i-1)*base3,mod3)
  pow4(i)=mod(pow4(i-1)*base4,mod3)
end do

ok=0;ng=N/2+1
md=(ok+ng)/2
do while(ng-ok>1)
  mdcheck=.false.
  
  seen1=-1;seen2=-1;seen3=-1;seen4=-1
  rh1=0;rh2=0;rh3=0;rh4=0
  do i=1,md
    rh1=mod(rh1+ichar(S(i:i))*pow1(md-i),mod1)
    rh2=mod(rh2+ichar(S(i:i))*pow2(md-i),mod2)
    rh3=mod(rh3+ichar(S(i:i))*pow3(md-i),mod3)
    rh4=mod(rh4+ichar(S(i:i))*pow4(md-i),mod3)
  end do
  seen1(rh1)=1;seen2(rh2)=1;seen3(rh3)=1;seen4(rh4)=1
  do i=2,N-md+1
    rh1=modulo(mod(rh1*base1,mod1)+ichar(S(i+md-1:i+md-1))-modulo(ichar(S(i-1:i-1))*pow1(md),mod1),mod1)
    rh2=modulo(mod(rh2*base2,mod2)+ichar(S(i+md-1:i+md-1))-modulo(ichar(S(i-1:i-1))*pow2(md),mod2),mod2)
    rh3=modulo(mod(rh3*base3,mod3)+ichar(S(i+md-1:i+md-1))-modulo(ichar(S(i-1:i-1))*pow3(md),mod3),mod3)
	rh4=modulo(mod(rh4*base4,mod3)+ichar(S(i+md-1:i+md-1))-modulo(ichar(S(i-1:i-1))*pow4(md),mod3),mod3)
    if(rh1<0)rh1=rh1+mod1
    if(rh2<0)rh2=rh2+mod2
    if(rh3<0)rh3=rh3+mod3
    if(rh4<0)rh4=rh4+mod3
    if(seen1(rh1)>0 .and. seen2(rh2)>0 .and. seen3(rh3)>0 .and.seen4(rh4)>0)then
      if(i-seen1(rh1)>=md .and. i-seen2(rh2)>=md.and.i-seen3(rh3)>=md.and.i-seen4(rh4)>=md)then
        mdcheck=.true.
        exit
      endif
    else
      if(seen1(rh1)<0)seen1(rh1)=i
      if(seen2(rh2)<0)seen2(rh2)=i
      if(seen3(rh3)<0)seen3(rh3)=i
      if(seen4(rh4)<0)seen4(rh4)=i
    endif
    
  end do
  if(mdcheck)then
    ok=md
  else
    ng=md
  endif
  md=(ok+ng)/2
end do

print"(I0)",ok
end