#P17441. 打字
打字
Problem Description
Igallta and Mi'er are playing a game.
First, they agree on two coefficients and related to how interesting the game is, and then they agree on a string . This string contains only characters from the first characters of $\Sigma=\texttt{abcdefghijklmnopqrstuvwxyz\_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789}$.
Luvia brings them a typewriter. Igallta is the one who types, and Mi'er is the one who controls the typewriter.
The typewriter is connected to a monitor. Both Igallta and Mi'er can see the string on the monitor, which represents what Igallta has typed. The typewriter has keys. Among them, keys correspond to the first characters of ; these are called character keys. Pressing a character key appends the corresponding character to the end of . The remaining key is a delete key; pressing it deletes the last character of . If is already empty, nothing happens.
This alone is too boring, so Luvia removes the labels on the character keys and shuffles them randomly. As a result, at the beginning Igallta does not know what character will be appended to when she presses a particular character key, unless she presses that key, in which case she can determine which character that key corresponds to.
However, Igallta could still learn the mapping quickly, so after Igallta presses character keys times, Mi'er will randomly shuffle these character keys again. Then Igallta no longer knows the mapping. Note that Igallta may press the delete key any number of times at any moment, and these do not count toward the number of character key presses.
In particular, when Igallta attempts to press a character key, Mi'er may decide arbitrarily what character that key corresponds to, as long as it does not contradict the information Igallta has obtained before. This is similar to an adaptive interactive judge.
Igallta may end the game at any time and compute her score for this round. However, when the total number of presses of character keys and the delete key reaches , the game will end automatically. If the final typed string is not a subsequence of , then her score is ; otherwise, her score is the length of .
Igallta wants to maximize her score, while Mi'er wants to minimize Igallta's score. Mi'er, as a “time-traveling hound”, and Igallta, as a new-generation artificial intelligence, can both make the optimal choices.
Now Luvia knows , , and , and she wants to know Igallta’s final score when both sides use optimal strategies. (It is easy to prove that under optimal play, Igallta’s final score will not be .)
However, agreeing on a reasonable and interesting game each time is troublesome. To increase the number of rounds, sometimes Igallta and Mi'er also give (where is the length of ) and choose a substring of to play the game. That is, when computing the score, if is not a subsequence of , then Igallta’s score is ; otherwise, Igallta’s score is .
Note that in this problem, strings are indexed starting from .
Note: is called a subsequence of if and only if by deleting some characters of at certain positions (possibly deleting none, or deleting all), and concatenating the remaining characters in the original order, we can obtain . For example, aad, aaead, aadead are all subsequences of aadead, while eda, aaaa, c are not subsequences of aadead. By definition, the empty string is a subsequence of every string.
refers to the string formed by concatenating the -th to the -th characters of in order.
Input Format
The first line contains two positive integers , representing the test point ID and the number of test groups. For the samples, indicates that the sample satisfies the constraints of test point .
Then there are groups of testdata. For each group, the first line contains three positive integers , and a non-negative integer . The second line contains a string of length consisting only of the first characters of , representing the string agreed upon in the initial game. Then follow lines, each containing two positive integers , representing one query.
Output Format
For each test group, output one line of non-negative integers. The -st number is Igallta’s final score in the initial game when both sides play optimally. The -th number () is Igallta’s final score when playing the game on the substring chosen in the -th query, with both sides playing optimally.
20 9
1 1 3 0
aaa
2 1 2 0
ab
3 3 8 0
abcabcab
3 2 5 0
aabcc
3 2 8 1
abcabcab
1 3
2 1 9 0
aabbaabba
4 3 9 0
abcddcbdd
14 10 10 0
ediahgnehc
21 17 11 0
ediauhgnehc
3
1
8
1
5 1
4
4
3
4
Hint
For all data, it is guaranteed that , , . It is guaranteed that , and .
| Test Point ID | Special Property | Time Limit | ||||||
|---|---|---|---|---|---|---|---|---|
| None | ||||||||
| None | ||||||||
| None |
Property : It is guaranteed that .
Property : It is guaranteed that and .
Property : It is guaranteed that the data is randomly generated in the following way.
First, determine and , then randomly generate groups of testdata. The generation of the groups is independent of each other.
In each test group, first determine , then the string is generated uniformly at random among all possible strings, and each query’s is also generated uniformly at random among all possible . The generation of and the generation of each query’s are also independent of each other.
Property : The input file for this test point is provided and can be found in the attachment download area as type*.in.
The maximum size of the input file is about . Please use a fast input method. The provided files include fastread.cpp, which can be used directly.
Please pay attention to constant-factor optimization.
Sample explanation:
For the game represented by the fifth sample group, the following is one possible process:
- Igallta presses the first character key and gets
c. - Igallta presses the delete key once, then presses the second character key and gets
b. Mi'er shuffles the keys again. - Igallta presses the second character key and gets
c. - Igallta presses the third character key and gets
a. Mi'er shuffles the keys again. Now . - Igallta presses the first character key and gets
c. - Igallta presses the third character key and gets
b. Mi'er shuffles the keys again. - Igallta ends the game. Now , and her score is .
For the first query of the fifth sample group, the following is one possible process:
- Igallta presses the first character key and gets
b. - Igallta presses the second character key and gets
a. Mi'er shuffles the keys again. - Now , which is not a subsequence of . If Igallta chooses to end the game now, her score would be . So she presses the delete key once, and now .
- Igallta presses the first character key and gets
a. - Igallta presses the delete key once, and now .
- Igallta presses the third character key and gets
b. Mi'er shuffles the keys again. - Igallta presses the delete key once and ends the game. Now , and her score is .
Judging limits:
Time limit: .
Memory limit: .
Judging mode: optimization enabled.
Idea: 2021CHD & fydj, Solution: fydj & 2021CHD, Code: fydj & 2021CHD, Data: fydj & 2021CHD.
Translated by ChatGPT 5