The Best Greeting
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Zenyk prepared a surprise for Marichka. He made a wonderful postcard with his own hands and decided to write the best greeting. And at this stage, Zenyk’s imagination ran out. So he found a greeting on the Internet and wrote it down.
Then Zenyk looked at his gift and realized that this would not impress Marichka. Therefore, he decided to cut out several letters and paste these same letters back in their place, but in a different order. However, so that Marichka does not notice this, Zenyk can cut out at most \(k\) letters.
And Zenyk cannot just paste the letters back in any way. For some reason, he decided that Marichka would really like the greeting if it is lexicographically minimal among all possible greetings he can obtain. Help him find this greeting.
A string \(s\) is considered lexicographically smaller than a string \(t\) if there exists such an index \(i\) that \(s_1 = t_1\), \(s_2 = t_2\), …, \(s_{i-1} = t_{i-1}\) and \(s_i < t_i\), or \(s\) is a prefix of \(t\).
Input
The first line contains two integers \(n\) and \(k\) — the number of letters in Zenyk’s greeting and the number of letters Zenyk can choose, respectively.
The next line contains Zenyk’s greeting, which he downloaded from the Internet. The greeting consists only of lowercase Latin letters.
Output
In a single line, print the lexicographically minimal greeting that Zenyk can obtain.
Constraints
\(1 \le k \le n \le 10^5\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 7 3 acbdaxa | aabcaxd |
Notes
In the message acbdaxa, Zenyk can choose the letters at
indices 2, 4, and 7. By cutting them out and pasting them back in a
different order, he can obtain aabcaxd. This will be the
lexicographically smallest greeting.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
The Best Greeting - Editorial
Denote the input string by \(s\). Let \(t\) be the lexicographically smallest string that can be obtained by rearranging the letters of \(s\), that is, \(s\) with its elements sorted in non-decreasing order.
The string \(t\) is the best possible answer, since it is the lexicographically smallest string obtainable by rearranging the letters of the input string. However, getting \(t\) may require cutting out more than \(k\) letters.
Let’s see how to find the smallest string taking into account the limit on the number of positions that can be cut out. We go from left to right. If \(s_0 = t_0\), the first character cannot be improved. Otherwise, if \(s_0 \neq t_0\), we can find the letter \(t_0\) in \(s\), cut it out together with \(s_0\) and swap them. So by cutting out two characters we got a better string. We continue the same reasoning with the next letters in order.
In general, consider the first \(x\) letters. Let’s count how many letters need to be cut out for the prefix of length \(x\) of \(s\) to match the prefix of \(t\). Every position is of one of two types: either \(s_i = t_i\) or \(s_i \neq t_i\). In the first case nothing needs to be cut out. However, all positions where the second condition holds must be cut out, since other characters must be put there. We cut out all such positions; let there be \(m\) of them. Now for each of them we need to find the letter \(t_i\) in \(s\), cut it out and put it in place of \(s_i\), which is \(m \cdot 2\) characters in total. However, we can do better. If for some position \(i\) there is \(j < x\) with \(s_j \neq t_j\) and \(t_i = s_j\), we can put the letter \(s_j\), which we cut out anyway, at position \(i\). This saves one cut.
In general, to count how many letters need to be cut out for the prefix of length \(x\), for each character \(c\) we compute two values: \(A_c\), the number of \(i < x\) with \(s_i \neq t_i\) and \(s_i = c\), and \(B_c\), the number of \(i < x\) with \(s_i \neq t_i\) and \(t_i = c\). \(A_c\) is the number of characters \(c\) we cut out, and \(B_c\) is the number of positions where the character \(c\) must be put. Clearly, \(min(A_c, B_c)\) characters can be put in place right away, and \(B_c - min(A_c, B_c)\) must be cut out additionally. So the total number of characters is \(m + \sum{B_c - min(A_c, B_c)}\).
For lexicographic minimality, we need to choose the largest \(x\) such that the required number of cut characters does not exceed \(k\). This can be done with binary search.
To find the answer, we find the optimal \(x\). The first \(x\) characters of the answer match the string \(t\). Then we need to find which characters must be cut out additionally and replace them with the surplus characters cut from the prefix. For lexicographic minimality, they should be cut as far to the right as possible and inserted in lexicographic order.
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|