#P9922. [POI 2023/2024 R1] CzatBBB

[POI 2023/2024 R1] CzatBBB

Background

Translated from XXXI Olimpiada Informatyczna - Stage I CzatBBB

Problem Description

You are given a string SS of length nn and a parameter kk. Let RR be the substring formed by the last kk letters of SS.

Suppose SS' is the new string obtained by appending one new letter to SS.

The appending rule is as follows. For each letter XX, count how many times it appears in SS immediately after an occurrence of RR. The letter with the highest frequency is chosen as the new appended letter. If multiple letters have the highest frequency, choose the smallest one. If RR does not occur anywhere else in SS, then take X=aX = a. Finally, we extend SS by appending the letter XX to its end.

For example, let S=abaaabababaS = \text{abaaabababa} and k=3k = 3. Then the substrings where RR appears together with the following letter are: abaa\text{abaa}, abab\text{abab}, abab\text{abab}. It most often appears with the letter b\text{b}, so we append b\text{b} to SS, obtaining S=abaaababababS' = \text{abaaabababab}.

Now S=abaaababababS' = \text{abaaabababab} and R=babR = \text{bab}. The substrings where RR appears together with the following letter are: baba\text{baba}, baba\text{baba}. Therefore, we append aa to the end of SS'.

And so on. This operation will be performed infinitely many times.

Your task is to write a program that outputs the characters from position aa to position bb of the extended string.

Input Format

The first line contains four integers nn, kk, aa, and bb.

The second line contains a string of length nn, consisting of lowercase English letters, representing the word SS.

Output Format

Output the characters from the aa-th to the bb-th position of the string SS', i.e. the letters located at these positions in the extended word SS'.

11 3 12 13
abaaabababa

ba

20 3 30 40
abcdabcdabcdabcdabcd

bcdabcdabcd

见附件
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

见附件
见附件

Hint

For all testdata, 2n1062\leq n\leq10^6, 1k<n<a<b<10181\leq k<n<a<b<10^{18}, b+1a106b+1-a\leq10^6, and the string contains only lowercase letters.

Subtask ID Additional Constraints Score
1 n100n\leq100, b1000b\leq1000 8
2 b108b\leq 10^8 10
3 n500n\leq 500, a previous occurrence of the suffix RR will always exist, and the same letter will always follow each occurrence 16
4 a previous occurrence of the suffix RR will always exist, and the same letter will always follow each occurrence 10
5 k20k\leq20, b1010b\leq 10^{10}, the string contains only ab 16
6 No additional constraints 40

Translated by ChatGPT 5