Problem Solving Problem (Juryeonggu)
Time limit1sMemory limit512 MB
Given a start face and a command string with nested repeats up to 10^9, roll a die on the octa-hexahedron and print the face on the floor at the end.
- Level
Medium6 of 10
- Topics
- Simulation, String, Recursion
- Solved
- No attempts yet
Problem
This problem cannot be submitted. See the problem "A Problem for Solving Problems".
The juryeonggu, a Silla-period die excavated from Anapji in Gyeongju, has a shape similar to a hexa-octahedron. It was used to decide the penalties when someone broke a drinking rule. It has eight hexagonal faces that look like triangles and six square faces.

Figure 1: Juryeonggu found in Onyang.
Let's play by rolling this die. At the start, you are given one face touching the floor and an adjacent face that shares an edge with it. The adjacent face lies in the downward direction from the edge touching the floor, and it is given to show the orientation of the die. Then process the commands in the input one character at a time, in order. The commands are L, R, U, and D, which mean left, right, up, and down. For each command, find the face adjacent to the current face in that direction, and roll the die until that face touches the floor. When the roll is completely finished, orient the die so that the face that was touching the floor before is facing down.
The short edges of the hexagonal faces are ignored. The die does not roll in that direction, and no adjacent face is considered to exist in that direction. (You can think of a hexagonal face as a triangle.) So if a roll cannot be performed, that is, when a hexagonal face is touching the floor and an up roll is required, that roll is ignored.
After performing all the commands in order, print the face touching the floor at the end.
The real juryeonggu has Chinese characters on every face, so for convenience this problem uses an octa-hexahedron folded from the net below, with the folds pointing inward so that the printed side faces away from the viewer.
\
Figure 2: The octa-hexahedron used in this problem. Some words that bring to mind the Gudegi Cup this year are included, so if you have time, cut out the net and fold it yourself.
Input
The first line contains the face touching the floor at the start and the face that should be placed directly below after the first roll, separated by a space.
The second line contains a string made of L, R, U, D, and (...)n. The length of the string is at least 1 and does not exceed 200,000. L, R, U, and D are commands that roll the die left, right, up, and down. (...)n means to repeat the string inside the parentheses n times. Here n is an integer with .
For example, L(U(LR)3D)2R is the same as LULRLRLRDULRLRLRDR.
Output
Print the face touching the floor after rolling the die according to the given commands.