Memory and Trident(CodeForces 712B)
Posted baiyi-destroyer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Memory and Trident(CodeForces 712B)相关的知识,希望对你有一定的参考价值。
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
RRU
-1
UDUR
1
RUUR
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 ",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 }
以上是关于Memory and Trident(CodeForces 712B)的主要内容,如果未能解决你的问题,请参考以下文章
codeforces 712B. Memory and Trident
Codeforces 712B. Memory and Trident
CodeForces 712B Memory and Trident (水题,暴力)