hackercup / 2016 /finals /rainbow_string.html
wjomlex's picture
2016 Problems
07f695f verified
raw
history blame
3.68 kB
<p>
You have a string <strong>S</strong> consisting of <strong>N</strong> uppercase letters. This is no ordinary string, however &mdash; it's a rainbow string!
Every letter has a colour, one of red, green, or blue (it might be ambitious to call this a rainbow, but close enough).
The colour of the <strong>i</strong>th letter in <strong>S</strong> is indicated by the <strong>i</strong>th letter in a secondary string <strong>C</strong>
(which also consists of <strong>N</strong> uppercase letters), with the three possible values "R", "G", and "B" representing the colors red, green, and blue respectively.
</p>
<p>
You'd like to answer <strong>Q</strong> questions about your rainbow string. The <strong>i</strong>th question asks:
</p>
<p>
"What's the {<strong>K<sub>i</sub></strong>}th lexicographically smallest substring of <strong>S</strong> which has length <strong>L<sub>i</sub></strong>,
includes at least one green letter, and includes no red letters?"
</p>
<p>
Note that, when considering the list of valid substrings of which to determine the {<strong>K<sub>i</sub></strong>}th lexicographically smallest one,
substrings which are equal to one another but occur at different positions in <strong>S</strong> are distinct!
Additionally, <strong>K<sub>i</sub></strong> is guaranteed to be no larger than the number of such valid substrings.
</p>
<p>
For example, consider <strong>S</strong> = "ABABAB", <strong>C</strong> = "GGBGRG", and <strong>L<sub>i</sub></strong> = 2.
The lexicographically-sorted list of valid substrings of <strong>S</strong> (those which have length 2, include at least one green letter, and include no red letters) is as follows:
</p>
<ol>
<li> AB (starting at index 1) </li>
<li> AB (starting at index 3) </li>
<li> BA (starting at index 2) </li>
</ol>
<p>
Therefore, if <strong>K<sub>i</sub></strong> = 2, the answer would be "AB". <strong>K<sub>i</sub></strong> can be no larger than 3 in this example.
</p>
<p>
To reduce the size of the output, you should simply output 26 integers, with the <strong>i</strong>th of them being the total number of times that the
<strong>i</strong>th letter of the alphabet appears throughout the answers to the <strong>Q</strong> questions.
</p>
<h3>Input</h3>
<p>
Input begins with an integer <strong>T</strong>, the number of rainbow strings you own.
For each rainbow string, there is first a line containing the space-separated integers <strong>N</strong> and <strong>Q</strong>.
The second line contains the length-<strong>N</strong> string <strong>S</strong> denoting the alphabetic characters in the rainbow string.
The third line contains the length-<strong>N</strong> string <strong>C</strong> denoting the colours of each letter of the rainbow string, as described above.
Then, <strong>Q</strong> more lines follow, the <strong>i</strong>th of which contains the space-separated integers
<strong>L<sub>i</sub></strong> and <strong>K<sub>i</sub></strong>.
</p>
<h3>Output</h3>
<p>
For the <strong>i</strong>th rainbow string, print a line containing "Case #<strong>i</strong>: " followed by
26 space-separated integers denoting the frequency of each letter amongst the answers to all of the questions, as described above.
</p>
<h3>Constraints</h3>
<p>
1 &le; <strong>T</strong> &le; 55 <br />
1 &le; <strong>N</strong> &le; 200,000 <br />
1 &le; <strong>Q</strong> &le; 400,000 <br />
1 &le; <strong>L<sub>i</sub></strong>, <strong>K<sub>i</sub></strong> &le; <strong>N</strong> <br />
</p>
<h3>Explanation of Sample</h3>
<p>
For the first string, the answers to the three questions are "AB", "AB", and "BA" respectively. "A" and "B" each show up 3 times in these answers.
</p>