Codeforces 712B Memory and Trident
Memory is performing a walk on the two-dimensional plane, starting at the origin. He is given a string s with his directions for motion:
- An 'L' indicates he should move one unit left.
- An 'R' indicates he should move one unit right.
- A 'U' indicates he should move one unit up.
- A 'D' indicates he should move one unit down.
But now Memory wants to end at the origin. To do this, he has a special trident. This trident can replace any character in s with any of 'L', 'R', 'U', or 'D'. However, because he doesn't want to wear out the trident, he wants to make the minimum number of edits possible. Please tell Memory what is the minimum number of changes he needs to make to produce a string that, when walked, will end at the origin, or if there is no such string.
The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.
If there is a string satisfying the conditions, output a single integer — the minimum number of edits required. In case it's not possible to change the sequence in such a way that it will bring Memory to to the origin, output -1.
RRU
-1
UDUR
1
RUUR
2
In the first sample test, Memory is told to walk right, then right, then up. It is easy to see that it is impossible to edit these instructions to form a valid walk.
In the second sample test, Memory is told to walk up, then down, then up, then right. One possible solution is to change s to "LDUR". This string uses 1 edit, which is the minimum possible. It also ends at the origin.
解题思路:
【题意】
Memory从二维坐标系的原点出发,按字符串s的指示运动
R:向右;L:向左;U:向上;D:向下
Memory最终想回到原点,问至少需要改变字符串s中的几个字符
若无论如何改变都无法回到原点,输出"-1"
【类型】
implementation
【分析】
很显然的,Memory从原点出发想要回到原点
那么他向右走的次数与向左走的次数需要一样,同时,向上走的次数和向下走的次数也必须一样
这也就是说,向右、向左、向上、向下走的总次数必定是偶数次
所以若字符串s长度为奇数,显然输出"-1"
接着将向右走的次数与向左走的次数抵消,向上走的次数与向下走的次数抵消
剩下的就只能用水平(向左、向右)的次数抵垂直(向上、向下)的次数,或垂直的次数抵水平的次数才能回到原点
而这部分就是需要改变的字符数
分别统计出向各个方向的步数,求出 abs(U-D)+abs(L-R) 回不到原点多余的步数。然后让 剩余的步数/2 修改一处可以保证两个状态OK
【时间复杂度&&优化】
O(strlen(s))
题目链接→Codeforces Problem 712B Memory and Trident
1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 char s[100005]; 6 int len; 7 int a=0,b=0,c=0,d=0; 8 while(gets(s)) 9 { 10 len=strlen(s); 11 if(len%2==1) 12 { 13 printf("-1\n"); 14 } 15 else 16 { 17 for(int i=0;s[i]!='\0';i++) 18 { 19 if(s[i]=='U') 20 a++; 21 else if(s[i]=='D') 22 b++; 23 else if(s[i]=='L') 24 c++; 25 else if(s[i]=='R') 26 d++; 27 } 28 printf("%d\n",(abs(a-b)+abs(c-d))/2); 29 a=b=c=d=0; 30 } 31 } 32 return 0; 33 }
作 者:Angel_Kitty
出 处:https://www.cnblogs.com/ECJTUACM-873284962/
关于作者:阿里云ACE,目前主要研究方向是Web安全漏洞以及反序列化。如有问题或建议,请多多赐教!
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
特此声明:所有评论和私信都会在第一时间回复。也欢迎园子的大大们指正错误,共同进步。或者直接私信我
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!
欢迎大家关注我的微信公众号IT老实人(IThonest),如果您觉得文章对您有很大的帮助,您可以考虑赏博主一杯咖啡以资鼓励,您的肯定将是我最大的动力。thx.
我的公众号是IT老实人(IThonest),一个有故事的公众号,欢迎大家来这里讨论,共同进步,不断学习才能不断进步。扫下面的二维码或者收藏下面的二维码关注吧(长按下面的二维码图片、并选择识别图中的二维码),个人QQ和微信的二维码也已给出,扫描下面👇的二维码一起来讨论吧!!!
欢迎大家关注我的Github,一些文章的备份和平常做的一些项目会存放在这里。