B. Memory and Trident
来源:互联网 发布:java cache类 编辑:程序博客网 时间:2024/05/13 02:15
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.
解题说明:此题可以看成是一道字符串题,要求回到起点其实就是要求出现数目相同的左右字母对以及上下字母对,求出每对中不匹配的数目,然后统计需要修改的数目。
#include<cstdio>#include <cstring>#include<cmath>#include<iostream>#include<algorithm>#include<vector>#include <map>using namespace std;char s[200002];int main() { int u,d,r,l,i,k=4,m=0,n=0; u=0;d=0;l=0;r=0; for (i=0;i<200002;i=i+2) { scanf ("%c",&s[i]); if (s[i] == 10) { break; } if (s[i] == 85 ) { u++; } if (s[i] == 68 ) { d++; } if (s[i] == 82 ) { r++; } if (s[i] == 76) {l++; } } if (i%4 == 0) { m=abs(r-l); n=abs(u-d); m=m+n; m=m/2; printf ("%d",m ); } else { printf ("-1\n"); } return 0;}
- B. Memory and Trident
- codeforces B. Memory and Trident
- CodeForces 712B Memory and Trident
- CF #370 div2 B. Memory and Trident
- codeforces 712B Memory and Trident
- Codeforces-712B-Memory and Trident
- Codeforces Problem 712B Memory and Trident(implementation)
- Codeforces #370 (Div.2) B. Memory and Trident【思维】
- Codeforces #370 (Div.2) B. Memory and Trident【思维】
- Codeforces 712B Memory and Trident 简单贪心
- CodeForces-712B. Memory and Trident(模拟)
- 【Codeforces Round 370 (Div 2) B】【简单贪心】Memory and Trident
- Codeforces 370D - Memory and Trident(模拟)
- trident
- PROCESS MEMORY AND MEMORY CORRUPTIONS
- B - Memory Manager
- B - Memory Manager
- Memory Limit Exceed B
- Struts2对EL的改变和OGNL
- 【49.23%】【hdu 1828】Picture
- 2016年Java面试题整理
- NOIP 2015模拟赛[nodgd题]题解&总结
- HDU 2602 Bone Collector(简单01背包)
- B. Memory and Trident
- Android开发常用网址
- Java并发编程 - 基本概念
- ubuntu16.04主题美化和软件推荐
- [HackerRank 101 Hack 42]Array Pairs
- 西邮Linux兴趣小组第一次技术分享会
- Spring如何处理线程并发问题
- centOS7安装java以及连接外网问题(Ping不通)
- Linux C 基础1