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 (Python):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial



def main():
    data = input()[::-1]

    index = 0
    while index < len(data):
        if data[index:index+5] == 'maerd':
            index += 5
        elif data[index:index+7] == 'remaerd':
            index += 7
        elif data[index:index+5] == 'esare':
            index += 5
        elif data[index:index+6] == 'resare':
            index += 6
        else:
            print('NO')
            sys.exit()

    print('YES')







if __name__ == '__main__':
    main()

Solution (Python):
s = input()
t = ''
divide = ['dream','dreamer','erase','eraser']

s = s[::-1]
for i in range(4):
    divide[i] = divide[i][::-1]
i = 0
while i < len(s):
    ends = False
    for j in range(4):
        div_str = divide[j]
        if s[i:i+len(div_str)] == div_str:
            t += s[i:i+len(div_str)]
            i += len(div_str)
            break
        elif j == 3:
            ends = True
    if ends:
        break
if t == s:
    print('YES')
else:
    print('NO')

Solution (Python):
s = input()
t = ''
n = len(s)
while n > len(t):
    if s[-5:] == 'dream' or s[-5:] == 'erase':
        t += 'dream'
        s = s[:-5]
    elif s[-7:] == 'dreamer':
        t += 'dreamer'
        s = s[:-7]
    elif s[-6:] == 'eraser':
        t += 'eraser'
        s = s[:-6]
    else:
        break
if len(s) == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
import sys
S = input()
length = len(S)
while 1:
    if length == 0:
        print("YES")
        sys.exit()
    elif S[length-7 : length] == "dreamer":
        length = length - 7
    elif S[length-5 : length] == "dream":
        length = length - 5
    elif S[length-5 : length] == "erase":
        length = length - 5
    elif S[length-6 : length] == "eraser":
        length = length - 6
    else:
        print("NO")
        sys.exit()

Solution (Python):
s=input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
if s:
    print("NO")
else:
    print("YES")

Solution (Python):
import sys
s = raw_input()
ts = ['']
while True:
  nts= []
  for t in ts:
    for w in ['dreamer', 'eraser', 'dream', 'erase']:
      if s == t + w:
        print 'YES'
        sys.exit()
      if s.startswith(t + w):
        nts.append(t + w)
  if len(nts) == 0:
    print 'NO'
    sys.exit()
  ts = nts

Solution (Python):
S=input()
A=["dreamer","dream","eraser","erase"]
endIndex=len(S)
while endIndex > 0:
  for s in A:
    if S.endswith(s, 0, endIndex):
      endIndex -= len(s)
      break
  else:
    print("NO")
    exit()
else:
  print("YES")

Solution (Python):


s = input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")

if s:
    print("NO")
else:
    print("YES")

Solution (Python):
print('YNEOS'[len(input().replace('eraser','').replace('erase','').replace('dreamer','').replace('dream',''))>0::2])

Solution (Python):
import re
print("YES" if re.match("^(dream|dreamer|erase|eraser)+$",input()) else "NO")

Solution (Python):


import re

S = input()
S=re.sub('dreamer(?!ase)','',S)
S=re.sub('eraser(?!ase)','',S)
S=S.replace('dream','')
S=S.replace('erase','')
 
if len(S) == 0:
  print('YES')
else:
  print('NO')

Solution (Python):
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

S = sys.stdin.readline().rstrip()
dp = [False] * (len(S) + 100)
dp[0] = True
words = ['dreamer', 'dream', 'erase', 'eraser']
for i in range(len(S)):
    if dp[i]:
        for w in words:
            dp[i + len(w)] |= S.startswith(w, i)
if dp[len(S)]:
    print('YES')
else:
    print('NO')

Solution (Python):
s = input()
flag = True
while s:
    if s[0:5] == "dream":
        s = s[5:]
        if s[0:2] == "er":
            if s[0:5] != "erase":
                s = s[2:]
    elif s[0:5] == "erase":
        s = s[5:]
        if s[0:1] == "r":
            if s[0:5] != "erase":
                s = s[1:]
    else:
        flag = False
        break
