Problem:
Score : 300 points

Problem Statement

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|≦10^5

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.

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

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int>P;
#define PI 3.14159265359
#define MOD 1000000007
//ABC166 E 
int main(){
  string s;
  cin >>s;
  int N=s.size();
  int flag=0;
  while(true){
    if(N==0){flag=100;break;}
    if(N<=4){flag=1;break;}
  	if(s[N-2]=='e'&&s[N-1]=='r'){
      	if(N<=5){flag=1;break;}
      	if(s[N-6]=='e'&&s[N-5]=='r'&&s[N-4]=='a'&&s[N-3]=='s'){N-=6;}
    	else if(s[N-7]=='d'&&s[N-6]=='r'&&s[N-5]=='e'&&s[N-4]=='a'&&s[N-3]=='m'){N-=7;}
      	else{flag=1;}
    }
    else if(s[N-5]=='d'&&s[N-4]=='r'&&s[N-3]=='e'&&s[N-2]=='a'&&s[N-1]=='m'){
    	N-=5;
    }
    else if(s[N-5]=='e'&&s[N-4]=='r'&&s[N-3]=='a'&&s[N-2]=='s'&&s[N-1]=='e'){
    	N-=5;
    }
    else{flag=1;}
    if(flag==1){break;}
  }
  if(flag==100){cout<<"YES";}
  if(flag==1){cout<<"NO";}
}