Fox Chef
Time limit1sMemory limit1024 MB
Given a row of O and X cookies, find the fewest flips of at least K consecutive cookies that make all cookies show the same side, and list those flips.
- Level
Medium6 of 10
- Topics
- Dynamic programming, Prefix sum, Greedy
- Solved
- No attempts yet
Problem
Foxes in the fox village also care about owning a home. A baby fox recently succeeded in living on its own, so its friends gathered at the baby fox's house to celebrate.
The baby fox decided to be a chef for a day and serve its grateful friends.
As an appetizer, the baby fox plans to arrange cookies decorated with foxes in a single row on a long plate. While setting the plate on the table, however, some cookies flipped over. If guests arrive like this, they may think the baby fox prepared the food carelessly.
The baby fox wants to flip the cookies back using tongs. Consecutive cookies can be flipped in one use. Because the cookies are small compared to the tongs, each use must flip at least consecutive cookies. The goal is to give the guests a neat impression, so it does not matter whether the cookies all face up or all face down, as long as they all show the same side.
The guests will arrive soon, and the baby fox has many other dishes to prepare. Determine whether all cookies can be made to show the same side using the tongs. If it is possible, find the minimum number of uses and how to use the tongs.
Input
The first line contains two positive integers and , separated by a space. ()
The second line contains a string of length consisting only of O and X. The -th character is O if the -th cookie lies face up, and X if it lies face down.
Output
If it is impossible to make all cookies show the same side no matter how the tongs are used, print -1 on the first line.
If it is possible, print the minimum number of tong uses () on the first line. Then print the uses, one per line, in order. To flip the cookies from the -th to the -th, print and separated by a space. (; )
Hint
You cannot move the cookies with the tongs. You can only change the face of each cookie in place.