String and Operations
Contest: Educational Codeforces Round 99 (Rated for Div. 2)
Difficulty rating: 2800
Tags: dp, greedy

You are given a string $$$s$$$ consisting of $$$n$$$ characters. These characters are among the first $$$k$$$ lowercase letters of the Latin alphabet. You have to perform $$$n$$$ operations with the string.

During the $$$i$$$-th operation, you take the character that initially occupied the $$$i$$$-th position, and perform one of the following actions with it:

- swap it with the previous character in the string (if it exists). This operation is represented as L;
- swap it with the next character in the string (if it exists). This operation is represented as R;
- cyclically change it to the previous character in the alphabet (b becomes a, c becomes b, and so on; a becomes the $$$k$$$-th letter of the Latin alphabet). This operation is represented as D;
- cyclically change it to the next character in the alphabet (a becomes b, b becomes c, and so on; the $$$k$$$-th letter of the Latin alphabet becomes a). This operation is represented as U;
- do nothing. This operation is represented as 0.

For example, suppose the initial string is test, $$$k = 20$$$, and the sequence of operations is URLD. Then the string is transformed as follows:

1. the first operation is U, so we change the underlined letter in test to the next one in the first $$$20$$$ Latin letters, which is a. The string is now aest;
2. the second operation is R, so we swap the underlined letter with the next one in the string aest. The string is now aset;
3. the third operation is L, so we swap the underlined letter with the previous one in the string aset (note that this is now the $$$2$$$-nd character of the string, but it was initially the $$$3$$$-rd one, so the $$$3$$$-rd operation is performed to it). The resulting string is saet;
4. the fourth operation is D, so we change the underlined letter in saet to the previous one in the first $$$20$$$ Latin letters, which is s. The string is now saes.

The result of performing the sequence of operations is saes.

Given the string $$$s$$$ and the value of $$$k$$$, find the lexicographically smallest string that can be obtained after applying a sequence of operations to $$$s$$$.

Input format:
The first line contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.

Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 500$$$; $$$2 \le k \le 26$$$).

The second line contains a string $$$s$$$ consisting of $$$n$$$ characters. Each character is one of the $$$k$$$ first letters of the Latin alphabet (in lower case).

Output format:
For each test case, print one line containing the lexicographically smallest string that can be obtained from $$$s$$$ using one sequence of operations.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Example input:
6
4 2
bbab
7 5
cceddda
6 5
ecdaed
7 4
dcdbdaa
8 3
ccabbaca
5 7
eabba

Example output:
aaaa
baccacd
aabdac
aabacad
aaaaaaaa
abadb

Editorial:
The crucial observation that we have to make is that the character that initially occupied the position $$$i$$$ cannot occupy the positions to the left of $$$i - 2$$$: we can shift some character two positions to the left using a combination of operations RL, but we can't go any further. So, the prefix of the first $$$i$$$ characters of the resulting string can only be affected by the prefix of the first $$$i + 2$$$ characters of the initial string.
Let's use the following dynamic programming to solve the problem: let $$$dp_i$$$ be the lexicographically minimum string that we can obtain by applying operations to the first $$$i$$$ characters (that is, $$$dp_i$$$ is the answer to the problem if we consider only $$$i$$$ first characters of the original string).
The transitions here are a bit tricky. If we apply the operation U or D to the character $$$i + 1$$$, then $$$dp_{i + 1} = dp_i + c'$$$, where $$$c'$$$ is the character we get when we apply the aforementioned operation to that character. L is a bit more complicated: we have to insert the character $$$s_{i + 1}$$$ just before the last character of $$$dp_i$$$.
Modeling that we can apply the operation R is likely the most complex transition in our dynamic programming. First of all, we can't just make an update to $$$dp_{i + 1}$$$ or $$$dp_{i + 2}$$$, since it leads us to a situation where we can still apply some operations to the prefix we have built. Instead, we have to consider the operation we will be able to do with the character $$$s_{i + 2}$$$. Using another operation R is useless since the result is like performing no operations with those two characters at all, so we have to consider two options for operation with the $$$(i + 2)$$$-th character — D or U (whichever is better), or L. In the first case, we update $$$dp_{i + 2}$$$ by appending the resulting two characters to $$$dp_i$$$ (the one that we get when we change $$$s_{i + 2}$$$, and the one that initially was $$$s_{i + 1}$$$). In the second case, things are a bit trickier, but still not very complicated: the character that was $$$s_{i + 2}$$$ moves two positions backward, so it is inserted right before the last character of $$$dp_i$$$, and then we append $$$s_{i + 1}$$$ to the string we get.
So, there are four transitions we have to make:
• a transition from $$$dp_i$$$ to $$$dp_{i + 1}$$$ that models the case when we apply U or D to the $$$(i + 1)$$$-th character;
• a transition from $$$dp_i$$$ to $$$dp_{i + 1}$$$ that models the case when we apply L to the $$$(i + 1)$$$-th character;
• a transition from $$$dp_i$$$ to $$$dp_{i + 2}$$$ to model the operations RD or RU;
• a transition from $$$dp_i$$$ to $$$dp_{i + 2}$$$ to model the operations RL.
Overall complexity is $$$O(n^2)$$$ but it can be improved to $$$O(n \log n)$$$ with some complicated data structures like persistent segment tree with hashes to compare strings and append characters to them in $$$O(\log n)$$$.