Hay Bales
InterviewTime limit2sMemory limit512 MB
Given a string of C and P, each move sorts any three consecutive characters so all C come before all P; find the minimum number of moves to fully sort the whole row.
- Level
Medium6 of 10
- Topics
- Greedy, String, Simulation, Dynamic programming
- Solved
- No attempts yet
Problem
Peter has lined up his hay bales in a row. Some of the bales carry parasites. To keep the parasites from spreading, Peter wants every infected bale at the back of the row.
In one operation Peter picks any three consecutive bales, takes them out, and puts them back in sorted order: the clean bales first, then the infected ones. The row is sorted once every clean bale stands in front of every infected bale.
Compute the minimum number of operations Peter has to execute to sort the row.
Input
The first line contains a string (), the sequence of hay bales. Each character of is either C for a clean bale or P for an infected one. The characters are given in the order the bales stand in the row.
Output
Print one integer, the minimum number of operations Peter has to execute.