Description
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.

Input
The first and only line contains the string s (1 ≤ |s| ≤ 100 000) — the instructions Memory is given.

Output
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.

Examples
Input
RRU
Output
-1
Input
UDUR
Output
1
Input
RUUR
Output
2
Note
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */int main(int argc, char *argv[]) {char s[100010];scanf("%s",&s);int i;int count[4];count[0]=0;count[1]=0;count[2]=0;count[3]=0;for(i=0;i<strlen(s);i++){switch(s[i]){case 'U':count[0]++;break;case 'D':count[1]++;break;case 'R':count[2]++;break;case 'L':count[3]++;break;}}if((count[0]+count[1]+count[3]+count[2])%2!=0)printf("-1\n");else{printf("%d\n",(abs(count[0]-count[1])+abs(count[3]-count[2]))/2);   }return 0;
}

Memory and Trident相关推荐

  1. T - Memory and Trident CodeForces - 712B( 注意:* ++ = 的优先级

    题意:有四种命令:U代表上移一个单位,D代表下移一个单位,R代表右移一个单位,L代表左移一个单位. 现在给出一串命令,问怎样修改命令中的任意一条命令,使得命令结束后重新返回原点,并且修改的步数最少. ...

  2. Codeforces Round #370 (Div. 2)

    A - Memory and Crow 这题我没看题意,看了样例猜了一下就AC了,题目好像还挺复杂的. #include<bits/stdc++.h> using namespace st ...

  3. ECJTUACM16 Winter vacation training #4 题解源码

    https://vjudge.net/contest/149692#overview 这周一VJ比赛,题解&源码已完成! A.................................. ...

  4. 2016区域赛前冲刺训练

    UPD 2016.10.23 shift-and (2题) Codeforces 训练 现在已经完成了: 191 [Codeforces Round #377] (6/6) Div 2 A Buy a ...

  5. Storm源码分析之四: Trident源码分析

    Storm源码分析之四: Trident源码分析 @(STORM)[storm] Storm源码分析之四 Trident源码分析 一概述 0小结 1简介 2关键类 1Spout的创建 2spout的消 ...

  6. java操作storm_Storm Trident

    Trident是Storm的延伸.像Storm一样,Trident也是由Twitter开发的.开发Trident的主要原因是在Storm之上提供高级抽象以及有状态流处理和低延迟分布式查询. Tride ...

  7. Cache Memory技术示例

    Cache Memory技术示例 为什么需要cache?如何判断一个数据在cache中是否命中?cache的种类有哪些,区别是什么? 为什么需要cache memory 先思考第一个问题:程序是如何运 ...

  8. TensorFlow XLA优化与Memory

    TensorFlow XLA优化与Memory XLA概述 XLA(加速线性代数)是用于优化TensorFlow计算的线性代数的域特定编译器.结果是在服务器与移动平台上的速度,内存使用率与可移植性得到 ...

  9. Cache 与Memory架构及数据交互

    Cache 与Memory架构及数据交互

最新文章

  1. 图像处理之简单脸谱检测算法
  2. 新书发布《每天5分钟玩转Docker容器技术》
  3. ITK:将itk :: CovariantVectors的点积
  4. 2016云计算大数据安全论坛即将在北京召开
  5. 为什么我们要阅读源码?
  6. NHibernate剖析:Mapping篇之Mapping-By-Code(2):运用ModelMapper
  7. Linux下MongoDB非正常关闭启动异常解决方法
  8. CAT扫盲:最常用的CAT翻译软件
  9. 在家如何下载nature中的文献
  10. 伍斯特理工学院计算机,世界排名领先,伍斯特理工学院到底有多厉害?
  11. chrome 您的浏览器禁用了Javascript
  12. LR性能测试Web Page Breakdown分析篇
  13. 算法之路-圆柱体面积
  14. 【渗透测试】锐捷网络-EWEB网管系统易网关批量GetShell
  15. 【深度学习】图像去雾,去噪里常用的相似评价指标:PSNR(峰值信噪比) SSIM(结构相似度)MSE(均方误差)
  16. 144显示器只有60_Win10系统下144hz显示器刷新率只显示60hz如何解决
  17. 学习嵌入式linux与ARM开发板入门方法(一)
  18. mysql block是什么_数据库中block代表什么意思?
  19. HP(惠普)大中华区总裁孙振耀退休感言
  20. 微信新BUG曝光:好友偷偷删了你,这样就能查出来

热门文章

  1. javascript map 排序_数据结构-JavaScript字典结构的编程实现
  2. 天津大学计算机科学学院夏令营,天津大学计算机科学与技术学院(专业学位)计算机技术保研夏令营...
  3. dell主板40针开机针脚_电脑主板各种接口的介绍最新版
  4. mysql帐户权限replace_mysql 用户管理和权限设置
  5. MyEclipse、eclipse代码自动补全
  6. oracle经典博客,Oracle启动步骤【经典博客】
  7. dev chartcontrol获取x y轴的值_终于,奔驰强势接手了腾势X
  8. 六十一、Vue中父子组件传值和组件参数校验
  9. 做时间序列预测有必要用深度学习吗?梯度提升回归树媲美甚至超越多个DNN模型...
  10. 零样本性能超越GPT-3!谷歌提出1370亿参数自回归语言模型