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 ...
随机推荐
- openstack名称发音收集
MariaDB: maria['mɛərɪr] Corosync: coro[kə'roʊ] sync[sɪŋk] pacemaker: [ˈpesˌmekɚ] galera: ...
- 加载window事件
$(document).ready()和window.onload的区别 发表于 2012-08-29 由 admin 以浏览器装载文档为例,在页面加载完毕后,浏览器会通过JavaScript为DOM ...
- Struts2实现异步调用机制详细剖析(XML和JSON)
一.使用XML传递 1.页面展示getXML.jsp <%@ page language="java" import="java.util.*" page ...
- 基于FPGA的OLED真彩色动态图像显示的实现
源:基于FPGA的OLED真彩色动态图像显示的实现 作为第3代显示器,有机电致发光器件(Organic Light Emitting Diode,OLED)由于其主动发光.响应快.高亮度.全视角.直流 ...
- android studio 更新失败解决办法
在andriod studio目录下找到studio.exe.vmoptions这个文件,用记事本打开,在后面加上 -Djava.net.preferIPv4Stack=true -Didea.upd ...
- Vector类
/* * Vector的特有功能 * * Vector出现较早,比集合更早出现 * * 1:添加功能 * public void addElement(Object obj);//用add()替代 * ...
- UVa 10684 - The jackpot
题目大意:给一个序列,求最大连续和. 用sum[i]表示前i个元素之和,那么以第i个元素结尾的最大连续和就是sum[i]-sum[j] (j<i)的最大值,也就是找前i-1个位置sum[]的最小 ...
- Linux安装Tomcat外部不能访问
Linux安装Tomcat后本地可以正常访问,可是这时Tomcat还不能被外界访问需要在Linux默认防护墙上打开8080端口 打开 /etc/sysconfig/iptables [root@loc ...
- iOS 之 时间格式与字符串转换
这个知识点涉及到三个类:NSDate.NSString,另外是一个最重要的类NSDateFormatter.它起到格式转换的作用,至于方法查看头文件就好了.时间格式注意下:yyyyMMddHHmmss
- IM 融云 之 初始化及登录
融云是没有用户体系的,用户登录需要获取token,测试时可以使用网站提供的接口,但是正式是要通过服务器调用专门的接口从融云获取的.并且我没看到融云的重连机制,不知道是否已在SDK内部实现. 现在看来, ...