白昼夢 / Daydream(AtCoder-2158)
Problem Description
You are given a string S consisting of lowercase English letters. Another string T is initially empty. Determine whether it is possible to obtain S=T by performing the following operation an arbitrary number of times:
Append one of the following at the end of T: dream, dreamer, erase and eraser.
Constraints
- 1≦|S|≦105
- S consists of lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
If it is possible to obtain S=T, print YES. Otherwise, print NO.
Example
Sample Input 1
erasedream
Sample Output 1
YES
Append erase and dream at the end of T in this order, to obtain S=T.Sample Input 2
dreameraser
Sample Output 2
YES
Append dream and eraser at the end of T in this order, to obtain S=T.Sample Input 3
dreamerer
Sample Output 3
NO
题意:给一个字符串 S 与一个空串 T,现在可以在 T 的末尾添加以下任何一项:dream、dreamer、erase、eraser,问能否添加任意次后使得 S=T
思路:模拟即可
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 100000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;
char str[N];
char Dream[5]= {'d','r','e','a','m'};
char Erase[5]= {'e','r','a','s','e'};
int main() {
scanf("%s",str);
int len=strlen(str);
bool flag=true;
for(int i=0; i<len; i++) {
if(str[i]=='d') {
int j=i;
for(; j<=i+4; j++) {
if(j>=len||str[j]!=Dream[j-i]) {//是否为dream或dreamer
flag=false;
break;
}
}
if(j==i+5) {
if(str[j]=='e'&&str[j+1]=='r'&&str[j+2]!='a')//dreamer,指针前移
i=j+1;
else//dream,指针后移
i=j-1;
}
}
else if(str[i]=='e') {
int j=i;
for(; j<=i+4; j++) {
if(j>=len||str[j]!=Erase[j-i]) { //是否为erase或eraser
flag=false;
break;
}
}
if(j==i+5) {
if(str[j]=='r')//eraser,指针前移
i=j;
else//erase,指针后移
i=j-1;
}
}
else
flag=false;
if(!flag)
break;
}
if(flag)
printf("YES\n");
else
printf("NO\n");
return 0;
}

浙公网安备 33010602011771号