Codeforces 712B Memory and Trident
B. Memory and Trident
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
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.
Input
The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.
Output
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.
Examples
Input
RRU
Output
-1
Input
UDUR
Output
1
Input
RUUR
Output
2
Note
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("-1n");
14 }
15 else
16 {
17 for(int i=0;s[i]!=' ';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("%dn",(abs(a-b)+abs(c-d))/2);
29 a=b=c=d=0;
30 }
31 }
32 return 0;
33 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Android自定义View中attrs.xml的实例详解
- Android开发之缓冲dialog对话框创建、使用与封装操作
- Windows 10 下安装 Apache 2.4.41的教程
- Android基础教程数据存储之文件存储
- 关于Android短信验证码的获取的示例
- Linux下制作给ARM开发板使用的文件系统
- Android开发之ProgressBar字体随着进度条的加载而滚动
- Android 动态注册监听网络变化实例详解
- linux 服务器自动备份脚本的方法(mysql、附件备份)
- 详解Android中Glide与CircleImageView加载圆形图片的问题
- android全局监控click事件的四种方式(小结)
- Linux下如何永久修改主机名的方法步骤
- android与asp.net服务端共享session的方法详解
- Android实现多个连续带数字圆圈效果
- Android录制mp3格式文件