博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Memory and Trident(CodeForces 712B)
阅读量:5107 次
发布时间:2019-06-13

本文共 2074 字,大约阅读时间需要 6 分钟。

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

Input
RRU
Output
-1
Input
UDUR
Output
1
Input
RUUR
Output
2

Hint

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
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #define max(a,b) (a>b?a:b)14 #define min(a,b) (a
>a;40 int l=strlen(a);41 if(l%2)42 {43 cout<<"-1"<
View Code

 

转载于:https://www.cnblogs.com/baiyi-destroyer/p/9745339.html

你可能感兴趣的文章
Python学习--Selenium模块简单介绍(1)
查看>>
AsyncTask实现网络图片的异步加载
查看>>
js中面向对象的封装
查看>>
s3c6410_时钟初始化
查看>>
STL中的常用的vector,map,set,Sort用法
查看>>
常用python机器学习库总结
查看>>
C/C++:.hpp与.h区别
查看>>
upc 9318 Slot Machines
查看>>
http方式接口响应实现步骤
查看>>
[转]Java compiler level does not match解决方法
查看>>
多线程中的Lock小结
查看>>
[算法]和为S的两个数字
查看>>
【Lintcode】104.Merge k Sorted Lists
查看>>
怎样把U盘制成驱动盘?
查看>>
Python多线程,多进程,并行,并发,异步编程
查看>>
配置storeconfigs以及解决 Error 400 on SERVER: Could not autoload active_record
查看>>
git 添加新分支后可能报错及解决方案
查看>>
signalR的集群与负载均衡
查看>>
KEILC51编译问题ERROR L104: MULTIPLE PUBLIC DEFINITIONS重复定义
查看>>
PHP反射类的理解(代码篇)
查看>>