Left and Right
InterviewTime limit2sMemory limit512 MB
Find the lexicographically smallest permutation of 1..n whose consecutive moves match the given L/R string.
- Level
Medium5 of 10
- Topics
- Greedy, Stack, Implementation
- Solved
- No attempts yet
Problem
With advances in technology, it is now possible to deliver mail with a robot. There is a neighborhood on a long horizontal road, with n houses numbered 1 to n from left to right. Every day a mail delivery robot receives a pile of letters with exactly one letter for each house. Due to mechanical restrictions, the robot cannot sort the letters. It always checks the letter on top of the pile, visits the house that should receive that letter, and delivers it. The robot repeats this procedure until all the letters are delivered. As a result, each of the n houses is visited by the robot exactly once during the mail delivery of a single day.
The mail delivery robot has a tracking device that records its delivery route. One day the device broke, and the exact route was lost. However, the technical team managed to recover the moving directions of the robot from the broken device, which are represented as a string of n − 1 letters. The i-th letter of the string is ‘L’ (or ‘R’) if the (i + 1)-th house visited by the robot is on the left (or right) of the i-th house visited. For example, if n = 4 and the robot visited the houses in the order 2, 4, 3, 1, its moving directions would be “RLL”.
With the moving directions, it may be possible to determine the order in which the robot visited the houses. The technical team has asked you to write a program to do that. There can be multiple orders producing the same moving directions, and among them you should find the lexicographically earliest order.
Input
The first line contains a single integer n (2 ≤ n ≤ 2 · 105). The second line contains a string of length n − 1 consisting of the letters ‘L’ and ‘R’ giving the moving directions of the robot.
Output
Output the lexicographically earliest order in which the robot may have visited the houses and delivered the letters according to the moving directions, one house number per line.