您的位置:首页 > 其它

B. Memory and Trident

2016-11-04 19:42 351 查看
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<cstdio>
#include <cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include <map>
using namespace std;

char s[200002];
int main()
{
int u,d,r,l,i,k=4,m=0,n=0;
u=0;d=0;l=0;r=0;
for (i=0;i<200002;i=i+2)
{
scanf ("%c",&s[i]);
if (s[i] == 10)
{
break;
}
if (s[i] == 85 )
{
u++;
}
if (s[i] == 68 )
{
d++;
}
if (s[i] == 82 )
{
r++;
}
if (s[i] == 76)
{
l++;
}
}
if (i%4 == 0)
{
m=abs(r-l);
n=abs(u-d);
m=m+n;
m=m/2;
printf ("%d",m );
}
else
{
printf ("-1\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: