Luxury River Cruise
InterviewTime limit1sMemory limit128 MB
Each of N ports has a left and right outgoing river; follow a fixed direction string of length M repeated K times from port 1 and report the final port.
- Level
Medium6 of 10
- Topics
- Binary search, Graph, Simulation, Implementation
- Solved
- No attempts yet
Problem
Farmer John is taking Bessie and the cows on a cruise! They are sailing on a network of rivers with ports () labeled , and Bessie starts at port . Each port has exactly two rivers leading out of it, going directly to two other ports, and each river can be sailed in only one direction.
At each port the tour guides pick either the left river or the right river to sail down next, and they repeat the same pattern over and over. Concretely, the guides fix a short sequence of directions (), each either left or right, and they follow this whole sequence times (). Bessie feels like she is going in circles -- help her work out which port she finishes at!
Input
- Line 1: three space-separated integers , , and .
- Lines 2 to : line contains two space-separated integers -- the ports that port 's left river and right river lead to, respectively.
- Line : space-separated characters, each either
LorR.Lmeans the left river andRmeans the right river.
Output
- A single integer: the number of the port where Bessie's cruise ends.
Hint
In the sample, the port numbers are arranged clockwise around a circle, L is a clockwise step and R is a counterclockwise step, and the sequence followed is L L R repeated three times.
After the first full pass through the direction sequence Bessie is at port (); after the second pass she is at port (); and after the third pass she finishes at port ().