#P16871. [GKS 2022 #A] Palindrome Free Strings

[GKS 2022 #A] Palindrome Free Strings

Problem Description

You are given a string SS consisting of characters 00, 11, and ??. You can replace each ?? with either 00 or 11. Your task is to find if it is possible to assign each ?? to either 00 or 11 such that the resulting string has no substrings that are palindromes of length 55 or more.

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow.

Each test case consists of two lines.

The first line of each test case contains an integer NN, denoting the length of the string SS.

The second line of each test case contains a string SS of length NN.

Output Format

For each test case, output one line containing Case #x\#x: yy, where xx is the test case number (starting from 11) and yy is POSSIBLE if there is a possible resulting string that has no palindromic substrings of length 55 or more, or IMPOSSIBLE otherwise.

2
9
100???001
5
100??
Case #1: IMPOSSIBLE
Case #2: POSSIBLE

Hint

In Sample Case #11, to prevent the whole string from being a palindrome, the first and last question mark must be different characters.

If we replace first question mark with 00 and replace the last question mark with 11, we get 1000?10011000?1001. If the remaining ?? is replaced by 11, we get 100011001100011001, then the first 55 characters form a palindrome of length 55. Otherwise, we get 100001001100001001, the first 66 characters are a palindrome of length 66.

If we replace first question mark with 11 we get 1001?00011001?0001. If the remaining ?? is replaced by 11, we get 100110001100110001, then the last 55 characters form a palindrome of length 55. Otherwise, we get 100100001100100001, the last 66 characters are a palindrome of length 66.

Hence, there is no way to get a valid string.

In Sample Case #22, one of the valid strings after replacing all the ?? is 1001110011.

Limits

1T1001 \le T \le 100.

SS only consists of characters 00, 11 and ??.

Test Set 11

1N151 \le N \le 15.

Test Set 22

1N5×1041 \le N \le 5 \times 10^4.