#P12956. [GCJ Farewell Round #3] Game Sort: Part 1

    ID: 12758 Type: RemoteJudge 5000ms 2048MiB Tried: 0 Accepted: 0 Difficulty: 3 Uploaded By: Tags>2023Special JudgeGoogle Code Jam

[GCJ Farewell Round #3] Game Sort: Part 1

题目描述

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 S\mathbf{S} and an integer P\mathbf{P} being chosen by an impartial judge. Then, Amir has to split S\mathbf{S} into exactly P\mathbf{P} contiguous non-empty parts (substrings). For example, if S=CODEJAM\mathbf{S} = \text{CODEJAM} was the chosen string and P=3\mathbf{P} = 3, Amir could split it up as [COD,EJA,M][\text{COD}, \text{EJA}, \text{M}] or as [CO,D,EJAM][\text{CO}, \text{D}, \text{EJAM}], but not as [COD,EJAM][\text{COD}, \text{EJAM}], [COD,JA,M][\text{COD}, \text{JA}, \text{M}], [EJA,COD,M][\text{EJA}, \text{COD}, \text{M}], nor as [CODE,EJA,M][\text{CODE}, \text{EJA}, \text{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, T\mathbf{T}. T\mathbf{T} test cases follow. Each test case consists of two lines. The first line of a test case contains a single integer P\mathbf{P}, the number of parts Amir made. The second line contains P\mathbf{P} strings $\mathbf{S}_1, \mathbf{S}_2, \ldots, \mathbf{S}_\mathbf{P}$, representing the P\mathbf{P} parts, in order.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is either POSSIBLE\text{POSSIBLE} if Badari can win the game, or IMPOSSIBLE\text{IMPOSSIBLE} if she cannot. If she can win the game, output a second line containing t1t2tPt_1 t_2 \ldots t_\mathbf{P} where tit_i is a rearrangement of the letters of Si\mathbf{S}_i, and tit_i is lexicographically earlier than or equal to ti+1t_{i+1}, for all ii. If there are multiple solutions, you may output any one of them.

3
3
CO DEJ AM
3
CODE JA M
2
ABABABAB AAA
Case #1: POSSIBLE
CO DEJ MA
Case #2: POSSIBLE
CODE JA M
Case #3: IMPOSSIBLE

提示

Sample Explanation

In Sample Case #1, Badari could also win in 5 other ways. Two of them are [CO,JED,MA][\text{CO}, \text{JED}, \text{MA}] and [CO,EJD,MA][\text{CO}, \text{EJD}, \text{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.

Limits

1T1001 \leq \mathbf{T} \leq 100.

Each character of Si\mathbf{S}_i is an English uppercase letter A through Z, for all ii.

Test Set 1 (4 Pts, Visible Verdict)

  • 2P32 \leq \mathbf{P} \leq 3.
  • 1the length of Si81 \leq \text{the length of } \mathbf{S}_i \leq 8, for all ii.

Test Set 2 (9 Pts, Hidden Verdict)

  • 2P1002 \leq \mathbf{P} \leq 100.
  • 1the length of Si1001 \leq \text{the length of } \mathbf{S}_i \leq 100, for all ii.