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 ...
随机推荐
- Java对象的访问
对象访问在Java语言中无处不在,即使最简单的访问也涉及Java栈.Java堆.方法区这三个重要的内存区域中. 例:Object obj = new Object(); Object obj ...
- maven2-snapshot快照库和release发布库的应用
在项目中应用snapshot和release库,应用snapshot和release库达到不同环境下发布不同的版本的目的,首先看一个pom文件的定义: <project> <mode ...
- iis Server Error in '/' Application
1.开始-运行-cmd-输入cd C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727-回车-aspnet_regiis.exe -i 回车 2.如果不是检查链接 ...
- 多线程中的synchronized
synchronized是Java中的关键字,是一种同步锁.它修饰的对象有以下几种: 1. 修饰一个代码块,被修饰的代码块称为同步语句块,其作用的范围是大括号{}括起来的代码,作用的对象是调用这个代码 ...
- 递归O(NlgN)求解逆序数
导言 第一次了解到逆序数是在高等代数课程上.当时想计算一个数列的逆序数直觉就是用两重循环O(n^2)暴力求解.现在渐渐对归并算法有了一定的认识,因此决定自己用C++代码小试牛刀. 逆序数简介 由自然数 ...
- gene_abundance_estimation
/home/liuhui/bin/trinityrnaseq_r20140413p1/util/support_scripts/get_Trinity_gene_to_trans_map.pl Tri ...
- css-文字和图片在容器内垂直居中实测。方法来源张鑫旭博客。
方法一:在文字或者图片后加入一个width为0的inline-block元素,将文字inline-block后vertical-align:middle.图片同理 多行文字居中:(有些浏览器会出问题, ...
- Ubuntu下C/C++man手册安装方法及使用方法
C++在线文档: http://www.cplusplus.com/reference/ https://msdn.microsoft.com/zh-cn/library/aa187916.aspx ...
- app 摇一摇功能
1.重写canBecomeFirstResponder ,并返回YES -(BOOL)canBecomeFirstResponder{ return YES; } 2.重写UIResponder的三个 ...
- Discuz! X2.5 /source/class/helper/helper_seo.php Remote Code Execution Vul
catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 SEO模块中的preg_replace+修正符e+双引号引发的远程代码执 ...