Fixing the Path
Time limit1sMemory limit1024 MB
Given a walk string of L, R, U, D moves, answer for each target point the fewest character changes so the final position equals that point.
- Level
Medium7 of 10
- Topics
- Math, Greedy, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
Seokpyo lives at on an ordinary coordinate plane where the -coordinate increases to the right and the -coordinate increases upward. He wants to go to Junho's house at . Every second Seokpyo moves 1 unit horizontally or vertically, and the string of length describes his movement plan. Depending on whether the -th character of is L, R, D, U, Seokpyo moves 1 unit left, right, down, or up, respectively.
The movement plan made by the careless Seokpyo may not be correct. A movement plan is correct if Seokpyo is at after completing all the moves. Even if he reaches in the middle, the plan is not correct unless he is at at the end.
Seokpyo repeatedly changes one character of to another character to turn it into a correct movement plan. He went through all cases the hard way and found the minimum number of changes needed. But sadly, news arrived that Junho has moved. Junho has moved to one of locations. The -th location is . Now Seokpyo must find the minimum number of changes needed for each of the locations.
Seokpyo fainted on hearing that he must find the minimum number of changes times. Let us find the answers for him.
Input
The first line contains and separated by a space.
The next line contains .
The following lines contain and separated by a space.
Output
For each location, output -1 if it is impossible to turn the plan into a correct movement plan, or the minimum number of changes needed otherwise, each on its own line.