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 C (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <stdio.h>

int main(){
    char s[100005];
    char a[] = "maerd", b[] = "remaerd", c[] = "esare", d[] = "resare";

    scanf("%s", s);

    int size = 0;
    while(s[size] != '\0'){
        size++;
    }
    char s_rev[100005];
    int i = 0;
    for(int j = size - 1; j >= 0; --j){
        s_rev[i] = s[j];
        i++;
    }
    s_rev[i] = '\0';

    int j = 0;
    int flag = 0;
    while(s_rev[j] != '\0'){
        int k = 0;
        if(s_rev[j] != '\0'){
            while(a[k] != '\0'){
                if(s_rev[j + k] == a[k]){
                    flag = 1;
                    k++;
                }else{
                    flag = 0;
                    k = 0;
                    break;
                }
            }
            if(flag == 1){
                j += k;
                k = 0;
            }
        }
        if(s_rev[j] != '\0'){
            while(b[k] != '\0'){
                if(s_rev[j + k] == b[k]){
                    flag = 1;
                    k++;
                }else{
                    flag = 0;
                    printf("%c\n", s_rev[j]);
                    k = 0;
                    break;
                }
            }
            if(flag == 1){
                j += k;
                k = 0;
            }
        }
        if(s_rev[j] != '\0'){
            while(c[k] != '\0'){
                if(s_rev[j + k] == c[k]){
                    flag = 1;
                    k++;
                }else{
                    flag = 0;
                    //printf("%c\n", s_rev[j]);
                    k = 0;
                    break;
                }
            }
            if(flag == 1){
                j += k;
                k = 0;
            }
        }
        if(s_rev[j] != '\0'){
            while(d[k] != '\0'){
                if(s_rev[j + k] == d[k]){
                    flag = 1;
                    k++;
                }else{
                    flag = 0;
                    k = 0;
                    break;
                }
            }
            if(flag == 1){
                j += k;
                k = 0;
            }
        }
    }

    if(flag == 1) puts("YES");
    else puts("NO");

    return 0;
}