아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Game Sort: Part 1

시간 제한5초메모리 제한1024 MB

요약
P개의 문자열이 주어질 때 각 문자열의 글자 순서를 바꿔 전체 목록이 사전순으로 증가하도록 만들 수 있는지 판정하고, 가능하면 그 예를 출력한다.
난이도

보통10점 중 5점

유형
그리디, 정렬, 문자열
정답자
아직 제출이 없습니다

문제

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=\mathbf{S} = \vphantom{}CODEJAM was the chosen string and P=3\mathbf{P} = 3, 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, 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 S_1,S_2,…,S_P\mathbf{S\_1}, \mathbf{S\_2}, \dots, \mathbf{S\_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 if Badari can win the game, or IMPOSSIBLE if she cannot. If she can win the game, output a second line containing t_1,t_2,…,t_Pt\_1\\, t\_2 \\,\dots\\, t\_{\mathbf{P}} where t_it\_i is a rearrangement of the letters of S_i\mathbf{S\_i}, and t_it\_i is lexicographically earlier than or equal to t_i+1t\_{i+1}, for all ii. If there are multiple solutions, you may output any one of them.

제한

  • 1≤T≤1001 \le \mathbf{T} \le 100.
  • Each character of S_i\mathbf{S\_i} is an English uppercase letter A through Z, for all ii.

힌트

In Sample Case #1, Badari could also win in 55 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.

예제1

  1. 예제 1

    입력
    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