Game Sort: Part 1
시간 제한5초메모리 제한1024 MB
P개의 문자열이 주어질 때 각 문자열의 글자 순서를 바꿔 전체 목록이 사전순으로 증가하도록 만들 수 있는지 판정하고, 가능하면 그 예를 출력한다.
문제
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 being chosen by an impartial judge. Then, Amir has to split into exactly contiguous non-empty parts (substrings). For example, if CODEJAM was the chosen string and , Amir could split it up as [COD, EJA, M] or as [CO, D, EJAM], but not as [COD, EJAM], [COD, JA, M], [EJA, COD, M], nor as [CODE, EJA, M].
Then, Badari must rearrange the letters within each part to make the list of parts be sorted in non-decreasing lexicographical order. If she can, then she wins. Otherwise, Amir wins.
Given the partition Amir made, can you help Badari win the game, or say that it is not possible?
입력
The first line of the input gives the number of test cases, . test cases follow. Each test case consists of two lines. The first line of a test case contains a single integer , the number of parts Amir made. The second line contains strings , representing the parts, in order.
출력
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 the game, or IMPOSSIBLE if she cannot. If she can win the game, 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.
제한
- .
- Each character of is an English uppercase letter
AthroughZ, for all .
힌트
In Sample Case #1, Badari could also win in 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 it to her, but other ways are also possible.
In Sample Case #3 Amir has guaranteed a win for himself leaving Badari no winning option.