#P1563. Toy Figures Puzzle

    ID: 14849 Type: Default 1000ms 256MiB

Toy Figures Puzzle

Toy Figures Puzzle

There are n toy figures arranged in a circle in clockwise order. Each toy has a profession and a facing direction (either in or out). A toy facing inward (i.e. in) has its left side in the clockwise direction and its right side in the counterclockwise direction. Conversely, a toy facing outward (i.e. out) has its left side in the counterclockwise direction and its right side in the clockwise direction.

The first toy (the 1st toy in the input order) issues a puzzle consisting of m instructions. Each instruction is given in the format:

  • left s or right s where s is a positive integer.

To process an instruction, starting from the current toy, do not count the current toy; instead, count s toys in the direction determined by both the instruction and the current toy's orientation. That is:

  • If the current toy is facing in:
    • left means move s positions in the clockwise direction.
    • right means move s positions in the counterclockwise direction.
  • If the current toy is facing out:
    • left means move s positions in the counterclockwise direction.
    • right means move s positions in the clockwise direction.

All movements are done modulo n (i.e. the circle wraps around). After processing all m instructions in order, output the profession of the toy at the final position.

inputFormat

The input consists of several parts:

  1. The first line contains two integers n and m (the number of toys and the number of instructions).
  2. The next n lines each contain a string and a word, representing a toy's profession and its facing direction. The direction is either in or out. The toys are given in clockwise order.
  3. The next m lines each contain an instruction in one of the two forms: left s or right s, where s is a positive integer.

outputFormat

Output a single line containing the profession (a string) of the toy reached after following all the instructions.

sample

4 3
singer in
writer in
thinker out
archer out
left 3
right 1
left 2
singer