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.02.3) (judge verdict: Accepted)

module CharMap = Map.Make (Char)

module RegExp : sig
  type t
  val empty_set : t
  val empty_str : t
  val char : Char.t -> t
  val app : t -> t -> t
  val union : t -> t -> t
  val star : t -> t

  val matches : t -> Char.t list -> bool
end = struct
  type t =
    { body : regexp;
      is_nullable : bool;
      mutable derivatives : t CharMap.t }
  and regexp =
    | EmptySet
    | EmptyStr
    | Char of Char.t
    | App of t * t
    | Union of t * t
    | Star of t

  let empty_set =
    { body = EmptySet;
      is_nullable = false;
      derivatives = CharMap.empty }

  let empty_str =
    { body = EmptyStr;
      is_nullable = true;
      derivatives = CharMap.empty }

  let char c =
    { body = Char c;
      is_nullable = false;
      derivatives = CharMap.empty }

  let app re1 re2 =
    match re1, re2 with
    | { body = EmptySet }, _ -> empty_set
    | _, { body = EmptySet } -> empty_set
    | { body = EmptyStr }, _ -> re2
    | _, { body = EmptyStr } -> re1
    | _, _ ->
        { body = App (re1, re2);
          is_nullable = re1.is_nullable && re2.is_nullable;
          derivatives = CharMap.empty }

  let union re1 re2 =
    match re1, re2 with
    | { body = EmptySet }, _ -> re2
    | _, { body = EmptySet } -> re1
    | _, _ ->
        { body = Union (re1, re2);
          is_nullable = re1.is_nullable || re2.is_nullable;
          derivatives = CharMap.empty }

  let star = function
    | { body = EmptySet } -> empty_str
    | { body = EmptyStr } -> empty_str
    | { body = Star re } -> re
    | re -> { body = Star re; is_nullable = true; derivatives = CharMap.empty }

  let rec derive c re =
    try CharMap.find c re.derivatives
    with Not_found ->
      let d =
        match re with
        | { body = EmptySet } -> empty_set
        | { body = EmptyStr } -> empty_set
        | { body = Char c' } ->
            if Char.compare c c' = 0 then empty_str else empty_set
        | { body = App (re1, re2) } ->
            union (if re1.is_nullable then derive c re2 else empty_set) (app (derive c re1) re2)
        | { body = Union (re1, re2) } ->
            union (derive c re1) (derive c re2)
        | { body = Star re0 } ->
            app (derive c re0) re in
      re.derivatives <- CharMap.add c d re.derivatives; d

  let matches re s =
    (List.fold_left (fun re c -> derive c re) re s).is_nullable
end

let () =
  let s = read_line () in
  print_endline @@
  if
    let open RegExp in
    matches
      (star @@
        union
          (app (char 'd') @@ app (char 'r') @@ app (char 'e') @@ app (char 'a') @@ app (char 'm') @@
            union empty_str @@ app (char 'e') @@ char 'r')
          (app (char 'e') @@ app (char 'r') @@ app (char 'a') @@ app (char 's') @@ app (char 'e') @@
            (union empty_str @@ char 'r'))) @@
      Array.to_list @@ Array.init (String.length s) (String.get s)
  then "YES"
  else "NO"