Game Sort: Part 1
InterviewTime limit5sMemory limit1024 MB
Given P strings, rearrange the letters inside each one so the resulting list is in non-decreasing lexicographic order, or report that it cannot be done.
Problem
Note: The main parts of the statements of the problems Game Sort: Part 1 and Game Sort: Part 2 are identical, except for the last paragraph. The problems can otherwise be solved independently.
Amir and Badari are playing a sorting game. The game starts with a string and an integer chosen by an impartial judge. Amir must split into exactly contiguous non-empty parts (substrings). For example, if is CODEJAM and , Amir can split it as [COD, EJA, M] or [CO, D, EJAM]. Amir cannot split it as [COD, EJAM], [COD, JA, M], [EJA, COD, M], or [CODE, EJA, M].
Then Badari must rearrange the letters within each part so that the list of parts is sorted in non-decreasing lexicographical order. If she can, Badari wins. Otherwise, Amir wins. Given the partition Amir made, can you help Badari win, or say that it is not possible?
Input
The first line contains the number of test cases . test cases follow. Each test case has two lines. The first line contains a single integer , the number of parts Amir made. The second line contains strings , representing the parts in order.
Output
For each test case, output one line containing Case #x: y, where is the test case number (starting from 1) and is either POSSIBLE if Badari can win, or IMPOSSIBLE if she cannot. If she can win, output a second line containing , where is a rearrangement of the letters of , and is lexicographically earlier than or equal to for all . If there are multiple solutions, you may output any one of them.
Limits
.
For all , each character of is an English uppercase letter from A through Z.
Hints
In Sample Case #1, Badari could also win in 5 other ways. Two of them are [CO, JED, MA] and [CO, EJD, MA].
In Sample Case #2, Badari can win simply by leaving all parts as Amir gave them. Other ways are also possible.
In Sample Case #3, Amir has guaranteed a win for himself by leaving Badari no winning option.