Memory and Trident(CodeForces 712B)

本文探讨了一个在二维平面上行走的问题,初始位置为原点,通过一系列指令(L左、R右、U上、D下)进行移动。目标是通过最少的编辑次数使行走结束后回到原点。文章提供了一种解决方案,并附带了代码实现。

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.

Sample Input

Input
RRU
Output
-1
Input
UDUR
Output
1
Input
RUUR
Output
2

Hint

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.

题解:水题。

代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<cmath>
 7 #include<map>
 8 #include<stack>
 9 #include<vector>
10 #include<queue>
11 #include<set>
12 #include<algorithm>
13 #define max(a,b)   (a>b?a:b)
14 #define min(a,b)   (a<b?a:b)
15 #define swap(a,b)  (a=a+b,b=a-b,a=a-b)
16 #define maxn 320007
17 #define N 100000000
18 #define INF 0x3f3f3f3f
19 #define mod 1000000009
20 #define e  2.718281828459045
21 #define eps 1.0e18
22 #define PI acos(-1)
23 #define lowbit(x) (x&(-x))
24 #define read(x) scanf("%d",&x)
25 #define put(x) printf("%d\n",x)
26 #define memset(x,y) memset(x,y,sizeof(x))
27 #define Debug(x) cout<<x<<" "<<endl
28 #define lson i << 1,l,m
29 #define rson i << 1 | 1,m + 1,r
30 #define ll long long
31 //std::ios::sync_with_stdio(false);
32 //cin.tie(NULL);
33 using namespace std;
34 
35 char a[111111];
36 int b[4];
37 int main()
38 {
39     cin>>a;
40     int l=strlen(a);
41     if(l%2)
42     {
43         cout<<"-1"<<endl;
44         return 0;
45     }
46     for(int i=0;i<l;i++)
47     {
48         if(a[i]=='L')
49             b[0]++;
50         if(a[i]=='R')
51             b[0]--;
52         if(a[i]=='U')
53             b[1]++;
54         if(a[i]=='D')
55             b[1]--;
56     }
57     //cout<<b[0]<<" "<<b[1]<<endl;
58     cout<<(abs(b[0])+abs(b[1]))/2<<endl;
59     return 0;
60 }
View Code

 

转载于:https://www.cnblogs.com/baiyi-destroyer/p/9745339.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值