Take-out
Time limit1sMemory limit128 MB
A white block is 'b' and a black block is 'c'. Scan a row of blocks with a stack; whenever the top k+1 blocks hold k white and one black, pop and record them, then print the recorded groups in reverse order.
- Level
Medium5 of 10
- Topics
- Stack, Simulation, Greedy
- Solved
- No attempts yet
Problem
Little Edna received the take-out game as a present. Take-out is a single-player game. The player is given a row of blocks placed side by side and numbered to from left to right. Each block is either white or black, and the number of white blocks is exactly times the number of black blocks.
The goal is to remove every block using permissible moves.
A single move removes exactly white blocks together with one black block, without moving any of the other blocks. Call a gap the empty space left by a block that was removed in an earlier move. A move is permissible when, listing the blocks it removes from left to right, no gap sits between two consecutive removed blocks. Blocks that are still present may sit between the removed blocks; only gaps created by earlier moves are forbidden.
Every input can be cleared completely, so such a sequence of moves always exists.
Input
The first line contains two integers and (, ) separated by a single space: the number of blocks, and the number of white blocks removed together with one black block in each move. In every test case divides .
The second line is a string of characters. Each character is b for a white block or c for a black block, giving the colors of the blocks from left to right.
Output
Print lines, one per move. Each line lists the block indices removed in that move, in increasing order, separated by single spaces.
Many valid sequences can exist, so print the specific one built by the following canonical procedure, which always produces a valid answer:
- Scan the blocks from left to right, pushing each one onto a stack.
- After every push, while the top blocks on the stack contain exactly one black block (hence white blocks), pop those blocks as one group and place the group at the front of the answer list. Re-check the new top before pushing the next block.
- Once the last block has been pushed the stack is empty and exactly groups have been formed.
Print the groups in that order, so the group formed last is printed first. Within a line the indices are in increasing order.
Note
A gap is the empty space left where a block was taken out. For the first sample input, performing its moves in order transforms the row of blocks step by step, as illustrated below.
