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. python解析minicap

    上篇知道了minicap发送图片的格式,照着官网的app.js代码,改用一个python版的来解析它,适当扩展,可以做个小工具实时显示手机屏幕. 步骤: 一.手机开启minicap服务 adb she ...

  2. 使用渐进式JPEG来提升用户体验

    今天才认识到原来JPEG文件有两种保存方式他们分别是Baseline JPEG(标准型)和Progressive JPEG(渐进式).两种格式有相同尺寸以及图像数据,他们的扩展名也是相同的,唯一的区别 ...

  3. swift3.0之后的Error处理

    在之前的版本中,Swift中Error与OC中NSError没有关系.但是现在两者可以互相强转. 我们看看两者的区别:Error是一个实现Error协议的枚举或者结构体,对外能够获取的具体信息只有ra ...

  4. FFT(Rock Paper Scissors Gym - 101667H)

    题目链接:https://vjudge.net/problem/Gym-101667H 题目大意:首先给你两个字符串,R代表石头,P代表布,S代表剪刀,第一个字符串代表第一个人每一次出的类型,第二个字 ...

  5. lucene删除索引——(五)

    增加在入门程序创建索引中,增删改用IndexWriter. 1.获取IndexWriter的代码 // public IndexWriter getIndexWriter() throws Excep ...

  6. 字符串格式化(百分号&format)

    字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号方式: %[(name)][flags][width].[precision]typecode [  ]:表示 ...

  7. Interval Sum I && II

    Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...

  8. Linux系统的快速启动机制(内核切换) 【转】

    转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26807463&id=4187846 原文地址:Linux系统的 ...

  9. node.js express开发web问题

    1.新建的layout.ejs,在里面使用了<%= title %>,但是在运行时提示title is not defined. 将title改为<%= locals.title % ...

  10. 【hadoop】har://

    来源:http://hadoop.apache.org/docs/r1.0.4/cn/hadoop_archives.html 在hadoop代码中发现了一个很奇怪的路径,由har://开头.经查询, ...