Codeforces 712B
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
#include <bits/stdc++.h>
using namespace std;
int main()
{
char s[];
int len;
int a=,b=,c=,d=;
while(gets(s))
{
len=strlen(s);
if(len%==)
{
printf("-1\n");
}
else
{
for(int i=;s[i]!='\0';i++)
{
if(s[i]=='U')
a++;
else if(s[i]=='D')
b++;
else if(s[i]=='L')
c++;
else if(s[i]=='R')
d++;
}
printf("%d\n",(abs(a-b)+abs(c-d))/);
a=b=c=d=;
}
}
return ;
}
Codeforces 712B的更多相关文章
- codeforces 712B. Memory and Trident
题目链接:http://codeforces.com/problemset/problem/712/B 题目大意: 给出一个字符串(由'U''D''L''R'),分别是向上.向下.向左.向右一个单位, ...
- codeforces 712B B. Memory and Trident(水题)
题目链接: B. Memory and Trident time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Memory and Trident(CodeForces 712B)
Description Memory is performing a walk on the two-dimensional plane, starting at the origin. He is ...
- CodeForces 712B Memory and Trident (水题,暴力)
题意:给定一个序列表示飞机要向哪个方向飞一个单位,让你改最少的方向,使得回到原点. 析:一个很简单的题,把最后的位置记录一下,然后要改的就是横坐标和纵坐标绝对值之和的一半. 代码如下: #pragma ...
- CSUST 8.4 早训
## Problem A A - Memory and Crow CodeForces - 712A 题意: 分析可得bi=ai+ai+1 题解: 分析可得bi=ai+ai+1 C++版本一 #inc ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
随机推荐
- 漂亮的HTML表格 - ebirdfighter的日志 - 网易博客
一个像素边框的表格: Info Header 1 Info Header 2 Info Header 3 Text 1A Text 1B Text 1C Text 2A Text 2B Text 2C ...
- 无限循环小数POJ1930
题意:给定一个无限循环小数,求其分数形势,要求分母最小 分析:看了别人的题解才做出来的,将无限循环小数转化成分数,分为纯循环和混循环两种形式. (1)对于纯循环:用9做分母,有多少个循环数就几个9,比 ...
- Java的JDBC事务详解
Java的JDBC事务详解 分类: Hibernate 2010-06-02 10:04 12298人阅读 评论(9) ...
- SQL复习四(完整性约束)
完整性约束是为了表的数据的正确性.主要有主键,外键的约束. 1 主键 当某一列添加了主键约束后,该列的数据就不能重复出现.这样每行记录中其主键列就能唯一的标识着以行.如学生可以用学号作为唯一的标识. ...
- XML&AJAX
AJAX: Asynchronous Javascript and XML 1. 客户端触发异步操作 2. 创建新的XMLHttpRequest, 是重要的js对象,通过它提起对服务器端的请求 3. ...
- JAVA中传递参数乱码问题
url传递中文如果jsp页面,myeclipse.web.xml中org.springframework.web.filter.CharacterEncodingFilter,都是UTF-8编码,直接 ...
- html 设置页脚div一直在页面底部
先上代码 <!DOCTYPE HTML> <html lang="en" style="height: 100%; width: 100%;" ...
- iOS开发UITableView基本使用方法总结
本文为大家呈现了iOS开发中UITableView基本使用方法总结.首先,Controller需要实现两个delegate ,分别是UITableViewDelegate 和UITableViewDa ...
- iOS自定义字体及类目 分类: ios技术 2015-05-15 16:34 195人阅读 评论(0) 收藏
1:获取字体文件 从各种渠道下载字体文件ttf, 网站或者从别的ipa里扣出来.(以fzltxh.ttf为例) 2:将fzltxh.ttf文件拷贝到工程中 3:在Info.plist中添加项: Fon ...
- JQuery Easy Ui dataGrid 数据表格 -->转
转至: http://www.cnblogs.com/cnjava/archive/2013/01/21/2869876.html#events 数据表格 - DataGrid 内容 概况 使用方法 ...