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 Pascal (FPC 2.6.2) (judge verdict: Wrong Answer)

var s,s2:ansistring;
pos1,pos2,pos3,pos4,cnt,i:longint;
begin
  readln(s);
  for i:=length(s) downto 1 do
    s2:=s2+s[i];
  cnt:=1;
  pos1:=0;
  pos2:=0;
  pos3:=0;
  pos4:=0;
  while (true) do
    begin
      pos1:=pos('maerd',s2);
      pos2:=pos('remaerd',s2);
      pos3:=pos('esare',s2);
      pos4:=pos('resare',s2);
      //writeln(s2);
      if (length(s2)=0) then break
      else if (pos1=0) and (pos2=0) and (pos3=0) and (pos4=0) then
        begin
          writeln('NO');
          halt;
        end
      else if ((pos2=0) or (pos1<pos2)) and ((pos3=0) or (pos1<pos3)) and ((pos4=0) or (pos1<pos4)) then
        begin
          delete(s2,1,5);
        end
        else if ((pos1=0) or (pos2<pos1)) and ((pos3=0) or (pos2<pos3)) and ((pos4=0) or (pos2<pos4)) then
        begin
          delete(s2,1,7);
        end
        else if ((pos1=0) or (pos3<pos1)) and ((pos2=0) or (pos3<pos2)) and ((pos4=0) or (pos3<pos4)) then
        begin
          delete(s2,1,5);
        end
        else if ((pos1=0) or (pos4<pos1)) and ((pos2=0) or (pos4<pos2)) and ((pos3=0) or (pos4<pos3)) then
        begin
          delete(s2,1,6);
        end
        else begin
        if length(s2)=0 then break;
          writeln('NO');
          halt;
        end;
    end;
    writeln('YES');
end.