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 Perl (v5.18.2) (judge verdict: Wrong Answer)

sub u1{my$i=pop;
	$p[$i]=$p[$i]&&$p[$i]!=$i?u1($p[$i]):$i+0
}
sub u2{my$i=pop;
	$q[$i]=$q[$i]&&$q[$i]!=$i?u2($q[$i]):$i+0
}
($N,$K,$L)=split$",<>;
for(1..$K){
	($a,$b)=split$",<>;
	$a=u1($a+0);
	$b=u1($b+0);
	if($a!=$b){
		$p[$a]=$b
	}
}
for(1..$L){
	($a,$b)=split$",<>;
	$a=u2($a+0);
	$b=u2($b+0);
	if($a!=$b){
		$q[$a]=$b
	}
}
@s=map[u1($_),u2($_),$_],1..$N;
@s=sort{@$a[0]<=>@$b[0]||@$a[1]<=>@$b[1]}@s;
for($i=0;$i<$N;){
	$j=$i+1,$t=1;
	($a,$b)=@{$s[$i]};
	for(;($c,$d)=@{$s[$j]},$j<$N&&$a==$c&&$b==$d;++$j){
		++$t
	}
	for(;$i<$j;++$i){
		$ans[@{$s[$i]}[2]]=$t
	}
}
print$_,$"for@ans