DreamGrid creates a programmable robot to explore an infinite two-dimension plane. The robot has a basic instruction sequence and a "repeating parameter" , which together form the full instruction sequence and control the robot.
There are 4 types of valid instructions in total, which are 'U' (up), 'D' (down), 'L' (left) and 'R' (right). Assuming that the robot is currently at , the instructions control the robot in the way below:
- U: Moves the robot to .
- D: Moves the robot to .
- L: Moves the robot to .
- R: Moves the robot to .
The full instruction sequence can be derived from the following equations
The robot is initially at and executes the instructions in the full instruction sequence one by one. To estimate the exploration procedure, DreamGrid would like to calculate the largest Manhattan distance between the robot and the start point during the execution of the instructions.
Recall that the Manhattan distance between and is defined as .
Input
There are multiple test cases. The first line of the input contains an integer indicating the number of test cases. For each test case:
The first line contains two integers and (), indicating the length of the basic instruction sequence and the repeating parameter.
The second line contains a string (, ), where indicates the -th instruction in the basic instruction sequence.
It's guaranteed that the sum of of all test cases will not exceed .
Output
For each test case output one line containing one integer indicating the answer.
Sample Input
2 3 3 RUL 1 1000000000 D
Sample Output
4 1000000000
Hint
For the first sample test case, the final instruction sequence is "RULRULRUL" and the route of the robot is (0, 0) - (1, 0) - (1, 1) - (0, 1) - (1, 1) - (1, 2) - (0, 2) - (1, 2) - (1, 3) - (0, 3). It's obvious that the farthest point on the route is (1, 3) and the answer is 4.
省赛题目:
一看是想的是先让机器人先走一遍,记住那是的坐标x,y,把他的坐标乘m-1遍,再走就是最后一遍循环,
这样可以找到那个最后一遍的最大值,
后来发现少了一种情况,在第一遍就出现最大值,以后没有超过的情况
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int t,n,m;
cin>>t;
string s;
long long x,y;
long long maxn;
while(t--){
maxn=0;
cin>>n>>m;
cin>>s;
x=0;
y=0;
for(int i=0;i<s.length();i++){
if(s[i]=='U'){
y++;
}else if(s[i]=='D'){
y--;
}else if(s[i]=='R'){
x++;
}else if(s[i]=='L'){
x--;
}
if(abs(x)+abs(y)>maxn){
maxn=abs(x)+abs(y);
}
}
x=(m-1)*x;
y=(m-1)*y;
for(int i=0;i<s.length();i++){
if(s[i]=='U'){
y++;
}else if(s[i]=='D'){
y--;
}else if(s[i]=='R'){
x++;
}else if(s[i]=='L'){
x--;
}
if(abs(x)+abs(y)>maxn){
maxn=abs(x)+abs(y);
}
}
cout<<maxn<<endl;
}
return 0;
}