Robot Movement
Time limit2sMemory limit512 MB
A robot walks on an infinite grid following a fixed-length string of U, D, L, R moves. Change at most M characters to maximize how many times it returns to the origin.
- Level
Medium6 of 10
- Topics
- Dynamic programming, String, Implementation, Brute force
- Solved
- No attempts yet
Problem
A robot stands on an infinitely large grid board. The board is divided into cells of size 1×1, and every cell is named by a coordinate . The robot occupies one cell, and it starts at .
The robot comes with a program, a string of operations written with the letters U, D, L, and R. U moves the robot one cell up, D one cell down, L one cell left, and R one cell right. When the power goes on, the robot performs the programmed operations one at a time from the front. After the last operation it stops moving and turns itself off.
The robot claps every time it arrives at cell . You want to rewrite the program so that the number of visits to is as large as possible. You may change at most operations. One change means picking a single character of the program and replacing it with a different character. Inserting a character or deleting one is not allowed.
The starting position is not counted as a visit. Only a moment right after an operation, with the robot standing on , counts.
Given the operation string and the integer , write a program that finds the maximum number of visits to .
Input
The first line contains the program of the robot. consists only of the letters U, D, L, and R. The second line contains the integer .
Let be the length of . Then and .
Output
Print on the first line the maximum number of visits to that at most changes can produce.
Hint
When is UULRRLLL and is 1, changing the first U into D gives three visits to . Changing the second U into D also gives three visits.