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.

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.

题意:给你一些方向,可以修改其中的字符(改变方向),让起点和终点相同,当然修改次数最小,不行就是-1

解法:首先奇数是不可能返回的,然后要让起点和终点相同,只要左右上下出现次数相同就好了,那么修改的也就是左右 和 上下 缺少的次数

#include<bits/stdc++.h>
using namespace std;
int MAX=100005;
struct P
{
int x;int y;
}He[1000005];
map<char,int>q;
int main()
{
string s;
cin>>s;
if(s.length()%2)
{
cout<<"-1"<<endl;
}
else
{
for(int i=0;i<s.length();i++)
{
q[s[i]]++;
}
cout<<(abs(q['L']-q['R'])+abs(q['U']-q['D']))/2<<endl;
}
return 0;
}

  

Codeforces Round #370 (Div. 2) B的更多相关文章

  1. Codeforces Round #370 (Div. 2) E. Memory and Casinos (数学&&概率&&线段树)

    题目链接: http://codeforces.com/contest/712/problem/E 题目大意: 一条直线上有n格,在第i格有pi的可能性向右走一格,1-pi的可能性向左走一格,有2中操 ...

  2. Codeforces Round #370 (Div. 2) E. Memory and Casinos 线段树

    E. Memory and Casinos 题目连接: http://codeforces.com/contest/712/problem/E Description There are n casi ...

  3. Codeforces Round #370 (Div. 2)C. Memory and De-Evolution 贪心

    地址:http://codeforces.com/problemset/problem/712/C 题目: C. Memory and De-Evolution time limit per test ...

  4. 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 ...

  5. Codeforces Round #370 (Div. 2) D. Memory and Scores 动态规划

    D. Memory and Scores 题目连接: http://codeforces.com/contest/712/problem/D Description Memory and his fr ...

  6. 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 ...

  7. Codeforces Round #370 (Div. 2) B. Memory and Trident 水题

    B. Memory and Trident 题目连接: http://codeforces.com/contest/712/problem/B Description Memory is perfor ...

  8. 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 ...

  9. Codeforces Round #370(div 2)

    A B C :=w= D:两个人得分互不影响很关键 一种是f[i][j]表示前i轮,分差为j的方案数 明显有f[i][j]=f[i-1][j-2k]+2*f[i-1][j-2k+1]+...+(2k+ ...

  10. Codeforces Round #370 (Div. 2)(简单逻辑,比较水)

    C. Memory and De-Evolution time limit per test 2 seconds memory limit per test 256 megabytes input s ...

随机推荐

  1. poj: 1207

    好吧这题竟然还有先大后小的可能,能不这么恶心下吗.. #include <iostream> #include <stdio.h> #include <string.h& ...

  2. struts.xml什么时候加载

    http://www.cnblogs.com/jiemoxiaodi/p/5621697.html

  3. Android -- 背景虚化

    1,在项目中我们常有这样的需求,就是在个人中心的时候,当用户登录后,要显示用户登陆后的头像,然后背景是用户头像的虚化 ,接下来就来实现一下这个功能,先看一下效果 2,实现起来也挺简单的,没什么难度 , ...

  4. 06---Net基础加强

    字符串特性-池-不可变性 class Program { static void Main(string[] args) { #region 判断两个对象是否是同一个对象 //Person p1 = ...

  5. 夺命雷公狗—angularjs—12—get参数的接收

    我们在实际的开发中get和post的交互都是离不开的,我们先来研究下get参数是如何接收到的.. 而且在实际开发中利用json来进行传递参数也是比较多的,这里我们就以get来接收参数为列.. 先创建一 ...

  6. ASP.NET MVC(三)

    ASP.NET Routing 模块的责任是将传入的浏览器请求映射为特有的MVC controller actions. 请求 URL 当我们不使用路由的时候 请求 http://server/app ...

  7. zw版【转发·台湾nvp系列Delphi例程】HALCON AffineTransImage

    zw版[转发·台湾nvp系列Delphi例程]HALCON AffineTransImage unit Unit1;interfaceuses Windows, Messages, SysUtils, ...

  8. 分数的加减法——C语言初学者代码中的常见错误与瑕疵(12)

    前文链接:分数的加减法——C语言初学者代码中的常见错误与瑕疵(11) 重构 题目的修正 我抛弃了原题中“其中a, b, c, d是一个0-9的整数”这样的前提条件,因为这种限制毫无必要.只假设a, b ...

  9. ckeditor添加插入flv视频的插件

    首发:个人博客,更新&纠错&回复 昨天写在网页中播放flv的博文的时候,想在博文中插入视频,但是发现无法实现.因为用的编辑器是ckeditor,决定自己写个插件插入视频.官方的教程在这 ...

  10. 爱普生 RS330 打印机墨水连供装置墨盒吸墨复位方法

    芯片复位方法: 先按充墨键(墨水灯按键),一下一下按,把墨车按停到右侧换墨盒的位置为止(就是右侧框框正中位置), 全程带电操作,停到换墨盒的位置后再按住芯片复位键(墨盒芯片上面白色的小按键)5秒以上再 ...