TopcoderArchiveVisit Topcoder
SRM25 Jun 2013
SRM 589

GearsDiv2

Problem statement, definition, constraints, and public examples.

Problem Statement

Goose Tattarrattat has a machine that contains N gears (cogwheels). The gears are numbered 0 through N-1. Currently, the gears are arranged into a cycle: for each i, gear i meshes with gears i-1 and i+1 (counting modulo N). In particular, gear 0 meshes with gear N-1.


Let X and Y be two meshing gears. Note that if X is turning, Y must clearly be turning in the opposite direction (clockwise vs. counter-clockwise).


For each of the N gears we have a desired direction of turning. You are given this information encoded as a String Directions. Character i of Directions is 'R' if we want gear i to turn to the right (clockwise), and it is 'L' if we want it to turn to the left.


Of course, it may be impossible to satisfy all the desired directions of turning. Return the minimal number of gears that have to be removed from the machine so that all remaining gears can turn in the desired directions at the same time.

Definition

Class:
GearsDiv2
Method:
getmin
Parameters:
String
Returns:
int
Method signature:
int getmin(String Directions)
(be sure your method is public)

Notes

  • Removing a gear creates a gap between the other gears. For example, after removing the gear 7, gears 6 and 8 will not mesh.

Constraints

  • Directions will contain between 3 and 50 characters, inclusive.
  • Each character of Directions will be 'R' or 'L'.

Examples

  1. "LRRR"
    Returns: 1
    Once we remove gear 2, the other three are free to turn in the desired directions.
  2. "RRR"
    Returns: 2
    Gear 0 meshes with gear 2.
  3. "LRLR"
    Returns: 0
  4. "LRLLRRLLLRRRLLLL"
    Returns: 6
  5. "RRRRRRRLRRRRRRRLRLRLLRLRLRLRRLRLRLLLRLRLLRLLRRLRRR"
    Returns: 14
Back to all problems