Official algorithmic problem description and constraints.
You are given a binary
β
string π
of length π
.
Your task is to find any subsequence
β
π
of π
such that:
You only need to output any valid subsequence π
that satisfies both conditions. If no such subsequence exists, output β1
.
Note that an empty string is both non-decreasing and a palindrome.
β
A binary string is a string consisting of characters '0' and '1'.
β
A subsequence of a string π =π
1
π
2
β¦π
π
is a sequence π=π
π
1
π
π
2
β¦π
π
π
such that 1β€π
1
<π
2
<β¦<π
π
β€π
. The characters are selected in order, but not necessarily contiguously. Note that an empty string is a subsequence of any string.
β‘
A string π‘=π‘
1
π‘
2
β¦π‘
π
is a palindrome if π‘
π
=π‘
πβπ+1
for all 1β€πβ€π
. In other words, the string reads the same forward and backward.
Input
The first line contains a single integer π‘
(1β€π‘β€3000
) β the number of test cases.
The first line of each test case contains a single integer π
(1β€πβ€10
) β the length of the string.
The second line contains a binary string π
of length π
.
Output
If a solution exists:
Otherwise, print a single line containing β1
.
Example
Input
Copy
5 3 010 3 001 5 00111 8 11010011 6 100101
Output
Copy
0 2 2 3 5 1 2 3 4 5 2 3 4 2 5 6
Note
In the first test case, we remove an empty string, resulting in π₯=πΆπ·πΆ
, which is a palindrome.
In the second test case, we remove π=πΆπ·
(indices 2
, 3
), resulting in π₯=πΆ
, which is a palindrome.
In the third test case, we remove π=πΆπΆπ·π·π·
(indices 1
to 5
), resulting in an empty string, which is trivially a palindrome.
In the fourth test case, we remove π=πΆπ·
(indices 3
, 4
), resulting in π₯=π·π·πΆπΆπ·π·
, which is a palindrome.
In the fifth test case, we remove π=πΆπ·
(indices 5
, 6
), resulting in π₯=π·πΆπΆπ·
, which is a palindrome.
Detailed video explanations, mathematical intuition, and clean C++ implementation code.