if flag == True:
    print("YES")
else:
    print("NO")

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    a = ["a", "i", "u", "e", "o"]
    c = S()
    if c[0] in a:
        print("vowel")
    else:
        print("consonant")
    return


def B():
    _ = II()
    t = LI()
    tsum = sum(t)
    for _ in range(II()):
        p, x = LI()
        print(tsum - t[p - 1] + x)
    return


def C():
    s = input()
    lens = len(s)
    check = ["dream", "dreamer", "erase", "eraser"]
    q = deque()
    q.append(0)
    while q:
        p = q.pop()
        if p == lens:
            print("YES")
            return
        for i in check:
            if s[p:p + len(i)] == i:
                q.appendleft(p + len(i))
    print("NO")
    return


def D():
    return


if __name__ == '__main__':
    C()

Solution (Python):
S = input()
S = S[::-1]

divide = ["esare","resare","maerd","remaerd"]
hantei = False
i = 0

while i < len(S):
    for j in range(len(divide)):
        if (len(S[j:]) >= len(divide[j])) and (S[i:i+len(divide[j])] == divide[j]):
            i += len(divide[j])
            break
    else:
         break
else:
    hantei = True

if hantei:
    print("YES")
else:
    print("NO")

Solution (Python):
wordlist = ['dream', 'erase', 'dreamer', 'eraser']
S = input()
index = -1
ans = 'YES'
word = ''
for i in range(0, len(S)):
	word = S[-1-i] + word
	if word in wordlist:
		word = ''
	elif len(word) == 7:
		ans = 'NO'
		break
print(ans)

Solution (Python):

import sys
sys.setrecursionlimit(300000)

YES = "YES"  
NO = "NO"  

def solve(S: str):
    elms = ['dream', 'dreamer', 'erase', 'eraser']
    while S:
        found = False
        for e in elms:
            if S[-len(e):] == e:
                found = True
                S = S[:-len(e)]
                break
        if not found:
            print(NO)
            return
    print(YES)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  
    solve(S)

if __name__ == '__main__':
    main()

Solution (Python):
import re
s = input()
s = re.sub('eraser', '', s)
s = re.sub('erase', '', s)
s = re.sub('dreamer', '', s)
s = re.sub('dream', '', s)
print('YES' if len(s) == 0 else 'NO')

Solution (Python):


S = input()
dream_set = {"dream", "dreamer", "erase", "eraser"}


while len(S) > 0:
    if S.endswith("dream") or S.endswith("erase"):
        S = S[:-5]
    elif S.endswith("eraser"):
        S = S[:-6]
    elif S.endswith("dreamer"):
        S = S[:-7]
    else:
        print("NO")
        exit()

print("YES")

Solution (Python):
s = input()
if 'eraser' in s:
    s = s.replace('eraser','')
if 'erase' in s:
    s = s.replace('erase','')
if 'dreamer' in s:
    s = s.replace('dreamer','') 
if 'dream' in s:
    s = s.replace('dream','') 
emp = len(s)
if emp == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
"""


 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())


 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  


a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654


char_list = list("abcd") 
"""

import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    key_words = [e[::-1] for e in ["dream", "dreamer", "erase", "eraser"]]

    target = input()[::-1]

    i = 0
    while True:
        matched_any = False
        for k in key_words:
            k_len = len(k)
            if k == target[i:i + k_len]:
                i += k_len
                matched_any = True
                break

        if not matched_any:
            print("NO")
            break

        if i == len(target):
            print("YES")
            break

Solution (Python):
"""


 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())


 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  


a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654


char_list = list("abcd") 
"""

