""" Google Code Jam Farewell Round C - Problem E Author : chaotic_iak Language: PyPy (Python 3.9.10) """ ################################################### SOLUTION def initialize_solution(): ... def main(): p,S = read(str) p = int(p) if p >= 4: for i in range(len(S)-1): if S[i] > S[i+1]: found = i ans = [] if found == 0: ans.extend(S[:p-1]) ans.append(S[p-1:]) elif found == len(S)-2: ans.append(S[:-(p-1)]) ans.extend(S[-(p-1):]) elif p <= found+3: ans.extend(S[:p-4]) ans.append(S[p-4:found]) ans.extend(S[found:found+2]) ans.append(S[found+2:]) else: ans.extend(S[:p-1]) ans.append(S[p-1:]) write("POSSIBLE") write(*ans) return return "IMPOSSIBLE" if p == 3: mn = min(S) mx = max(S) if mn == mx: if len(S) == 3: return "IMPOSSIBLE" write("POSSIBLE") write(S[:2], S[2], S[3:]) return if any(S[i] == mn for i in range(2, len(S))): found = min(i for i in range(2, len(S)) if S[i] == mn) if found == len(S)-1: write("POSSIBLE") write(S[:-2], S[-2], S[-1]) return else: write("POSSIBLE") write(S[:found], S[found], S[found+1:]) return if S[1] == mn and S[0] != mn: write("POSSIBLE") write(S[0], S[1], S[2:]) return gone = (1 if S[-1] == mx else 0) streak = 0 for i in range(len(S)-2, -1, -1): if S[i] == mx: streak += 1 if streak > gone: write("POSSIBLE") write(S[:i], S[i:i+streak], S[i+streak:]) return else: gone += streak streak = 0 return "IMPOSSIBLE" if p == 2: cl = [0]*26 cr = [0]*26 for c in S: cr[ord(c)-65] += 1 lmn = 26 rmx = max(i for i in range(26) if cr[i]) for i in range(len(S)-1): c = ord(S[i])-65 cl[c] += 1 lmn = min(lmn, c) cr[c] -= 1 while cr[rmx] == 0: rmx -= 1 if lmn > rmx: write("POSSIBLE") write(S[:i+1], S[i+1:]) return if lmn == rmx: share = min(cl[lmn], cr[lmn]) if sum(cl) > share: write("POSSIBLE") write(S[:i+1], S[i+1:]) return return "IMPOSSIBLE" ############################################### PROBLEM TYPE HAS_TESTCASES = True INTERACTIVE = False INTERACTIVE_WRONG_ANSWER = "" #################################################### HELPERS import sys def read_str(): ipt = input().strip() if INTERACTIVE and ipt == INTERACTIVE_WRONG_ANSWER: sys.exit() return ipt def read(mapping=int): ipt = input().strip() if INTERACTIVE and ipt == INTERACTIVE_WRONG_ANSWER: sys.exit() return list(map(mapping, ipt.split())) def write_str(obj, end="\n"): global OUTPUT OUTPUT.append(obj + end) def write(*obj, sep=" ", end="\n"): global OUTPUT OUTPUT.append(sep.join(map(str, obj)) + end) def flush(): global OUTPUT print("".join(OUTPUT), end="", flush=True) OUTPUT = [] def solve_testcase(): result = main() if result is not None: write(result) OUTPUT = [] initialize_solution() if HAS_TESTCASES: TOTAL_CASES = int(input()) else: TOTAL_CASES = 1 for CASE_NUMBER in range(TOTAL_CASES): write("Case #" + str(CASE_NUMBER+1) + ": ", end="") solve_testcase() flush() flush() READ_FROM_FILE = ""