B. Memory and Trident

题目连接:

http://codeforces.com/contest/712/problem/B

Description

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.

Sample Input

RRU

Sample Output

-1

Hint

题意

修改最少的字符,使得这个机器人走回原点

题解:

从整体考虑,只要是偶数长度,那么一定有解。

解就是L和R的绝对值+U和D的绝对值之差除以2,这个比较显然

代码

#include<bits/stdc++.h>
using namespace std; int a[4];
int main()
{
string s;
cin>>s;
for(int i=0;i<s.size();i++)
{
if(s[i]=='L')a[0]++;
if(s[i]=='R')a[1]++;
if(s[i]=='U')a[2]++;
if(s[i]=='D')a[3]++;
}
if(s.size()%2)
{
printf("-1\n");
return 0;
}
printf("%d\n",(abs(a[0]-a[1])+abs(a[2]-a[3]))/2);
}

Codeforces Round #370 (Div. 2) B. Memory and Trident 水题的更多相关文章

  1. Codeforces Round #370 (Div. 2) C. Memory and De-Evolution 水题

    C. Memory and De-Evolution 题目连接: http://codeforces.com/contest/712/problem/C Description Memory is n ...

  2. Codeforces Round #370 (Div. 2) A. Memory and Crow 水题

    A. Memory and Crow 题目连接: http://codeforces.com/contest/712/problem/A Description There are n integer ...

  3. Codeforces Round #370 (Div. 2)B. Memory and Trident

    地址:http://codeforces.com/problemset/problem/712/B 题目: B. Memory and Trident time limit per test 2 se ...

  4. Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  5. Codeforces Round #322 (Div. 2) A. Vasya the Hipster 水题

    A. Vasya the Hipster Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/p ...

  6. Codeforces Round #290 (Div. 2) A. Fox And Snake 水题

    A. Fox And Snake 题目连接: http://codeforces.com/contest/510/problem/A Description Fox Ciel starts to le ...

  7. Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题

    B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...

  8. Codeforces Round #368 (Div. 2) A. Brain's Photos 水题

    A. Brain's Photos 题目连接: http://www.codeforces.com/contest/707/problem/A Description Small, but very ...

  9. Codeforces Round #359 (Div. 2) A. Free Ice Cream 水题

    A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adve ...

随机推荐

  1. 何凯文每日一句打卡||DAY12

  2. MAC Book 共享网络连接

    CHENYILONG Blog MAC Book 共享网络连接 MAC Book 共享网络连接 MAC比Windows共享连接要方便很多,只需要以下两步操作: 1.打开系统偏好设置,选择共享 2.选择 ...

  3. iOS 根据生日得到生肖,星座,年龄的算法

    根据用户生日,得到相应的年龄,星座和生肖.有的项目中可能会用到,贴出来共享. 得到年龄,这个很简单了: - (void)getAgeWith:(NSDate*)birthday{ //日历 NSCal ...

  4. ifconfig,netstat command not found

    当CentOS7进行最小化安装时,有很多工具包是没有的. [root@vultr ~]# ifconfig -bash: ifconfig: command not found [root@vultr ...

  5. IDEA配置文件的配置文件配置

    IDEA配置文件的配置文件配置: 路径 /Applications/IntelliJ IDEA 3.app/Contents/bin/idea.vmoptions (/IntelliJ IDEA 3. ...

  6. nginx:在centos中自启动

    参考网址:http://www.jb51.net/article/120545.htm # vi /etc/init.d/nginx #!/bin/sh # Name:nginx4comex # ng ...

  7. java 组合接口时的名字冲突

    重载方法仅通过返回类型是区分不开的,在打算组合的不同接口中,尽量使用不同的方法名,否则可能导致混乱 //: interfaces/InterfaceCollision.java package obj ...

  8. 解决Python3 pip list 红色DEPRECATION

    解决Python3 pip list 红色DEPRECATION 打开文件扩展名和隐藏的项目 找到ProgramData,在该目录下创建pip文件夹,在pip里面创建pip.ini  在pip.ini ...

  9. Kafka 集群配置SASL+ACL

    一.简介 在Kafka0.9版本之前,Kafka集群时没有安全机制的.Kafka Client应用可以通过连接Zookeeper地址,例如zk1:2181:zk2:2181,zk3:2181等.来获取 ...

  10. 采用MiniProfiler监控EF与.NET MVC项目(Entity Framework 延伸系列1)(转)

    前言 Entity Framework 延伸系列目录 今天来说说EF与MVC项目的性能检测和监控 首先,先介绍一下今天我们使用的工具吧. MiniProfiler~ 这个东西的介绍如下: MVC Mi ...