import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    tmp = ["dream", "dreamer", "erase", "eraser"]
    key_words = [e[::-1] for e in tmp]
    

    target = input()[::-1]
    

    i = 0

    while True:
        matched_any = False
        
        for k in key_words:
            k_len = len(k)
            
            
            if k == target[i:i + k_len]:
                i += k_len
                matched_any = True
                break

        if not matched_any:
            print("NO")
            exit()

        if i == len(target):
            print("YES")
            exit()

Solution (Python):
import re

s = input()

s = re.sub('eraser','',s)

s = re.sub('erase','',s)

s = re.sub('dreamer','',s)

s = re.sub('dream','',s)



if s == "":
    print("YES")
else:
    print("NO")

Solution (Python):
suf = ["dream", "dreamer", "erase", "eraser"]

s = input()
dp = [0] * (len(s) + 1)
dp[0] = 1
for i in range(len(s)):
    if not dp[i]:
        continue
    for it in suf:
        if s[i:i+len(it)] == it:
            dp[i + len(it)] = 1
print("YES") if dp[len(s)] else print("NO")

Solution (Python):
S = input()
S_original = S
S = S + "@" * 7
S_temp = []
i = 0
while (len(S_original) != len(S_temp) or len(S) > i):
    if S[i] == "d":
        if (S[i+5] == "e" and S[i+7] == "a") or S[i+5] == "d":
            S_temp.append("dream")
            i += 5
        elif S[i+5] == "e" and S[i+7] != "@":
            S_temp.append("dreamer")
            i += 7
        elif S[i+5] == "@":
            S_temp.append("dream")
            break
        elif S[i+5] == "e" and S[i+7] == "@":
            S_temp.append("dreamer")
            break
        else:
            break
    elif S[i] == "e":
        if S[i+5] == "r" and S[i+6] != "@":
            S_temp.append("eraser")
            i += 6
        elif S[i+5] == "d" or S[i+5] == "e":
            S_temp.append("erase")
            i += 5
        elif S[i+5] == "@":
            S_temp.append("erase")
            break
        elif S[i+5] == "r" and S[i+6] == "@":
            S_temp.append("eraser")
            break
        else:
            break
    else:
        break

if S_original == "".join(S_temp):
    print("YES")
else:
    print("NO")

Solution (Python):
import itertools

s = str(input())
a = list(itertools.permutations(['dream', 'dreamer', 'erase', 'eraser']))

for i in a:
    t = s.replace(i[0], '')
    t = t.replace(i[1], '')
    t = t.replace(i[2], '')
    t = t.replace(i[3], '')
    if not t:
        print('YES')
        exit()

print('NO')

Solution (Python):
S = input()
T = list(['eraser', 'erase', 'dreamer', 'dream', ])
for i in range(4):
    S = S.replace(T[i], '')
if S == '':
    print('YES')
else:
    print('NO')

Solution (Python):
S = input()[::-1]
T = list(['maerd', 'remaerd', 'esare', 'resare'])
while(1):
    if len(S) == 0:
        print('YES')
        break
    elif S[:5] in T:
        S = S[5:]
    elif S[:6] in T:
        S = S[6:]
    elif S[:7] in T:
        S = S[7:]
    else:
        print('NO')
        break

Solution (Python):
temp = ['dream','dreamer','erase','eraser']

Divider = list()
for n in temp:
    Divider.append(n[::-1])

TargetStr = input()[::-1]

EndFlag = True

while EndFlag:
    for n in Divider:
        if TargetStr[:len(n)] == n:
            TargetStr= TargetStr.replace(n,'',1)
            break
    else:
        EndFlag =False

if len(TargetStr) == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
s=input()
s=s[::-1]
t=""
i=0
while i<=len(s)-5:
    if s[i:i+5]=="maerd":
        t+="maerd"
        i+=5
    elif s[i:i+5]=="esare":
        t+="esare"
        i+=5
    elif s[i:i+6]=="resare":
        t+="resare"
        i+=6
    elif s[i:i+7]=="remaerd":
        t+="remaerd"
        i+=7
    else:
        print("NO")
        exit()
