This page is still under construction.

Parts of this page are still being built. What you see may change.

Pandora

Time limit1sMemory limit128 MB

Summary
Given the left/right turn sequence of a rectilinear polygon, count the coordinate axes it is monotone with respect to.
Level

Hard8 of 10

Topics
Geometry, String
Solved
No attempts yet

Problem

An unmanned robot named Pandora, launched by KARA (the Korea Astronomy Research Association), has landed on Mars, where it found a structure whose outline is a simple rectilinear polygon.

A rectilinear polygon is a polygon in which every edge is horizontal or vertical, so the interior angle at each vertex is either 90° or 270°. Such a polygon is simple when (1) exactly two edges meet at every vertex and (2) no two edges intersect except at a shared endpoint.

To recover the outline, Pandora walked along the boundary in counterclockwise order until it returned to the starting point, and reported, for each vertex, only the turning direction. The letter L marks a left turn (an interior angle of 90°) and the letter R marks a right turn (an interior angle of 270°). The edge lengths were lost in transmission, so the shape must be understood from the turn sequence alone.

The report is a string SS of the letters L and R. For instance, LLLL describes an axis-aligned rectangle. Let ll be the number of L's and rr the number of R's in SS. Because the walk is counterclockwise, l=r+4l = r + 4 always holds, with l≥4l \ge 4 and r≥0r \ge 0. Conversely, every string SS with l=r+4l = r + 4 can be realized by one or more simple rectilinear polygons on l+rl + r vertices whose counterclockwise turn sequence is exactly SS.

A simple rectilinear polygon is monotone with respect to the X-axis if every line perpendicular to the X-axis (that is, every vertical line) meets the polygon in at most one connected segment. Monotonicity with respect to the Y-axis is defined the same way using horizontal lines. All simple rectilinear polygons realizing a given SS share the same monotonicity, so the number of axes they are monotone with respect to depends only on SS. Output that number: 0 if they are monotone to neither axis, 1 if to exactly one axis, and 2 if to both axes.

Input

The first line contains the number of test cases TT. Each of the next TT lines contains one string SS consisting of the letters L and R, where the number of L's is exactly four more than the number of R's. The length of SS is between 4 and 100000 inclusive.

Output

For each test case, print one line containing the number of axes (0, 1, or 2) that the simple rectilinear polygons described by SS are monotone with respect to.

Examples4

  1. Example 1

    Input
    3
    LRLLLRLL
    LLLLLLRR
    LRRLLLRLLLRRLL
    
    Expected output
    2
    1
    0
    
  2. Example 2

    Input
    1
    LLLL
    
    Expected output
    2
    
  3. Example 3

    Input
    1
    LLRLLL
    
    Expected output
    2
    
  4. Example 4

    Input
    6
    LLLL
    LLRLLL
    LLRRLLLL
    LLRLLRRLLRRLLL
    LRRLLLRLLLRRLL
    LLLLRRRLLL
    
    Expected output
    2
    2
    1
    1
    0
    0