luogu#P14975. [USACO26JAN1] COW Splits B

[USACO26JAN1] COW Splits B

Problem Description

Bessie is given a positive integer NN and a string SS of length 3N3N which is generated by concatenating NN strings of length 33, each of which is a cyclic shift of COW\texttt{COW}. In other words, each string will be COW\texttt{COW}, OWC\texttt{OWC}, or WCO\texttt{WCO}.

String XX is a square string if and only if there exists a string YY such that X=Y+YX = Y + Y where ++ represents string concatenation. For example, COWCOW\texttt{COWCOW} and CC\texttt{CC} are examples of square strings but COWO\texttt{COWO} and OC\texttt{OC} are not.

In a single operation, Bessie can remove any subsequence TT from SS where TT is a square string. A subsequence of a string is a string which can be obtained by removing several (possibly zero) characters from the original string.

Your job is to help Bessie determine whether it is possible to transform SS into an empty string. Additionally, if it is possible, then you must provide a way to do so.

Bessie is also given a parameter kk which is either 00 or 11. Let MM be the number of operations in your construction.

  • If k=0k = 0, then MM must equal the minimum possible number of operations.
  • If k=1k = 1, then MM can be up to one plus the minimum possible number of operations

Input Format

The first line contains TT, the number of independent test cases (1T1041\le T\le 10^4) and kk (0k10 \le k \le 1).

The first line of each test case has NN (1N1051 \le N \le 10^5).

The second line of each test case has SS.

The sum of NN across all test cases will not exceed 10510^5.

Output Format

For each test case, output either one or two lines using the following procedure. If it is impossible to transform SS into an empty string, print 1-1 on a single line.

Otherwise, on the first line print MM -- the number of operations in your construction. On the second line, print 3N3N space-separated integers. The iith integer xx indicates that the iith letter of SS was deleted as part of the xxth subsequence (1xM1 \le x \le M).

3 1
3
COWOWCWCO
4
WCOCOWWCOCOW
6
COWCOWOWCOWCOWCOWC
-1
1
1 1 1 1 1 1 1 1 1 1 1 1
3
3 3 2 3 3 2 1 1 1 1 1 1 1 1 1 1 1 1
3 0
3
COWOWCWCO
4
WCOCOWWCOCOW
6
COWCOWOWCOWCOWCOWC
-1
1
1 1 1 1 1 1 1 1 1 1 1 1
2
1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2

Hint

For the last test, the optimal number of operations is two, so any valid construction with either M=2M=2 or M=3M=3 would be accepted.

For M=3M=3, here is a possible construction:

  1. In the first operation, remove the last twelve characters. Now we're left with COWCOW\texttt{COWCOW}.
  2. In the second operation, remove the subsequence WW. Now we're left with COCO\texttt{COCO}.
  3. In the last operation, remove all remaining characters.

  • Inputs 3-4: T10,N6,k=0T \le 10, N \le 6, k = 0
  • Inputs 5-6: k=1k = 1
  • Inputs 7-14: k=0k = 0