if t==s:
    print("YES")
else:
    print("NO")

Solution (Python):
s = list(input())

while 1:
    if len(s)==0:
        print("YES")
        exit()
    if 1<len(s)<4:
        print("NO")
        exit()
    if len(s)>=5:
        if "".join(s[-5:]) == "dream" or "".join(s[-5:]) == "erase":
            del s[-5:]
        elif "".join(s[-6:]) == "eraser":
            del s[-6:]
        elif "".join(s[-7:]) == "dreamer":
            del s[-7:]
        else:
            print("NO")
            exit()

Solution (Python):
s = input()[::-1]
a = 'maerd'
b = 'remaerd'
c = 'esare'
d = 'resare'
lens = len(s)
cnt = 0
while(cnt < lens):
  if s[cnt:cnt+5] == a or s[cnt:cnt+5] == c:
    cnt += 5
  elif s[cnt:cnt+6] == d:
    cnt += 6
  elif s[cnt:cnt+7] == b:
    cnt += 7
  else:
    print('NO')
    exit()
print('YES')

Solution (Python):
S = input()
word_list = ["eraser", "erase", "dreamer", "dream"]

for i in range(4):
	S = S.replace(word_list[i], "")
if(S == ""):
    print("YES")
else:
    print("NO")

Solution (Python):
import re
print("YES" if re.match("^(dream|dreamer|erase|eraser)+$", input()) else "NO")

Solution (Python):
import re
s=input()
if re.search(r"^(dreamer|dream|erase|eraser)*$",s):
    print("YES")
else:
    print("NO")

Solution (Python):
s = input()
s = s[::-1]

while 1:
    
    
    if s[:5] == "maerd":
        s=s[5::]
    
    elif s[:7] == "remaerd":
        s=s[7::]
    
    elif s[:5] == "esare":
        s=s[5::]
    
    elif s[:6] == "resare":
        s=s[6::]
    
    else:
        print("NO")
        break
    
    if len(s)==0:
        print("YES")
        break

Solution (Python):
import re
print("YES" if re.match("^(dream|dreamer|erase|eraser)+$", input()) else "NO")

Solution (Python):
S = input()
S_list = list(S)

while len(S_list) >= 5 :
    a = len(S_list)
    if S_list[0:5] == ['e', 'r', 'a', 's', 'e']:
        if a >= 6 :
            if S_list[5] == 'r' :
                del S_list[0:6]
            else:
                del S_list[0:5]
        else:
            del S_list[0:5]
    elif S_list[0:5] == ['d', 'r', 'e', 'a', 'm']:
        if a >= 8 :
            if S_list[5:8] == ['e', 'r', 'a'] :
                del S_list[0:5]
            elif S_list[5:7] == ['e', 'r'] :
                del S_list[0:7]
            else :
                del S_list[0:5]
        elif a >= 7 :
            if S_list[5:7] == ['e', 'r'] :
                del S_list[0:7]
            else:
                del S_list[0:5]
        else :
            del S_list[0:5]
    else:
        print('NO')
        break


if len(S_list) == 0:
    print('YES')
elif 0<len(S_list)<5:
    print('NO')

Solution (Python):
S = str(input())
def remove_word(S):
  S1 = S
  if S[-5:] == 'dream':
    S1 = S[:-5]
  if S[-7:] == 'dreamer':
    S1 = S[:-7]
  if S[-5:] == 'erase':
    S1 = S[:-5]
  if S[-6:] == 'eraser':
    S1 = S[:-6]
  if S1 != S:
    return S1
  else:
    return'NO'
while len(S)>0:
  S = remove_word(S)
  if S == 'NO':
    break
if S == 'NO':
  print('NO')
else:
  print('YES')

Solution (Python):
S = input()

