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
output
input
output
input
output
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;
}
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;
}
相关文章推荐
- Codeforces Round #370 (Div. 2)B. Memory and Trident
- Codeforces-712B-Memory and Trident
- CF #370 div2 B. Memory and Trident
- Codeforces #370 (Div.2) B. Memory and Trident【思维】
- Codeforces #370 (Div.2) B. Memory and Trident【思维】
- Codeforces Round #370 (Div. 2) B. Memory and Trident 水题
- 【Codeforces Round 370 (Div 2) B】【简单贪心】Memory and Trident
- codeforces B. Memory and Trident
- Memory and Trident
- Codeforces 370D - Memory and Trident(模拟)
- Sample Code on Linux semaphore and shared memory
- Nib Loading and Memory Management
- An Analytical Model for a GPU Architecture with Memory-level and Thread-level Parallelism Awareness
- Linux SNMP OID’s for CPU,Memory and Disk Statistics
- memset and ZeroMemory
- JVM内存泄漏检测与处理(JVM Memory Leak detection and handling)
- Part 2 Processor and Memory
- CodeForces 712 D.Memory and Scores(dp)
- Memory Barrier in Compiler and CPU
- Threads are going to be renewed over time to try and avoid a probable memory leak.