Memory and Trident_t - memory and trident-程序员宅基地

技术标签: C语言  # C语言  

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;
}
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_43272781/article/details/82942027

智能推荐

CrossOver软件2022可以使苹果MAC电脑运行Windows软件应用_crossover mac-程序员宅基地

文章浏览阅读952次。CrossOver软件2022可以使苹果MAC电脑运行Windows软件应用_crossover mac

【论文翻译】VGG网络论文中英对照翻译--(very deep convolutional networks for large-scale image  recognition)_vgg论文翻译-程序员宅基地

文章浏览阅读4.8k次,点赞6次,收藏39次。【开始时间】2018.09.23【完成时间】2018.09.24【论文翻译】VGG网络论文中英对照翻译--(very deep convolutional networks for large-scale image recognition)【中文译名】 用于大规模图像识别的甚深卷积网络【论文链接】https://arxiv.org/abs/1409.1556目录 本文概..._vgg论文翻译

PythonStock(14):使用pandas 批量处理股票数据,批量计算股票指标_pandas_ta dmi-程序员宅基地

文章浏览阅读1w次,点赞5次,收藏49次。前言使用Python开发一个股票项目。 项目地址: https://github.com/pythonstock/stock 相关资料: http://blog.csdn.net/freewebsys/article/details/78294566 主要使用开发语言是python。 使用的lib库是pandas,tushare,TensorFlow,tornado等。本文的_pandas_ta dmi

如何给Jupyter添加python库路径_jupyter 增加库路径-程序员宅基地

文章浏览阅读9.4k次,点赞6次,收藏18次。【时间】2018.12.13【题目】如何给Jupyter添加python库路径转载链接:http://www.cnblogs.com/jisongxie/p/9892660.html概述python 在cmd中import模块成功,但是在jupyter notebook中出现No module xxx found的问题,这是没有将python库路径添加到环境变量中造成的,解决方法是..._jupyter 增加库路径

FatFs 文件系统使用_fatfs下载-程序员宅基地

文章浏览阅读4.7k次,点赞3次,收藏37次。FatFs 文件系统使用1.简介FatFs是面向小型嵌入式系统的一种通用的 FAT 文件系统。它完全是由 ANSIC 语言编写并且完全独立于底层的 I/O 介质。因此它可以很容易地不加修改地移植到其他的处理器当中,如 8051、PIC、 AVR、 SH、 Z80、 H8、 ARM 等。 FatFs 支持 FAT12、 FAT16、 FAT32 等格式,所以我们利用写好的 SPI Flash 芯片驱动或者SD驱动,把 FatFs 文件系统代码移植到工程之中,就可以利用文件系统的各种函数,对 SPI Fla_fatfs下载

Delphi常用代码汇总_delphi软件头部代码-程序员宅基地

文章浏览阅读2k次。◇[DELPHI]网络邻居复制文件uses shellapi;copyfile(pchar('newfile.txt'),pchar('//computername/direction/targer.txt'),false);◇[DELPHI]产生鼠标拖动效果通过MouseMove事件、DragOver事件、EndDrag事件实现,例如在PANEL上的LABEL:var x_delphi软件头部代码

随便推点

前端用CryptoJS类库的AES加密,后端解密不出来_前端aes加密后端解密报错-程序员宅基地

文章浏览阅读9.2k次。前端使用CryptoJS类库,进行AES加密提交到后台之后,后台解密失败,报错Given final block not properly padded经过一番搜索,终于有了解决方案!由于后台java使用AES加密使用的是AES/EBC/PKCS5Padding进行初始化的,1、首先var key = CryptoJS.enc.Utf8.parse(key); 2、由于后端用的P_前端aes加密后端解密报错

Git Hooks 使用说明_run git hooks-程序员宅基地

文章浏览阅读3.8k次,点赞2次,收藏12次。官方文档 Git - githooks DocumentationLearn how to improve your Git skillsGit 除了版本控制,还有一些其他的高级功能,其中之一就是Git Hooks,明面意思就是钩子,可以类比于钩子函数。简单说就是Hook会在做某些事情的前后被钩子钩走去一些额外事情,那么这些额外事情我们可以用作于检查、测试和部署等操作。在 Git 中有许多的事件(commit、push 等等),每个事件也是对应了有不同的钩子的(如 commit 前,commit ._run git hooks

当老师计算机要学哪些科目,如果可以当老师,哪门科目的老师最没有压力,工作很轻松?...-程序员宅基地

文章浏览阅读1.2k次。今年高考结束,不少市重点高中也送走了一批学校的尖子生。有很多老教师,已经从教十几年了。甚至有的教师教了几十年的书,直到近几年才因为年龄的原因不得不退休。记得我当年高中的时候,化学老师就因为得了膀胱癌而提前退休,当时我们还是高二,结果我们高二的教学担子就全部落在了一位正在教高三的化学老师身上。高中老师所需要担负的责任也非常多,尤其是高中的班主任,更要管理到几乎每一个学生。不仅高中如此,现在的初中生也..._计算机专业当老师可以教哪些科目

问题解决之-Spring Data开发手册|Java持久化API(JPA)需要了解到什么程度呢?_api问题怎么解决-程序员宅基地

文章浏览阅读115次。JPA,Java Persistence API是Sun官方提出的Java持久化规范。它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据。它的出现主要是为了简化现有的持久化开发工作和整合ORM技术ORM:通过使用描述对象和数据库之间映射的元数据,将程序中的对象自动持久化到关系数据库中。本质就是将数据从一种形式转换到另外一种形式。同时也结束了Hibernate、TopLink等ORM框架各自为营的局面。JPA充分吸收了Hibernate、TopLink等ORM框架的基础上发展起_api问题怎么解决

yuv420数据快速裁剪_yuv422 横向裁剪 cpu占用率-程序员宅基地

文章浏览阅读2.2k次,点赞3次,收藏2次。yuv420数据快速裁剪 最近碰到一个项目需求,这个项目是两年多年已经量产的项目,这是一个车载后视镜项目,当时配置的后视摄像头是确定的,也就是说这个后视摄像头是厂家定制出去的,当时研发在做倒车显示的时候,显示的画面是完整的裸数据,没有裁剪过,也就是后视摄像头出来的CVBS数据是N制式的时候就是720X480的原始数据来显示的。 但是最近又接到一个需求,就是这个_yuv422 横向裁剪 cpu占用率

874复习第四天-习题7编程题_.#include<stdio.h>#definen6voidmain(){inti,a[n]={2-程序员宅基地

文章浏览阅读832次,点赞2次,收藏9次。编程实现从键盘任意输入20个整数,计算其中所有非负数之和//计算20个非负整数之和#include<stdio.h>#include<string.h>int main(){ int i,sum=0; int a[20]; for(i=0;i<20;i++) { scanf("%d",&a[i]); } for(i=0;i<20;i++) { if(a[i]>=0) { sum=sum+a[i]; } } p_.#include#definen6voidmain(){inti,a[n]={2,5,8,10,15,21};for(i=0

推荐文章

热门文章

相关标签