let hd_opt t =
  match t with
  | [] -> None
  | x :: _ -> Some x

open Batteries
let () =
  let s = Scanf.scanf "%s " (fun a -> a) in
  let rev_s = String.rev s in

  let dict = ["dream";"dreamer";"erase";"eraser"] in
  let rev_dict = List.map (fun x -> String.rev x) dict in

  let rec aux s = match s with
      "" -> "YES"
    | s -> 
      let res = List.map (fun dict_s -> 
          let len = String.length dict_s in
          let target = String.slice ~last:len s in
          if dict_s = target then len else 0
        ) rev_dict |> List.filter (fun x -> x <> 0) in

      match (hd_opt res) with
      Some x -> aux (String.slice ~first:x s)
      | None -> "NO"
  in

  Printf.printf "%s\n" @@
  aux rev_s
