Don't Stay
Time limit2sMemory limit512 MB
Given a lamplighter program s and the set of lamps that must be on, find a prefix program t that is undone after s so the final lamp state matches the target.
- Level
Medium7 of 10
- Topics
- Simulation, String, Implementation
- Solved
- No attempts yet
Problem
There are infinitely many lamps on a line, numbered with integers. Each lamp can be either turned on or turned off. A lamplighter starts at lamp number . He can walk one lamp left or right (that is, decrease or increase his coordinate by ) and toggle the lamp at his position. We denote these actions with letters "L", "R" and "X", respectively.
A string of letters "L", "R" and "X" is called a program for the lamplighter. For example, if he is currently at position and receives the string "RRXL", he goes right twice, toggles the lamp at position , and then goes left and ends up at position .
Initially, all lamps are turned off. In the end, you want to achieve a state where the lamps with coordinates , , are turned on, and all other lamps are turned off. The lamplighter has a program which he is going to execute, and he does not want to change his plans much. However, he agreed to help you a bit. If you give him a program , he will start by executing all instructions from . Then he will execute his own program , as he planned before. After that, he will try to cancel your help by performing all instructions from in reverse order, changing "L" to "R" and vice versa.
Given the coordinates and the lamplighter's program , find a program such that in the end, lamps , , are on and all other lamps are off, or determine that it is not possible.
Input
The first line contains a string () consisting of the characters "L", "R" and "X": the initial program of the lamplighter.
The second line contains an integer (), the number of lamps that should be turned on in the end. The third line contains space-separated integers: the coordinates of the lamps. All given coordinates are distinct and their absolute values do not exceed .
Output
Output the string that you can give to the lamplighter to achieve the goal. If there are several possible answers, output any one of them. The answer may be empty. The length of the answer must not exceed characters.
If achieving the goal is not possible, print a single word "NO" (without quotes).
Note
In the example, the lamplighter has the string "RXR", and the desired lamp positions are , , and . You can give the lamplighter the string "XLLXR". His combined program then becomes "XLLXR-RXR-LXRRX" (dashes added for clarity).
The lamplighter lights lamp number , then goes to and lights it, goes back to and turns it off, turns it on again, and finally goes to and turns it on. In the end, the state of all lamps is the one you wanted.