Codeforces Round #370 (Div. 2) 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.
The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.
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.
RRU
-1
UDUR
1
RUUR
2
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的更多相关文章
- Codeforces Round #370 (Div. 2) E. Memory and Casinos (数学&&概率&&线段树)
题目链接: http://codeforces.com/contest/712/problem/E 题目大意: 一条直线上有n格,在第i格有pi的可能性向右走一格,1-pi的可能性向左走一格,有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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #370 (Div. 2) B. Memory and Trident 水题
B. Memory and Trident 题目连接: http://codeforces.com/contest/712/problem/B Description Memory is perfor ...
- 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 ...
- 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+ ...
- Codeforces Round #370 (Div. 2)(简单逻辑,比较水)
C. Memory and De-Evolution time limit per test 2 seconds memory limit per test 256 megabytes input s ...
随机推荐
- C++字符串(String)
字符串的声明: string s; string str="abcdefg"; char ch[]="abcdefg"; //使用string类型初始化另一个s ...
- [原创]java WEB学习笔记74:Struts2 学习之路--自定义拦截器,struts内建的拦截器
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [原创]java WEB学习笔记68:Struts2 学习之路-- 类型转换与复杂属性配合使用
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- [转]JAVA程序员一定知道的优秀第三方库(2016版)
原文地址:http://blog.csdn.net/zxc123e/article/details/51418163 几乎每个程序员都知道要“避免重复发明轮子”的道理——尽可能使用那些优秀的第三方框架 ...
- the serializable class XXX does not declare a static final seriaVersionUID...的问题
关于myeclips提示The serializable class XXX does not declare a static final serialVersionUID field of typ ...
- C#写好的类库dll怎么在别人调用的时候也能看到注释?
菜单 Project -> 'xxxx' Properties -> Build -> Output -> 勾上 XML Documentation file
- sql查询所有表以及表名的模糊查询
--1.查看所有表名:select name from sysobjects where type='U'--2.查找包含用户的表名,可通过以下SQL语句实现, Select * From sysob ...
- 夺命雷公狗mongodb之----mongodb---2---常用命令和技巧
查看有那些数据库: show dbs 切换到那个库: use 库名 use local use还有一个作用就是可以“创建一个数据库” use test 删除数据库: db.dropDatabase( ...
- Elasticsearch DSL语句之连接查询
传统数据库支持的full join(全连接)查询方式. 这种方式在Elasticsearch中使用时非常昂贵的.因此,Elasticsearch提供两种操作可以支持水平扩展 更多内容请参考Elasti ...
- linux-kernel 学习计划
[资料] http://www.ibm.com/developerworks/cn/views/linux/libraryview.jsp http://www.kerneltravel.net/ [ ...