题目描述
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.
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
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)把前面的不删除串赋值给原串也可以
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 int main() 5 { 6 string word; 7 cin>>word; 8 string a = "dreamer"; 9 string b = "dream"; 10 string c = "eraser"; 11 string d = "erase"; 12 int flag = 1; 13 while(flag) 14 { 15 flag = 0; 16 int len = word.length(); 17 if(len < 5)break; 18 19 if((word.compare(len-5,len,b)==0) || (word.compare(len-5,len,d)==0)) 20 { 21 word.erase(len-5,len); 22 flag = 1; 23 continue; 24 } 25 if((word.compare(len-6,len,c)==0)) 26 { 27 word.erase(len-6,len); 28 flag = 1; 29 continue; 30 } 31 if((word.compare(len-7,len,a)==0)) 32 { 33 word.erase(len-7,len); 34 flag = 1; 35 continue; 36 } 37 } 38 if(word.length() == 0)printf("YES\n"); 39 else printf("NO\n"); 40 } 41