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 Java8 (OpenJDK 1.8.0) (judge verdict: Memory Limit Exceeded)

import java.util.ArrayList;
import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String S=sc.next();
		if(S.length()<5)System.out.println("NO");
		StringBuilder s=new StringBuilder(S); 
		ArrayList<String>de=new ArrayList<>();
		de.add("dream");
		de.add("dreamer");
		de.add("erase");
		de.add("eraser");
		while(true){
			if(de.contains(s.substring(0,s.length()))){System.out.println("YES");break;}
			if(s.length()<5 || s.length()==8 || s.length()==9){System.out.println("NO");break;}
		
			if((s.length()>5&&s.substring(0,6).equals("dreamd"))||(s.length()>7&&s.substring(0,8).equals("dreamera")))s.delete(0,5);
			else if(s.length()>=6&&(s.substring(0,6).equals("erasee")||s.substring(0,6).equals("erased")))s.delete(0,5);
			else if(s.length()>=7&&(s.substring(0,7).equals("erasere")||s.substring(0,7).equals("eraserd")))s.delete(0,6);
			else if(s.length()>=8&&(s.substring(0,8).equals("dreamerd")||s.substring(0,8).equals("dreamere")))s.delete(0,7);
			else {System.out.println("NO");break;}
			
		}
}
}