while len(S) >= 5:
    if len(S) >= 7 and S[-7:] == "dreamer":
        S = S[:-7]
        continue

    if len(S) >= 6 and S[-6:] == "eraser":
        S = S[:-6]
        continue

    elif S[-5:] == "dream" or S[-5:] == "erase":
        S = S[:-5]
        continue

    else:
        break

if len(S) == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
S = input()
divide = ["dream","dreamer","erase","eraser"]
S = S[::-1]
for i in range(len(divide)):
    divide[i]=divide[i][::-1]
can = True
i=0
while(i<len(S)):
    can2 = False
    for j in range(len(divide)):
        d = divide[j]
        if(S[i:i+len(d)]==d):
            can2 = True
            i += len(d)
    if(can2 != True):
        can=False
        break
if(can):
    print("YES")
else:
    print("NO")

Solution (Python):
s = raw_input()
def solve(s):
	n = len(s)
	states = [[0, 0]]
	while len(states) > 0:
		state, i = states.pop()
		if i > n:
			print "what"
			continue
		if i == n:
			return "YES"
		if state == 1:
			if n - i >= 2 and s[i: i+2] == "er":
				states.append([0, i+2])
		if state == 2:
			if n - i >= 1 and s[i:i+1] == "r":
				states.append([0, i+1])
		if n - i >= 5:
			cs = s[i: i + 5]
			if cs == "dream":
				states.append([1, i+5])
			elif cs == "erase":
				states.append([2, i+5])
	return "NO"
 
print solve(s)

Solution (Python):
S = input()
S = S[::-1]

last = ['maerd', 'remaerd', 'esare', 'resare']
length = [5, 7, 5, 6]

index = 0
while index < len(S):
    for i, tail in enumerate(last) :
        sub = S[index:index + length[i]]
        if sub == tail :
            index += length[i]
            break
    else :
        print('NO')
        break
else :
    print('YES')

Solution (Python):



s = input()

s = s.replace('eraser','')
s = s.replace('erase','')
s = s.replace('dreamer','')
s = s.replace('dream','')
if s == '':
    print('YES')
else:
    print('NO')

Solution (Python):
S = input()
S = S.replace("eraser", "")
S = S.replace("erase", "")
S = S.replace("dreamer", "")
S = S.replace("dream", "")
if(S == ""):
    print("YES")
else:
    print("NO")

Solution (Python):
S = input()
r_S = S[::-1]
devide = ["dream", "dreamer", "erase", "eraser"]
r_devide = [d[::-1] for d in devide]
string = r_S

while string != '':
  can = False
  for d in r_devide:
    if string[:len(d)] == d:
      can = True
      string = string[len(d):]
  if can == False:
    break

if can:
  print("YES")
else:
  print("NO")

Solution (Python):

S = input()

T = ""

while T in S and T != S:

    if S[len(T)] == "d":

        if len(S) == len(T) + 5:
            T += "dream"

        elif len(S) == len(T) + 7:

            T += "dreamer"

        elif len(S)>=len(T)+5 and (S[len(T) + 5] == "d"):
            T += "dream"
        
        elif len(S)>=len(T)+7 and (S[len(T) + 7] == "d" or S[len(T) + 7] == "e"):
            T += "dreamer"

        else:
            T += "dream"

    else:

        if len(S) == len(T) + 5:
            T += "erase"

        elif len(S)>=len(T)+6 and S[len(T) + 5] == "r":
            T += "eraser"

        else:
            T += "erase"

    



if T == S:
    print ("YES")

else:
    print ("NO")

Solution (Python):
s=input()
s=s.replace("eraser","")
s=s.replace("erase","")
s=s.replace("dreamer","")
s=s.replace("dream","")
if s=="":
    print("YES")
else:
    print("NO")

Solution (Python):
S = input()

