#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char* str_dream   = "dream";
static char* str_dreamer = "dreamer";
static char* str_erase   = "erase";
static char* str_eraser  = "eraser";
static char* str_mer     = "mer";
static char* str_ser     = "ser";

int main(int argc, char* argv[]){
        char *buf= NULL;
        char tmp[256+1]={0};
        int  spos = -1;
        int  epos = -1;

        buf = (char*)calloc(100000+2, sizeof(char));
        if(buf==NULL){
                return 1;
        }

        while(fgets(tmp, sizeof(tmp)-1, stdin)!=NULL){
                strcat(buf, tmp);
                memset(tmp, 0, sizeof(tmp));
        }

        epos = strlen(buf) -1;
        spos = epos;
        int ans =  1;
        for(;epos>=0 && spos>=0 && ans!=0;){
                if(buf[epos]=='\n'){
                        epos--;
                        continue;
                }
                if(buf[epos]=='m'){ //dream
                        spos = epos - 5 + 1; //dream
                        if(spos<0){
                                ans = 0;
                                break;
                        }
                        else{
                                if(memcmp((void*)(&buf[spos]), str_dream, 5)!=0){
                                        ans = 0;
                                }
                        }
                }
                else if(buf[epos]=='e'){ //erase
                        spos = epos - 5 + 1; //erase
                        if(spos<0){
                                ans = 0;
                        }
                        else{
                                if(memcmp((void*)(&buf[spos]), str_erase, 5)!=0){
                                        ans = 0;
                                }
                        }
                }
                else if(buf[epos]=='r'){ //dreamer or eraser
                        spos = epos - 3 + 1; // mer or ser
                        if(spos<0){
                                ans = 0;
                        }
                        else if(memcmp((void*)(&buf[spos]), str_mer, 3)==0){ //mer
                                spos = epos - 7 + 1; //dreamer
                                if(spos<0){
                                        ans = 0;
                                }
                                else{
                                        if(memcmp((void*)(&buf[spos]), str_dreamer, 7)!=0){
                                                ans = 0;
                                        }
                                }
                        }
                        else if(memcmp((void*)(&buf[spos]), str_ser, 3)==0){ //ser
                                spos = epos - 6 + 1; //eraser
                                if(spos<0){
                                        ans = 0;
                                }
                                else{
                                        if(memcmp((void*)(&buf[spos]), str_eraser, 6)!=0){
                                                ans = 0;
                                        }
                                }
                        }
                        else{
                                ans = 0;
                        }
                }
                else{
                        ans = 0;
                }
                epos = spos - 1;
        }

        if(ans==1){
                printf("YES\n");
        }
        else{
                printf("NO\n");
        }
        return 0;
}

Measured memory_bytes: 256