题目链接: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的更多相关文章

  1. CodeForces 712B Memory and Trident (水题,暴力)

    题意:给定一个序列表示飞机要向哪个方向飞一个单位,让你改最少的方向,使得回到原点. 析:一个很简单的题,把最后的位置记录一下,然后要改的就是横坐标和纵坐标绝对值之和的一半. 代码如下: #pragma ...

  2. Codeforces 712B

    B. Memory and Trident time limit per test:2 seconds memory limit per test:256 megabytes input:standa ...

  3. codeforces 712B B. Memory and Trident(水题)

    题目链接: B. Memory and Trident time limit per test 2 seconds memory limit per test 256 megabytes input ...

  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) B. Memory and Trident 水题

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

  6. Memory and Trident(CodeForces 712B)

    Description Memory is performing a walk on the two-dimensional plane, starting at the origin. He is ...

  7. codeforces 712A. Memory and Crow

    题目链接:http://codeforces.com/problemset/problem/712/A 题目大意: 给你一个数字系列,求其满足条件的一个序列. 条件为: ai = bi - bi +  ...

  8. Codeforces 712E Memory and Casinos

    Description There are n casinos lined in a row. If Memory plays at casino \(i\), he has probability ...

  9. Codeforces 712C Memory and De-Evolution

    Description Memory is now interested in the de-evolution of objects, specifically triangles. He star ...

随机推荐

  1. 各种图(流程图,思维导图,UML,拓扑图,ER图)简介

    来源于:http://www.cnblogs.com/jiqing9006/p/3344221.html 流程图 1.定义:流程图是对过程.算法.流程的一种图像表示,在技术设计.交流及商业简报等领域有 ...

  2. Android:View随手指移动

    View的自动移动,我们可以设置动画,如之类提到的 ViewCompat,Animation. 如何是View随着手指的移动而移动呢? 在onTouch事件实现 @Overridepublic boo ...

  3. js简易日历

    js简易日历中设计的知识点:选项卡切换   数组    innerHTML  连接符 与选项卡的区别:div的个数不同 连接符中需要注意的:(优先级) "abc"+12+3+&qu ...

  4. Maven异常Type Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix

    eclipse maven错误“Project configuration is not up-to-date with pom.xml. Run proje” 导入maven工程后,出现如下错误: ...

  5. 73.Android之SparseArray替代HashMap

    转载:https://liuzhichao.com/p/832.html HashMap是java里比较常用的一个集合类,我比较习惯用来缓存一些处理后的结果.最近在做一个Android项目,在代码中定 ...

  6. 编译hadoop-2.5.2总结

    原文:http://www.cnblogs.com/qigang/p/4194728.html 一.准备的资料 64位linux系统.我使用的是 CentOS JDK 1.7+.注:使用1.7即可,如 ...

  7. Leetcode 400. Nth digits

    解法一: 一个几乎纯数学的解法 numbers:   1,...,9, 10, ..., 99, 100, ... 999, 1000 ,..., 9999, ... # of digits:   9 ...

  8. mvc remote的验证

    1,问题 在mvc验证的时候怎么自定义验证action?比如说验证用户名是否重复. 2.解决方法 通过remote 的特性 第一参数是对应的action 第二个对应的是controller contr ...

  9. Scala: 包对象

    包对象最重要的用途是兼容旧的类库,或者为某些数据类型提供增强版本:一般我们可以将其作为扩展工具方法或数据来使用

  10. Cacheable key collision with DefaultKeyGenerator

    The default is to use the hashcode of each parameter and create another (32-bit) hash code. Obviousl ...