while S != '' :
    if len(S) >= 5 and S[-5:] == 'dream' :
        S = S[:-5]
        continue
    if len(S) >= 7 and S[-7:] == 'dreamer' :
        S = S[:-7]
        continue
    if len(S) >= 5 and S[-5:] == 'erase' :
        S = S[:-5]
        continue
    if len(S) >= 6 and S[-6:] == 'eraser' :
        S = S[:-6]
        continue
    
    break
    
if S == '' :
    print('YES')
else :
    print('NO')

Solution (Python):
import itertools

s = input()
t_list = ['dreamer', 'dream', 'eraser', 'erase']

flg = False
for t in list(itertools.permutations(t_list)):
    s_tmp = s
    for _t in t:
        s_tmp = s_tmp.replace(_t, '')
    if len(s_tmp) == 0:
        print('YES')
        flg = True
        break

if not flg:
    print('NO')

Solution (Python):
word_list = []
s = input()
ls = len(s)
l, r = None, None
for i in range(ls-4):
    if s[i:i+5] in ["dream","erase"]:
        l = r
        r = i
        if l != None:
            word_list.append(s[l:r])
word_list.append(s[r:])
correct_word = ["dream", "dreamer", "erase", "eraser"]
word_list = [ i in correct_word for i in word_list ]
if all(word_list):
    print("YES")
else:
    print("NO")

Solution (Python):
import sys
msg = str(input())


len_l1 = 0
while len(msg) != len_l1:
    len_l1 = len(msg)
    
    
    if msg[-7:] == "dreamer":
        msg = msg[:-7]
    
    if msg[-6:] == "eraser":
        msg = msg[:-6]
        
    
    if msg[-5:] == "dream":
        msg = msg[:-5]
    
    if msg[-5:] == "erase":
        msg = msg[:-5]
        
    if len(msg) == 0:
        print('YES')
        sys.exit()    
    
print('NO')

Solution (Python):
import re
def main():
    s = input()
    pattern = "^(dream|dreamer|erase|eraser)+$"
    if re.match(pattern,s):
        print('YES')
    else:
        print('NO')

if __name__ == '__main__':
    main()

Solution (Python):
S = input()

tmp = ''
for i in range(len(S)):
    tmp += S[-i-1]
    
    if tmp in ['maerd', 'remaerd', 'esare', 'resare']:
        tmp = ''
        
if tmp == '':
    print('YES')
else:
    print('NO')

Solution (Python):
s = input()
s_list = ["eraser","erase","dreamer","dream"]
for i in range(4):
	s = s.replace(s_list[i],"")
if len(s):
	print("NO")
else:
	print("YES")

Solution (Python):
S=input()
S = S.replace('eraser', '')
S = S.replace('erase', '')
S = S.replace('dreamer', '')
S = S.replace('dream', '')
S=len(S)
if S==0:
    print('YES')
else:
    print('NO')

Solution (Python):
def daydream():
	S = input()
	flag = False
	divide = ['dreamer', 'dream', 'eraser', 'erase']
	while True:
		if len(S) == 0:
			flag = True
			break
		for d in divide:
			if S.endswith(d):
				S = S[:-len(d)]
				break
		else:
			break
	if flag:
		print('YES')
	else:
		print('NO')

daydream()

Solution (Python):
def daydream():
	S = input()
	flag = False
	divide = ['dreamer', 'dream', 'eraser', 'erase']
	while True:
		if len(S) == 0:
			flag = True
			break
		for d in divide:
			if S.endswith(d):
				S = S[:-len(d)]
				break
		else:
			break
	if flag:
		print('YES')
	else:
		print('NO')

daydream()

Solution (Python):
def daydream():
	S = input()
	flag = False
	while True:
		if len(S) == 0:
			flag = True
			break
		elif S.endswith('dreamer'):
			S = S[:-7]
		elif S.endswith('dream'):
			S = S[:-5]
		elif S.endswith('eraser'):
			S = S[:-6]
		elif S.endswith('erase'):
			S = S[:-5]
		else:
			break
	if flag:
		print('YES')
	else:
		print('NO')

daydream()

Solution (Python):
S = input()


