浙大出题 zoj链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4115
签到题,太菜了,最后没签出来
思路:先遍历一次找出ans最大值,然后平移乘以k-1次,再遍历一次比较最大值
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e5+10; char s[N]; int main() { int t; cin>>t; while(t--){ ll x = 0,y = 0,n,k,ans = 0; cin>>n>>k; cin>>s; for(int i = 0; i < n; i++){ if(s[i] == 'R') x++; else if(s[i] == 'L') x--; else if(s[i] == 'U') y++; else if(s[i] == 'D')y--; ans = ans>(abs(x)+abs(y))?ans:(abs(x)+abs(y)); } x = (k-1)*x; y = (k-1)*y; for(int i = 0; i < n; i++){ if(s[i] == 'R') x++; else if(s[i] == 'L') x--; else if(s[i] == 'U') y++; else if(s[i] == 'D')y--; ans = ans>(abs(x)+abs(y))?ans:(abs(x)+abs(y)); } cout<<ans<<endl; } return 0; }