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

 #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的更多相关文章

  1. codeforces 712B. Memory and Trident

    题目链接:http://codeforces.com/problemset/problem/712/B 题目大意: 给出一个字符串(由'U''D''L''R'),分别是向上.向下.向左.向右一个单位, ...

  2. codeforces 712B B. Memory and Trident(水题)

    题目链接: B. Memory and Trident time limit per test 2 seconds memory limit per test 256 megabytes input ...

  3. Memory and Trident(CodeForces 712B)

    Description Memory is performing a walk on the two-dimensional plane, starting at the origin. He is ...

  4. CodeForces 712B Memory and Trident (水题,暴力)

    题意:给定一个序列表示飞机要向哪个方向飞一个单位,让你改最少的方向,使得回到原点. 析:一个很简单的题,把最后的位置记录一下,然后要改的就是横坐标和纵坐标绝对值之和的一半. 代码如下: #pragma ...

  5. CSUST 8.4 早训

    ## Problem A A - Memory and Crow CodeForces - 712A 题意: 分析可得bi=ai+ai+1 题解: 分析可得bi=ai+ai+1 C++版本一 #inc ...

  6. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  7. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  8. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  9. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

随机推荐

  1. implemented loader.php

    http://stackoverflow.com/questions/11787176/manage-url-routes-in-own-php-framework This is how i imp ...

  2. 加载window事件

    $(document).ready()和window.onload的区别 发表于 2012-08-29 由 admin 以浏览器装载文档为例,在页面加载完毕后,浏览器会通过JavaScript为DOM ...

  3. HAProxy 7层 负载均衡

    系统 CentOS 5.8 x64 wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.26.tar.gz cd haproxy-1.3.2 ...

  4. Memcache第一篇---基础教程

    Memcache是什么 Memcache是danga.com的一个项目,最早是为 LiveJournal 服务的,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力. 它可以应 ...

  5. Leetcode题1

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  6. mongodb 3.x connect with credential

    package mongoDb; import java.net.UnknownHostException; import java.util.ArrayList; import java.util. ...

  7. 解决KVM中鼠标不同步问题

    VNCViewer中的鼠标走得总是比本地系统中的鼠标要慢,不同步,往往实体机中的鼠标都移出vnc窗口外边了,虚拟机中的鼠标指针还没移到需要点击的位置,操作起来很不方便. 起初的想法也是配置的问题,就按 ...

  8. 如何获得keyhash

    keyhash是由keystore产生的 三个命令解决问题 keytool -exportcert -alias androiddebugkey -keystore debug.keystore &g ...

  9. tomcat 插件

    1. http://www.eclipsetotale.com/tomcatPlugin.html

  10. 【贪心】【堆】Gym -100956D - Greedy Game

    题意:给定n个物品,每个物品对于A和B来说具有不同的价值,记为ai,bi,两人交替取,A先手,A总是贪心地取当前剩下的物品中,对于他价值最高的,如果有多个,则任取一个.问B在最坏情况下,能取到的物品的 ...