while True:
    if len(S) == 0:
        print('YES')
        break
    else:
        
        if S.endswith('dream'):
            S = S[:-5]
        
        elif S.endswith('dreamer'):
            S = S[:-7]
        
        elif S.endswith('erase'):
            S = S[:-5]
        
        elif S.endswith('eraser'):
            S = S[:-6]
        else:
            print('NO')
            break

Solution (Python):
S = input()

while True:
    if len(S) == 0:
        print('YES')
        break
    else:
        if S[-5:]=='dream':
            S = S[:-5]
        elif S[-7:] == 'dreamer':
            S = S[:-7]
        elif S[-5:]== 'erase':
            S = S[:-5]
        elif S[-6:] == 'eraser':
            S = S[:-6]
        else:
            print('NO')
            break

Solution (Python):
s = input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
if s:
    print("NO")
else:
    print("YES")

Solution (Python):
s = input()
words = ['eraser', 'erase', 'dreamer', 'dream']
for i in range(4):
    s = s.replace(words[i], '')
if(len(s) == 0):
    print("YES")
else:
    print("NO")

Solution (Python):
row = input().split()
s = row[0]
s = s[::-1]

dvide = ["dream", "dreamer", "erase", "eraser"]
for i in range(4):
    dvide[i] = dvide[i][::-1]
can = True
i=0
while i < len(s):
    can2 = False
    for j in range(4):
        d = dvide[j]
        if d == s[i:i+len(dvide[j])]:
            i += len(dvide[j])
            can2 = True
    if can2 == False:
        can = False
        break


if can:
    print("YES")
else:
    print("NO")

Solution (Python):
s = str(input())[::-1]
while True:
    if s[:5]=="maerd" or s[:5]=="esare":
        s=s[5:]
        if len(s)==0:
            print("YES")
            exit()
    elif s[:6]=="resare":
        s=s[6:]
        if len(s)==0:
            print("YES")
            exit()
    elif s[:7]=="remaerd":
        s=s[7:]
        if len(s)==0:
            print("YES")
            exit()
    else:
        print("NO")
        exit()

Solution (Python):
s=input()

matsu=["dream","dreamer","erase","eraser"]

i=len(s)
while 0<i:
    if s[i-5:i] in matsu:
        i-=5
        continue
    elif s[i-6:i] in matsu:
        i-=6
    elif s[i-7:i] in matsu:
        i-=7
    else:
        break
if i==0:
    print("YES")
else:
    print("NO")

Solution (Python):
S=input()
CHAR=['dream','dreamer','erase','eraser']
reverse_S=S[::-1]
reverse_CHAR=[]
for i in range(len(CHAR)):
  reverse_CHAR.append(CHAR[i][::-1])

i=0
keizoku=True
while i < len(reverse_S) and keizoku==True:
  for j in range(len(reverse_CHAR)):
    if reverse_S[i:i+len(reverse_CHAR[j])]==reverse_CHAR[j]:
      i+=len(reverse_CHAR[j])
      break
    else:
      if j==(len(reverse_CHAR)-1):
        keizoku=False

if keizoku==True:
  print('YES')
else:
  print('NO')

Solution (Python):
s=input()
s=s.replace("eraser","")
s=s.replace("erase","")
s=s.replace("dreamer","")
s=s.replace("dream","")
if s=="":print("YES")
else:print("NO")

Solution (Python):
S = input()

f = 0
while(1):
    
    if len(S) <= 4:
        break
    if S[:5] == "dream":
        if len(S) == 5:
            f = 1
            break
        elif len(S) == 6:
            break
        if S[:7] == "dreamer":
            if len(S) == 7:
                f = 1
                break
            if S[7] != "a":
                S = S[7:]
                continue
            else:
                S = S[5:]
        else:
            S = S[5:]
    elif S[:5] == "erase":
        if len(S) == 5:
            f = 1
            break
        if S[:6] == "eraser":
            if len(S) == 6:
                f = 1
                break
            if S[6] != "a":
                S = S[6:]
                continue
            else:
                S = S[:5]
        else:
            S = S[5:]
    else:
        break

