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 OCaml (4.10.0) (judge verdict: Runtime Error)

let asReversedString s =
  String.of_seq (List.to_seq (List.rev (List.of_seq (String.to_seq s))))

let isPrefix prefixString targetString =
  let prefixLength = String.length prefixString in
  let targetSub = String.sub targetString 0 prefixLength in
  prefixString = targetSub

let remove_prefix prefixString targetString =
  let prefixLength = String.length prefixString in
  let targetLength = String.length targetString in
  let newTargetStart = prefixLength in 
  let newTargetLength = targetLength - prefixLength in
  String.sub targetString newTargetStart newTargetLength

let satisfy s =
  let reversed = asReversedString s in
  let word1 = asReversedString "dream" in
  let word2 = asReversedString "dreamer" in
  let word3 = asReversedString "erase" in
  let word4 = asReversedString "eraser" in
  let rec satisfy_aux s =
    if s = "" then true else
      if isPrefix word1 s then satisfy_aux (remove_prefix word1 s) else
        if isPrefix word2 s then satisfy_aux (remove_prefix word2 s) else
          if isPrefix word3 s then satisfy_aux (remove_prefix word3 s) else
            if isPrefix word4 s then satisfy_aux (remove_prefix word4 s) else
              false in
  satisfy_aux reversed
    

let main () =
  let s = read_line () in
  let ans = if satisfy s
            then "YES"
            else "NO" in
  (
    Printf.printf "%s" ans;
    print_newline ()
  )

let _ = main ()