【Codeforces Round 370 (Div 2) B】【简单贪心】Memory and Trident

99 篇文章 0 订阅
84 篇文章 5 订阅
B. Memory and Trident
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 1e5 + 10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
char s[N];
int n;
int main()
{
	while (~scanf("%s", s))
	{
		n = strlen(s);
		if (n & 1)
		{
			puts("-1");
			continue;
		}
		int x = 0;
		int y = 0;
		for (int i = 0; i < n; ++i)
		{
			if (s[i] == 'R')++x;
			else if (s[i] == 'L')--x;
			else if (s[i] == 'D')++y;
			else if (s[i] == 'U')--y;
		}
		x = abs(x);
		y = abs(y);
		printf("%d\n", x + y >> 1);
	}
	return 0;
}
/*
【题意】
有一个长度为n(1e5)的指令,
按照指令,我们从(0,0)出发,进行一系列上下左右的行走。
问你,最少要修改多少数量的指令,才可以使得最后我们停留在(0,0)点

【类型】
贪心

【分析】
首先n必须为偶数
我们求出最终的横纵坐标位移y和x(求绝对值之后),最终答案就是(y+x)/2

因为我们至少需要(y+x)/2次修改(显然)
同时我们一定可以在(y+x)/2次修改内完成目标

以上。

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值