if f == 0:
    print("NO")
else:
    print("YES")

Solution (Python):
s = input()

while len(s) > 0:
    if s.startswith("dreamera"):
        s = s[5:]
        
    elif s.startswith("eraser"):
        s = s[6:]
        
    elif s.startswith("dreamer"):
        s = s[7:]
    elif s.startswith("dream"):
        s = s[5:]
    elif s.startswith("erase"):
      	s = s[5:]
    else:
        
        print("NO")
        exit()
print("YES")

Solution (Python):


S = input()
while True:
    if len(S)!=0:
        if S[len(S)-7:len(S)]=="dreamer":
            S = S[0:len(S)-7]
        elif S[len(S)-6:len(S)]=="eraser":
            S = S[0:len(S)-6]
        elif S[len(S)-5:len(S)]=="dream":
            S = S[0:len(S)-5]
        elif S[len(S)-5:len(S)]=="erase":
            S = S[0:len(S)-5]
        else:
            print("NO")
            break

    else:
        print("YES")
        break

Solution (Python):
s = input()
while True:
    if s[-5:] == "dream":
        s = s[:-5]
        continue
    if s[-5:] == "erase":
        s = s[:-5]
        continue
    if s[-7:] == "dreamer":
        s = s[:-7]
        continue
    if s[-6:] == "eraser":
        s = s[:-6]
        continue
    break
print("YES" if s == "" else "NO")

Solution (Python):
s=input()
s=s[::-1]
for p in["eraser","erase","dreamer","dream"]:
	p=p[::-1]
	s=s.replace(p,"")
print("NO"if s else"YES")

Solution (Python):
S = input()
while True:
  if S.endswith("dream"):
    S = S[0: len(S)-len("dream")]
  elif S.endswith("dreamer"):
    S = S[0: len(S)-len("dreamer")]
  elif S.endswith("erase"):
    S = S[0: len(S)-len("erase")]
  elif S.endswith("eraser"):
    S = S[0: len(S)-len("eraser")]
  else:
    break
if (len(S) == 0):
  print("YES")
else:
  print("NO")

Solution (Python):

sentence = input()
ans = 0
sentence = sentence.replace("eraser", "")
sentence = sentence.replace("erase", "")
sentence = sentence.replace("dreamer", "")
sentence = sentence.replace("dream", "")

if sentence == "":
    print("YES")
else:
    print("NO")

Solution (Python):
s = str(input())

T = ["dream", "dreamer", "erase", "eraser"]

new_s =s[::-1]
new_t = []
for item in T:
  new_t.append(item[::-1])

ans_t =''
collector = ''

for c in new_s:
  collector += c
  if collector in new_t:
    ans_t += collector
    collector = ''
    
if new_s == ans_t:
  print('YES')
else:
  print('NO')

Solution (Python):
S = input()

strs = ['eraser', 'erase', 'dreamer', 'dream']

for i in range(len(strs)):
    S = S.replace(strs[i], '')

if len(S) > 0:
    print('NO')
else:
    print('YES')

Solution (Python):
S = input()

strs = ['eraser', 'erase', 'dreamer', 'dream']

for i in range(len(strs)):
    S = S.replace(strs[i], '')

if len(S) > 0:
    print('NO')
else:
    print('YES')

Solution (Python):
S = input()

fstrs = ['dreamer', 'eraser', 'dream', 'erase']

flag = True
while flag:
    for fstr in fstrs:
        result = S.rfind(fstr)
        if result >= 0 and result == len(S) - len(fstr):
            
            S = S[0:result]
            flag = True
            break
        else:
            
            flag = False
    if len(S) == 0:
        break

if len(S) > 0:
    print('NO')
else:
    print('YES')