题目链接: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. android file path

    问题 出现的异常为:java.lang.IllegalArgumentException: File /mnt/sdcard/crazyit.bin contains a pathseparator. ...

  2. FTP 的搭建过程和遇到的问题

    http://linux.it.net.cn/e/server/ftp/2015/0227/13554.htmlhttps://mp.weixin.qq.com/s?__biz=MzA3OTgyMDc ...

  3. 6.7-3将数组arr中索引值为2的元素替换为“bb”

    package shuzu; import java.util.Arrays; public class TH { public static void main(String[] args) { / ...

  4. iOS开发小技巧--即时通讯项目:消息发送框(UITextView)高度的变化; 以及UITextView光标复位的小技巧

    1.即时通讯项目中输入框(UITextView)跟随输入文字的增多,高度变化的实现 最主要的方法就是监听UITextView的文字变化的方法- (void)textViewDidChange:(UIT ...

  5. 【BZOJ 2038】【2009 国家集训队】小Z的袜子(hose) 分块+莫队

    $SDOI2016Day-1$临时抱佛脚学习一下莫队算法$233$ 我预感到自己省选要爆0hhh #include<cmath> #include<cstdio> #inclu ...

  6. 线性回归&&code

    # -*- coding: utf-8 -*- import numpy as np import matplotlib.pyplot as plt from certifi import __mai ...

  7. shell截取字符串的方法

    参考文献: linux中shell截取字符串方法总结 [Linux]如何在Shell脚本中计算字符串长度? 截取字符串的方法一共有八种,主要为以下方法 shell中截取字符串的方法有很多中, ${ex ...

  8. C语言与套接字

    我们已经知道如何使用I/O与文件通信,还知道了如何让同一计算机上的两个进程进行通信,这篇文章将创建具有服务器和客户端功能的程序 互联网中大部分的底层网络代码都是用C语言写的. 网络程序通常有两部分组成 ...

  9. asp.net mvc web api 可跨域方法

    1.直接修改 web.config ,不过这是针对所有 Action. <location path="Sample.txt"> <system.webServe ...

  10. jquery插件-表单提交插件-jQuery.Form

    1.介绍 JQuery Form插件是一款强大的Ajax表单提交插件,可以简单方便的实现让我们的表单 由传统的提交方式转换成Ajax无刷新提交! 他提供了两个核心的方法ajaxForm以及ajaxSu ...