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. PHP 设计模式 单例模式 工厂模式 注册模式

    1.工厂模式,工厂方法或者类生成对象,而不是在代码中直接new 2.单例模式,使某个类的对象仅允许创建一个 3.注册模式,全局共享和交换对象 项目文件目录 入口文件 index.php <?ph ...

  2. HTTP Methods

    简介 HTTP 定义了一组请求方法,以表明要对给定资源执行的操作.指示针对给定资源要执行的期望动作, 虽然他们也可以是名词,但这些请求方法有时被称为HTTP动词.每一个请求方法都实现了不同的语义,但一 ...

  3. source insigh安装使用

    下载和安装: 最好去官网下载(http://www.sourceinsight.com/),最新版本是3.5. 第一次去六维下载了sourceinsight,免安装,但是打开后发现界面没有任何窗口,全 ...

  4. expect学习笔记及实例详解【转】

    1. expect是基于tcl演变而来的,所以很多语法和tcl类似,基本的语法如下所示:1.1 首行加上/usr/bin/expect1.2 spawn: 后面加上需要执行的shell命令,比如说sp ...

  5. Oracle 数据库和监听器开机自启动两种实现方法

    数据库和监听器开机自启动   编辑oratab文件: 修改:orcl:/u01/app/oracle/product/11.2.0/db_1:N            orcl:/u01/app/or ...

  6. java垃圾回收的回收器

    回收器的种类: --串行(–XX:+UseSerialGC ) Out ofBox算法,年轻代串行复制,年老代串行标记整理,主要用于桌面应用 --并行(–XX:+UseParallelGC ) 年轻代 ...

  7. (转)js函数前加分号和感叹号是什么意思?有什么用?

    转载地址:https://www.cnblogs.com/mq0036/p/4605255.html 一般看JQuery插件里的写法是这样的 (function($) { //... })(jQuer ...

  8. 【转载】webstorm忽略node_modules目录

    转载自:http://www.cnblogs.com/chengwb/p/6183440.html 我在使用了cnpm后node_modules之前的层级目录变成了同一级目录,所以目录很多,造成web ...

  9. 浅谈JavaScript DDOS 攻击原理与防御

    前言 DDoS(又名"分布式拒绝服务")攻击历史由来已久,但却被黑客广泛应用.我们可以这样定义典型的DDoS攻击:攻击者指使大量主机向服务器发送数据,直到超出处理能力进而无暇处理正 ...

  10. Vue(SPA) WebPack模块化打包、SEO优化(Vue SSR服务端同构直出)、全浏览器兼容完整解决方案

    白驹过隙,时光荏苒 大概去年这个时候写了angular 结合webpack的一套前端方案,今年此时祭出vue2结合webpack的一套前端方案. 明年的这个时候我又是在做什么... 读在最前面: 1. ...