#P3546. [POI 2012] PRE-Prefixuffix

    ID: 2599 远端评测题 1000ms 64MiB 尝试: 1 已通过: 1 难度: 8 上传者: 标签>2012POI哈希,HASH前缀和线性递推,递推式

[POI 2012] PRE-Prefixuffix

Description

We consider strings consisting of lowercase letters of the English alphabet in this problem. An initial fragment of a given string is called its prefix. A final (terminal) fragment of a given string is called its suffix. In particular, the empty string is both a prefix and a suffix of any string. Two strings are cyclically equivalent if one of them can be obtained from another by moving its certain suffix from the end of the string to its beginning. For example, the strings ababba and abbaab are cyclically equivalent, whereas the strings ababba and ababab are not. In particular, every string is cyclically equivalent to itself.

A string tt consisting of nn letters is given. We are looking for its prefix pp and suffix ss of equal length such that:

  • pp and ss are cyclically equivalent,
  • the common length of pp and ss does not exceed (i.e., the prefix pp and the suffix ss do not overlap in tt), and
  • the common length of pp and ss is maximized.

Input Format

The first line of the standard input contains a single integer nn (1n1061 \le n \le 10^6) denoting the length of the string tt.

The second line of input contains the string tt itself, consisting of nn lowercase letters of the English alphabet.

Output Format

Your program should print a single integer in the first and only line of the standard output, namely the common length of the prefix pp and the suffix ss that we are looking for.

15
ababbabababbaab
6

Hint

In tests worth 30%30\% of the points the condition n500n \le 500 holds in addition.

In tests worth 50%50\% of the points the condition n5000n \le 5000 holds in addition.