#P9211. 「不死鸟附体」

「不死鸟附体」

Background

To die and be reborn, to live and die again. A phoenix is such a creature, endlessly repeating this cycle through infinite time.

So it is probably best not to gain eternal youth and immortality.

Problem Description

A phoenix’s “life” can be seen as a string S0S_0 of length at most lmaxl_{\max}. After endless cycles, it forms an infinite string Sinf=S0+S0+S0+S_{\mathrm{inf}}=S_0+S_0+S_0+\cdots. Now take the first ll characters of SinfS_{\mathrm{inf}} as the phoenix’s life within the observable time, denoted by SfinS_{\mathrm{fin}}.

However, the so-called cycle is not a rigid mechanical repetition. Therefore, in SfinS_\mathrm{fin}, no more than nn characters are changed into other characters, becoming SrealS_{\mathrm{real}}.

Now we have observed SrealS_{\mathrm{real}}, and we want to find the period S0S_0 of this cycle. But since the phoenix’s cycle is far too long, we only want to find a string S0S_0' such that the SfinS_\mathrm{fin}' generated from it can be turned into SrealS_{\mathrm{real}} by modifying no more than mm characters.

Input Format

The first line contains four positive integers l,lmax,n,ml,l_{\max},n,m.

The second line describes the observed string SrealS_{\mathrm{real}} of length ll.

Output Format

Output a positive integer l0l_0 on the first line, representing the length of the S0S_0 you found. You must guarantee 1l0lmax1\le l_0\le l_{\max}.

Output a string S0S_0 of length l0l_0 on the second line, representing the string you found.

25 8 5 10
aaacdaabbbaabccaabcdaabcd

5
aaacd

Hint

Sample Explanation

The sample is only for understanding the statement, and does not satisfy the Constraints. For the actual constraints, see “Constraints and Conventions”.

The S0S_0 used to generate SrealS_{\mathrm{real}} is aabcd\verb!aabcd!.

  • It generates $S_{\mathrm{inf}}=\verb!aabcdaabcdaabcdaabcdaabcd!\cdots$;
  • It generates Sfin=aabcdaabcdaabcdaabcdaabcdS_{\mathrm{fin}}=\verb!aabcdaabcdaabcdaabcdaabcd!;
  • It generates $S_{\mathrm{real}\kern{-2.5pt}}=\verb!aaacdaabbbaabccaabcdaabcd!$.

The sample output gives one possible S0=aaacdS_0'=\verb!aaacd!. From this we compute the difference between SfinS_{\mathrm{fin}}' and SrealS_{\mathrm{real}}:

$$\begin{aligned} S_{\mathrm{fin}}'=&\texttt{aaacdaa\textcolor{red}a\textcolor{red}c\textcolor{red}daa\textcolor{red}ac\textcolor{red}daa\textcolor{red}acdaa\textcolor{red}acd}\cr S_{\mathrm{real}}=&\texttt{aaacdaabbbaabccaabcdaabcd}\cr \end{aligned}$$

The difference is 77, not exceeding m=10m=10, so it is acceptable.

Constraints and Conventions

For all testdata, it is guaranteed that l=3×105l=3\times 10^5, n=3×103n=3\times 10^3, m=104m=10^4, and 1lmax1051\le l_{\max} \le 10^5.

Translated by ChatGPT 5