Game Sort: Part 2

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

요약
문자열 S를 정확히 P개의 연속 부분으로 나눠, 각 부분의 글자를 재배열해도 부분 목록을 사전순으로 정렬할 수 없게 만들고, 그런 분할을 출력하거나 IMPOSSIBLE을 출력한다.
난이도

보통10점 중 7점

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

문제

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 initial string and number of parts, can you help Amir win the game by choosing his parts in a way Badari cannot win herself? If not, say that it is not possible.

입력

The first line of the input gives the number of test cases, T\mathbf{T}. T\mathbf{T} lines follow, each describing a single test case containing an integer P\mathbf{P} and a string S\mathbf{S}, the number of parts and string to be partitioned, respectively.

출력

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 Amir can win the game, or IMPOSSIBLE if he cannot. If he 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 the ii-th part of the winning partition you found for Amir. If there are multiple solutions, you may output any one of them.

제한

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

힌트

In Sample Case #1, there is no way for Badari to rearrange DEJAM to be lexicographically after O, so Amir guaranteed a win.

In Sample Case #2, AAA is guaranteed to be earlier than any rearrangement of a string containing more than 33 letters, so Amir also wins.

In Sample Case #3, all possible partitions result in a list of parts that is already sorted in lexicographical order, so Amir cannot possibly win.

예제1

  1. 예제 1

    입력
    3
    3 CODEJAM
    2 ABABABABAAAA
    3 AABBCDEEFGHIJJKLMNOPQRRSTUVWXYZZ
    
    예상 출력
    Case #1: POSSIBLE
    C O DEJAM
    Case #2: POSSIBLE
    ABABABABA AAA
    Case #3: IMPOSSIBLE