codeforces 712B. Memory and Trident
题目链接:http://codeforces.com/problemset/problem/712/B
题目大意:
给出一个字符串(由'U''D''L''R'),分别是向上、向下、向左、向右一个单位,问修改若干字符,可以回到原点。回不到原点输出 -1,可以则输出最少修改的步数。
解题思路:
如果是奇数步,则不可以回到原点 直接输出-1;
否则:分别统计出向各个方向的步数,求出 abs(U-D)+abs(L-R) 回不到原点多余的步数。然后让 剩余的步数/2 修改一处可以保证两个状态OK.(例如:DRUR=>DRUL,修改了最后一个R为L,不仅保证了RL,而且取消了了本身对应的L,故修改一处即可)。
AC Code:
#include<bits/stdc++.h>
using namespace std; int main()
{
string s;
int a[];
while(cin>>s)
{
memset(a,,sizeof(a));
for(int i=; i<s.size(); i++)
{
if(s[i]=='L')a[]++;
if(s[i]=='R')a[]++;
if(s[i]=='U')a[]++;
if(s[i]=='D')a[]++;
}
if(s.size()%)
printf("-1\n");
else printf("%d\n",(abs(a[]-a[])+abs(a[]-a[]))/);
}
return ;
}
codeforces 712B. Memory and Trident的更多相关文章
- CodeForces 712B Memory and Trident (水题,暴力)
题意:给定一个序列表示飞机要向哪个方向飞一个单位,让你改最少的方向,使得回到原点. 析:一个很简单的题,把最后的位置记录一下,然后要改的就是横坐标和纵坐标绝对值之和的一半. 代码如下: #pragma ...
- Codeforces 712B
B. Memory and Trident time limit per test:2 seconds memory limit per test:256 megabytes input:standa ...
- codeforces 712B B. Memory and Trident(水题)
题目链接: B. Memory and Trident time limit per test 2 seconds memory limit per test 256 megabytes input ...
- 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) B. Memory and Trident 水题
B. Memory and Trident 题目连接: http://codeforces.com/contest/712/problem/B Description Memory is perfor ...
- Memory and Trident(CodeForces 712B)
Description Memory is performing a walk on the two-dimensional plane, starting at the origin. He is ...
- codeforces 712A. Memory and Crow
题目链接:http://codeforces.com/problemset/problem/712/A 题目大意: 给你一个数字系列,求其满足条件的一个序列. 条件为: ai = bi - bi + ...
- Codeforces 712E Memory and Casinos
Description There are n casinos lined in a row. If Memory plays at casino \(i\), he has probability ...
- Codeforces 712C Memory and De-Evolution
Description Memory is now interested in the de-evolution of objects, specifically triangles. He star ...
随机推荐
- php csv导出
/** * 下载csv * @param unknown $orders_id * @param unknown $orders_date_start * @param unknown $orders ...
- java实现顺序栈
public class MyStack{ Object[] data; int top; int maxSize; public MyStack(int maxSize) { this.maxSiz ...
- spring mvc mybatis 搭建 配置文件信息
参考地址:http://blog.csdn.net/fox_lht/article/details/16952683 shiro集成:http://www.cnblogs.com/miskis/p/5 ...
- 检测当前网段哪些IP是在线的
[root@storage ~]# cat ping.ip #!/bin/bashfor ip in `seq 1 255` do { ping -c 2 192.168.220 ...
- [转]Java使用commons-dbcp2.0
原文地址:http://blog.csdn.net/jiutianhe/article/details/39670817 dbcp 是 apache 上的一个 java 连接池项目,也是 tomcat ...
- C#微信开发之旅(二):基础类之HttpClientHelper(更新:SSL安全策略)
public class HttpClientHelper 2 { 3 /// <summary> 4 /// get请求 5 ...
- 纯JSP实现简单登录跳转
1.JSP介绍 JSP即Java Server Pages,JSP技术使用Java编程语言编写类XML的tags和scriptlets,来封装产生动态网页的处理逻辑.网页还能通过tags和script ...
- 【matlab】设定函数默认参数
C++/java/python系列的语言,函数可以有默认值,通常类似如下的形式: funtion_name (param1, param2=default_value, ...) 到了matlab下发 ...
- 【BZOJ-1952】城市规划 [坑题] 仙人掌DP + 最大点权独立集(改)
1952: [Sdoi2010]城市规划 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 73 Solved: 23[Submit][Status][ ...
- 【bzoj1911】 Apio2010—特别行动队
http://www.lydsy.com/JudgeOnline/problem.php?id=1911 (题目链接) 题意 给出一个序列,将序列分成连续的几段,每段的价值为a*s*s+b*s+c,其 ...