#5394. Problem 2. COW Splits
Problem 2. COW Splits
Problem 2. COW Splits
USACO 2026 First Contest, Bronze
Bessie is given a positive integer and a string of length which is generated by concatenating strings of length , each of which is a cyclic shift of "COW". In other words, each string will be "COW", "OWC", or "WCO".
String is a
square string
if and only if there exists a string such that where represents string concatenation. For example, "COWCOW" and "CC" are examples of square strings but "COWO" and "OC" are not.
In a single operation, Bessie can remove any
subsequence
from where 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 into an empty string. Additionally, if it is possible, then you must provide a way to do so.
Bessie is also given a parameter which is either or . Let be the number of operations in your construction.
- If , then must equal the minimum possible number of operations.
- If , then can be up to one plus the minimum possible number of operations
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains , the number of independent test cases () and ().
The first line of each test case has ().
The second line of each test case has .
The sum of across all test cases will not exceed .
OUTPUT FORMAT (print output to the terminal / stdout):
For each test case, output either one or two lines using the following procedure.
If it is impossible to transform into an empty string, print on a single line.
Otherwise, on the first line print -- the number of operations in your construction. On the second line, print space-separated integers. The th integer indicates that the th letter of was deleted as part of the th subsequence ().
SAMPLE INPUT:
3 1 3 COWOWCWCO 4 WCOCOWWCOCOW 6 COWCOWOWCOWCOWCOWC
SAMPLE OUTPUT:
-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
For the last test, the optimal number of operations is two, so any valid construction with either or would be accepted.
For , here is a possible construction:
- In the first operation, remove the last twelve characters. Now we're left with COWCOW.
- In the second operation, remove the subsequence WW. Now we're left with COCO.
- In the last operation, remove all remaining characters.
SAMPLE INPUT:
3 0 3 COWOWCWCO 4 WCOCOWWCOCOW 6 COWCOWOWCOWCOWCOWC
SAMPLE OUTPUT:
-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
SCORING: Inputs 3-4: Inputs 5-6: Inputs 7-14:
Problem credits: Aakash Gokhale