luogu#P10271. 漫长悄悄话

漫长悄悄话

Background

Which page does the story begin from?

Small stones, red earth, distant fire.

A groping outline, in the night sky, people who rush into the flames.

Hands clasping hands, hearts touching hearts.

Surging deep in the throat, my warm fire.

Problem Description

Some preliminary definitions:

  • Rev(S):\text{Rev}(S): the string formed by concatenating SS,SS1,,S1S_{|S|}, S_{|S|-1}, \dots, S_1 in order. That is, reverse SS.

  • lcp(i,j):\text{lcp}(i,j): the string that is the longest common prefix of the suffix starting at position ii and the suffix starting at position jj.

  • lcs(i,j):\text{lcs}(i,j): the string that is the longest common suffix of the prefix ending at position ii and the prefix ending at position jj.

  • LCP(S,T):\text{LCP}(S,T): the longest common prefix of SS and TT.


Given a string SS of length nn, compute:

$$\max\limits_{1 \le i < j \le n}\{\text{LCP}(\text{Rev}(\text{lcs}(i,j)),\text{lcp}(i,j))\}$$

Input Format

The first line contains an integer nn.

The second line contains a string SS of length nn.

Output Format

Output a single number, which is the answer.

9
abbbabbba
3

Hint

Explanation of Sample 1

lcp(3,7):\text{lcp}(3,7): bba.

lcs(3,7):\text{lcs}(3,7): abb.

$\text{LCP}(\text{Rev}(\texttt{\color{blue}abb}),\texttt{\color{blue}bba}):$ bba.

It can be proven that there is no better choice than i=3,j=7i=3, j=7.

Constraints

For 30%30\% of the testdata, 1n2×1031 \le n \le 2\times 10^3.

For 60%60\% of the testdata, 1n1051 \le n \le 10^5.

For the other 10%10\% of the testdata, 1in2,SiSi+2\forall 1 \le i \le n-2, S_{i}\not=S_{i+2}.

For 100%100\% of the testdata, 1n1061 \le n \le 10^6, and the input consists only of integers and lowercase letters.

Translated by ChatGPT 5