6491: Daydream

时间:2021-08-01 21:29:17

题目描述

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.

输入

The input is given from Standard Input in the following format:
S

输出

If it is possible to obtain S=T, print YES. Otherwise, print NO.

样例输入

erasedream

样例输出

YES

提示

Append erase and dream at the end of T in this order, to obtain S=T.

题意很简单,就是判断S字符串是不是有那四个单词组成,orz很水的题,可是当时写的时候比较卡。

那么我们就从字符串后面判断,如果有匹配的就删去就好了(你说为什么不从前面?dreamer 和erase、eraser 。我怎么知道该删的是dreamer 还是 dream,说不定后面有个erase或者eraser呢)

删除的话用 string.erase(pos,nops);

当然用 substr(pos,nops)把前面的不删除串赋值给原串也可以

 #include<bits/stdc++.h>
using namespace std; int main()
{
string word;
cin>>word;
string a = "dreamer";
string b = "dream";
string c = "eraser";
string d = "erase";
int flag = ;
while(flag)
{
flag = ;
int len = word.length();
if(len < )break; if((word.compare(len-,len,b)==) || (word.compare(len-,len,d)==))
{
word.erase(len-,len);
flag = ;
continue;
}
if((word.compare(len-,len,c)==))
{
word.erase(len-,len);
flag = ;
continue;
}
if((word.compare(len-,len,a)==))
{
word.erase(len-,len);
flag = ;
continue;
}
}
if(word.length() == )printf("YES\n");
else printf("NO\n");
}