#P16753. [GKS 2020 #B] Robot Path Decoding

[GKS 2020 #B] Robot Path Decoding

Problem Description

Your country's space agency has just landed a rover on a new planet. The planet's surface can be thought of as a grid of squares containing 10910^9 columns (numbered starting from 11 from west to east) and 10910^9 rows (numbered starting from 11 from north to south). Let (w,h)(w, h) denote the square in the ww-th column and the hh-th row. The rover begins on the square (1,1)(1, 1).

The rover can be maneuvered around on the surface of the planet by sending it a program, which contains a string of characters representing movements in the four cardinal directions. The robot executes each character of the string in order. The rover moves according to the following rules:

  • N: Move one unit north.
  • S: Move one unit south.
  • E: Move one unit east.
  • W: Move one unit west.

There is also a special instruction X(Y)\text{X}(\text{Y}), where X\text{X} is a number between 22 and 99 (inclusive) and Y\text{Y} is a non-empty subprogram. This denotes that the robot should repeat the subprogram Y\text{Y} a total of X\text{X} times. For example:

  • 2(NWE)2(\text{NWE}) is equivalent to NWENWE.
  • 3(2(E))3(\text{S } 2(\text{E})) is equivalent to SEESEESEE.
  • EEEE4(N)4(\text{N}) 2(SS)2(\text{SS}) is equivalent to EEEENNNNSSSS.

Since the planet is a spheroid, the first and last columns are adjacent, so moving east from column 10910^9 will move the rover to column 11 and moving south from row 10910^9 will move the rover to row 11. Similarly, moving west from column 11 will move the rover to column 10910^9 and moving north from row 11 will move the rover to row 10910^9. Given a program that the robot will execute, determine the final position of the robot after it has finished all its movements.

Input Format

The first line of the input gives the number of test cases, TT. TT lines follow. Each line contains a single string: the program sent to the rover.

Output Format

For each test case, output one line containing Case #x: w h, where x is the test case number (starting from 1) and w h is the final square (w,h)(w, h) the rover finishes in.

4
SSSEEE
N
N3(S)N2(E)N
2(3(NW)2(W2(EE)W))
Case #1: 4 4
Case #2: 1 1000000000
Case #3: 3 1
Case #4: 3 999999995

Hint

In Sample Case #1, the rover moves three units south, then three units east.

In Sample Case #2, the rover moves one unit north. Since the planet is a torus, this moves it into row 10910^9.

In Sample Case #3, the program given to the rover is equivalent to NSSSNEEN.

In Sample Case #4, the program given to the rover is equivalent to NWNWNWWEEEEWWEEEEWNWNWNWWEEEEWWEEEEW

Limits

1T1001 \le T \le 100.

The string represents a valid program.

The length of each program is between 11 and 20002000 characters inclusive.

Test Set 1

The total number of moves the robot will make in a single test case is at most 10410^4.

Test Set 2

No additional constraints.