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):
s=input()
for i in range(len(s)//5+1):
    if s[-5:]=="dream" or s[-5:]=="erase":
        s=s[:-5]
    elif s[-6:]=="eraser":
        s=s[:-6]
    elif s[-7:]=="dreamer":
        s=s[:-7]
    if len(s)==0:
        print('YES')
        exit()
print('NO')

Solution (Python):
s = input()[::-1]
flag = 1
i = 0
while i < len(s):
    if s[i:i+5] == "maerd" or s[i:i+5] == "esare":
        i += 5
    elif s[i:i+6] == "resare":
        i += 6
    elif s[i:i+7] == "remaerd":
        i += 7
    else:
        flag = 0
        break

print("YES" if flag else "NO")

Solution (Python):
st=input()
c="0"
j="0"
u="0"
for i in st:
    if i=="d" and u=="0" and c=="0":
        u="d"
        j="1"
    elif i=="r" and u=="d":
        u="r1"
    elif i=="e" and u=="r1":
        u="e1"
    elif i=="a" and u=="e1":
        u="a1"
    elif i=="m" and u=="a1":
        u="0"
        
    elif i=="e" and u=="0" and c=="0":
        u="e2"
        if j !="1":
            c="1"
    elif i=="r" and u=="e2":
        u="0"
        j="2"
    elif i=="a" and u=="0" and j=="2":
        u="a2"
    elif i=="s" and u=="a2":
        u="s"
    elif i=="e" and u=="s":
        u="0"
        c="0"
        j="3"
    elif i=="r" and u=="0" and j=="3":
        j="0"
    else:
        print("NO")
        exit(0)
if u=="0"and c=="0":
    print("YES")
else:
    print("NO")

Solution (Python):
from sys import stdin

s = stdin.readline().rstrip().replace("eraser", "").replace("erase", "").replace("dreamer", "").replace("dream", "")

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

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

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

while i < len(s):
    if s[i:i+5] == 'maerd' or s[i:i+5] == 'esare':
        i += 5
    elif s[i:i+6] == 'resare':
        i += 6
    elif s[i:i+7] == 'remaerd':
        i += 7
    else:
        break

print('YES' if i == len(s) else 'NO')

Solution (Python):
S = list(input())
a = list('dream')
b = list('dreamer')
c = list('erase')
d = list('eraser')

while True:
    if S[-5:] == a:
        del S[-5:]
    elif S[-7:] == b:
        del S[-7:]
    elif S[-5:] == c:
        del S[-5:]
    elif S[-6:] == d:
        del S[-6:]
    else:
        break
        
if len(S) == 0:
    print('YES')
else:
    print('NO')

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

Solution (Python):
S = input()
l = len(S)
while l > 0:
  if S[l-5:l] == 'erase' or S[l-5:l] == 'dream':
    l -= 5
  elif S[l-6:l] == 'eraser':
    l -= 6
  elif S[l-7:l] == 'dreamer':
    l -= 7
  else:
    l = -10
    
if l == 0:
  print("YES")
else:
  print("NO")

Solution (Python):
s = input()
s_rem = s
flag = 0
while len(s_rem) > 0:
    s_spl = s_rem[:5]
    if s_spl != "dream" and s_spl != "erase":
        flag = 1
        break
    s_rem = s_rem[5:]
    if s_rem[:1] == "":
        break
    elif s_spl == "dream" and (s_rem[:1] != "d" and s_rem[:1] != "e"):
        flag = 1
        break
    elif s_spl == "dream" and s_rem[:1] == "d":
        continue
    elif s_spl == "dream" and s_rem[:1] == "e":
        if s_rem[:5] == "erase":
            continue
        elif s_rem[:2] == "er":
            s_rem = s_rem[2:]
            continue
        else:
            flag = 1
            break
    elif s_spl == "erase" and (s_rem[:1] != "d" and s_rem[:1] != "e" and s_rem[:1] != "r"):
        flag = 1
        break
    elif s_spl == "erase" and s_rem[:1] == "r":
        s_rem = s_rem[1:]
        continue
    elif s_spl == "erase" and s_rem[:1] == "d":
        continue
    else:
        if s_rem[:5] == "erase":
            continue
        elif s_rem[:2] == "er":
            s_rem = s_rem[2:]
            continue
        else:
            flag = 1
            break

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

Solution (Python):

from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007






s = input()
while s:
    if s[:11] == "dreameraser":
        s = s[11:]
    elif s[:10] == "dreamerase":
        s = s[10:]
    elif s[:7] == "dreamer":
        s = s[7:]
    elif s[:6] == "eraser":
        s = s[6:]
    elif s[:5] == "dream":
        s = s[5:]
    elif s[:5] == "erase":
        s = s[5:]
    else:
        print("NO")
        quit()
print("YES")

Solution (Python):
S = input()
while len(S) > 0:
    if 'dream' in S[-5:]:
        S = S[:-5]
        continue
    if 'dreamer' in S[-7:]:
        S = S[:-7]
        continue
    if 'erase' in S[-5:]:
        S = S[:-5]
        continue
    if 'eraser' in S[-6:]:
        S = S[:-6]
        continue
    break
if S == '':
    print('YES')
else:
    print('NO')

Solution (Python):
S = input()
dd = ['dream', 'dreamer', 'erase', 'eraser']
chara = ''.join(reversed(list(S)))
flag = True
while flag:
    cnt = 0
    for i in dd:
        r = ''.join(reversed(list(i)))
        if r in chara[:len(r)]:
            chara = chara[len(r):]
            break
        cnt += 1
    if cnt == 4:
        flag = False

if chara == '':
    print('YES')
else:
    print('NO')

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

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

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

Solution (Python):
s = input().replace("eraser","")

s = s.replace("erase","")

s = s.replace("dreamer","")

s = s.replace("dream","")


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

Solution (Python):
s = input()

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

Solution (Python):
S = input()
l = len(S)
while l > 0:
  if S[l-5:l] == 'erase' or S[l-5:l] == 'dream':
    l -= 5
  elif S[l-6:l] == 'eraser':
    l -= 6
  elif S[l-7:l] == 'dreamer':
    l -= 7
  else:
    l = -10
    
if l == 0:
  print("YES")
else:
  print("NO")

Solution (Python):
S = input()
S = S[::-1]
l = ['dream','dreamer','erase','eraser']
L = []
pos = 0
f=True

for i in l:
    L.append(i[::-1])

while pos < len(S):
    no_match = True
    for candidate in L:
        if S[pos:pos+len(candidate)]==candidate:
            pos+=len(candidate)
            no_match =False
            break
    if no_match:
        f =False
        break


if f:
    print('YES')
else:
    print('NO')

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

Solution (Python):
s = input()
divide = ['dream','dreamer','erase','eraser']
dp = [0] * 100010
dp[0] = 1
for i in range(len(s)):
    
    
    if dp[i] == False:
        continue
    for str in divide:
        
        if s[i:i+len(str)] == str:
            dp[i + len(str)] = 1

print("YES") if dp[len(s)] else print("NO")

Solution (Python):
import sys
s = list(input())
check =['dream','dreamer','erase','eraser']
while len(s) >= 8:
    if ''.join(s[-5:]) == check[0]:
        for _ in range(5): s.pop()
        continue
    if ''.join(s[-7:]) == check[1]:
        for _ in range(7): s.pop()
        continue
    if ''.join(s[-5:]) == check[2]:
        for _ in range(5): s.pop()
        continue
    if ''.join(s[-6:]) == check[3]:
        for _ in range(6): s.pop()
        continue
    print('NO')
    sys.exit()
if ''.join(s) == check[0] or ''.join(s) == check[1] or ''.join(s) == check[2] or ''.join(s) == check[3]:
    print('YES')
else:
    print('NO')

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

for w in ll[::-1]:
  s=s.replace(w, '')
if len(s)>0:
  print('NO')
else:
  print('YES')

Solution (Python):
s=input()
before=""
while len(s):
    if s[0:5]=="dream":
        if s[0:7]=="dreamer":
            before="er"
            s=s[7:]
        else:
            before=""
            s=s[5:]
    elif s[0:5]=="erase":
        if s[0:6]=="eraser":
            before="er"
            s=s[6:]
        else:
            before=""
            s=s[5:]
    elif before=="er":
        if s[0:3]=="ase":
            if s[0:4]=="aser":
                before="er"
                s=s[4:]
            else:
                before=""
                s=s[3:]
        else:
            break
    else:
        break
if len(s):
    print("NO")
else:
    print("YES")

Solution (Python):
S=input()

S=''.join(list(reversed(S)))

SS=['maerd','remaerd','esare','resare']

e=0
f=0

while e==0:
    s=1
    for i in range(len(SS)):
        if S[f:f+len(SS[i])]==SS[i]:
            f+=len(SS[i])
            s=0
    if f==len(S):
        print('YES')
        e=1
    if s==1:
        print('NO')
        e=1

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

"""

import itertools

s=str(input())
add=["dream","dreamer","erase","eraser"]
t=[]

for i,_ in enumerate(add,1):
	for j in itertools.permutations(add,r=i):
		t.append("".join(j))

print(t)

temp=False
for i in range(len(t)):
	if(s==t[i]):
		temp=True
		break

if temp==True:
	print("Yes")
else:
	print("No")
	

"""

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

words = ['dream', 'dreamer', 'erase', 'eraser']
for i in range(4):
    words[i] = words[i][::-1]

cnt = 0
while cnt < len(s):
    for w in words:
        if s[cnt: cnt + len(w)] == w:
            cnt += len(w)
            break
    else:
        print('NO')
        exit()

print('YES')

Solution (Python):
s = str(input())
s = s.replace('eraser', '').replace('erase', '').replace('dreamer', '').replace('dream', '')
if len(s)>1:
    print('NO')
else:
    print('YES')

Solution (Python):
int_s = input()
str_sr = int_s[::-1]


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

str_r = list(map(lambda x: x[::-1], str_a))

result = True

while len(str_sr):
    currnt_flg = False
    for r in str_r:
        
        if len(str_sr) >= len(r) and str_sr[:len(r)] == r:
            
            str_sr = str_sr[len(r):]
            
            current_flg = True
            break
    else:
        result = False
        break

if result:
    print('YES')
else:
    print('NO')

Solution (Python):
s=input()

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

for w in words:
  s=s.replace(w, '')

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

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

Solution (Python):
S = input()
S = S[::-1]
divide = list(map(lambda phrase:phrase[::-1], ['dream', 'dreamer', 'erase', 'eraser']))

flag = False
i = 0
while i != len(S):
    flag = False
    for d in divide:
        if S[i:i+len(d)] == d:
            flag = True
            i += len(d)
            break

    if flag == False:
        break

print('YES' if flag else 'NO')

Solution (Python):


import sys

YES = "YES"  
NO = "NO"  

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

def solve(S: str):
    print(YES if is_valid2(S) else NO)
    return

def is_valid(S):
    if S == '':
        return True
    for token in tokens:
        sub = S[-len(token):]
        if sub == token:
            return is_valid(S[:-len(token)])
    return False

def is_valid2(S):
    import re
    x = r"^(dream|dreamer|erase|eraser)*$"
    return True if re.match(x, S) else False

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):
S = input().replace('eraser','').replace('erase','').replace('dreamer','').replace('dream','')
if(S == ''):
  print('YES')
else:
  print('NO')

Solution (Python):
S=input()
S=S[::-1]
divide=['dream','dreamer','erase','eraser'] 
flag=False
for i in range(4):
  divide[i]=divide[i][::-1]

i=0
while i!=len(S):
  flag=False
  for d in divide:
    if S[i:i+len(d)]==d:   
      flag=True
      i+=len(d)
      break
    
  if flag==False:
     break
      
print('YES' if flag else 'NO')

Solution (Python):

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

pattern = ['maerd', 'remaerd', 'esare', 'resare']

pos = 0
possible = True

while pos < len(S):
    no_match = True
    for candidate in pattern:
        if S[pos : pos + len(candidate)] == candidate:
            pos += len(candidate)
            no_match = False
            break

    if no_match:
        possible = False
        break

if possible:
    print('YES')
else:
    print('NO')

Solution (Python):
s = input()
sr = s[::-1]
w = ["resare","esare","remaerd","maerd"]
for i in range(4):
    sr = sr.replace(w[i],"")
if sr == "":print("YES")
else:print("NO")

Solution (Python):
s=input()
w=""
for c in s[::-1]:
    w+=c
    if(w=="dreamer"[::-1] or w=="dream"[::-1] or w=="eraser"[::-1] or w=="erase"[::-1]):
        w=""
if(w==""):
    print("YES")
else:
    print("NO")

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

Solution (Python):
s=input()
strings=["dreamer","dream", "eraser","erase"]
i=-1
while len(s)!=0:
  if s[i-1:] in strings:
    s=s[:i-1]
    i=-1
  i-=1
  if i==-len(s):
    print('NO')
    exit()
if len(s)==0:
  print('YES')

Solution (Python):
S = list(raw_input())[::-1]

T = [list('dream')[::-1],list('dreamer')[::-1],list('erase')[::-1],list('eraser')[::-1]]

while True:
    flag = True
    if len(S) >= 5 and T[0] == S[0:5]:
        del S[0:5]
        flag = False
    if len(S) >= 7 and T[1] == S[0:7]:
        del S[0:7]
        flag = False
    if len(S) >= 5 and T[2] == S[0:5]:
        del S[0:5]
        flag = False
    if len(S) >= 6 and T[3] == S[0:6]:
        del S[0:6]
        flag = False
    if flag:
        break

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

Solution (Python):
import re
import sys
input = sys.stdin.readline

S = input()
regex = r'^(dream(er)?|erase(r)?)+$'
if re.match(regex, S) is None:
    print('NO')
else:
    print('YES')

Solution (Python):
S = str(input())
def detect_dream(string):
    assert string[0] == "d"
    if string[:5] != "dream":
        return False, ""
    if len(string) >= 8:
        if string[:8] in ["dreamere", "dreamerd"]:
            return True, "dreamer"
    if string == "dreamer":
        return True, "dreamer"
    return True, "dream"

def detect_erase(string):
    assert string[0] == "e"
    if string[:5] != "erase":
        return False, ""
    if len(string) >= 6:
        if string[:6] == "eraser":
            return True, "eraser"
    return True, "erase"

while len(S) >= 5:
    if S[0] == "d":
        result, string = detect_dream(S)
    elif S[0] == "e":
        result, string = detect_erase(S)
    else:
        break
    if result:
        S = S.replace(string,"",1)
    else:
        break
if len(S) == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
inp = input()
inp = inp[::-1]
li = ['dream','dreamer','erase','eraser']
li = [k[::-1] for k in li]
m = 0
while True:
    for i in li:
        if inp.startswith(i):
    	    inp = inp.replace(i,'',1)
    	    m = 0
    else:
    	if m == 1:
    		break
    	else:
    		m = 1
    		continue
    break
if inp == '':
	print("YES")
else:
	print("NO")

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

Solution (Python):
S=input()
flag=True
words={'dream','dreamer','erase','eraser'}
while flag:
    if S=="" :
        break
    if S[-7:] in words:
        S=S[0:-7]
        continue
    elif S[-6:] in words:
        S=S[0:-6]
        continue
    elif S[-5:] in words:
        S=S[0:-5]
        continue
    else :
        flag=False
if flag:
    print("YES")
else :
    print("NO")

Solution (Python):
s = input()
n = len(s) // 5
for i in range(n + 1):
   if len(s) == 0:
      print('YES')
      exit()
   if s[:6] == 'eraser':
      s = s[6:]
   elif s[:5] == 'erase':
      s = s[5:]
   elif ((s[:7] == 'dreamer') and (s[:10] != 'dreamerase') and (s[:11] != 'dreameraser')) or ((len(s) == 7) and (s == 'dreamer')):
      s = s[7:]
   elif (s[:5] == 'dream'):
      s = s[5:]
   else:
      print('NO')
      exit()

Solution (Python):
s = input()
t_list = ['dream', 'dreamer', 'erase', 'eraser']
ans = 'YES'
while len(s) > 0:
  for t in t_list:
    if s.endswith(t):
      s = s[:-len(t)]
      break
  else:
    ans = 'NO'
    break
print(ans)

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

print('YES')

Solution (Python):
S = input()

flag = 1
while len(S):
    if S[-7:] == 'dreamer':
        S = S[:-7]
    elif S[-6:] == 'eraser':
        S = S[:-6]
    elif S[-5:] == 'dream' or S[-5:] == 'erase':
        S = S[:-5]
    else:
        flag = 0
        break

if flag:
    print('YES')
else:
    print('NO')

Solution (Python):
S = input()

flag = 1
while len(S):
    if S[:7] == 'dreamer':
        if S[5:11] == 'eraser':
            S = S[11:]
        elif S[5:10] == 'erase':
            S = S[10:]
        else:
            S = S[7:]
    elif S[:6] == 'eraser':
        S = S[6:]
    elif S[:5] == 'dream' or S[:5] == 'erase':
        S = S[5:]
    else:
        flag = 0
        break

if flag:
    print('YES')
else:
    print('NO')

Solution (Python):
S = input()
patterns = ['dreamer', 'dream', 'eraser', 'erase']


while len(S) != 0:
    isShaved = False
    for p in patterns:
        if S.endswith(p):
            S = S[:len(S)-len(p)]
            isShaved = True
            break
    if not isShaved:
        break

print('YES' if len(S) == 0 else 'NO')

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

Solution (Python):
str_list = ['dream', 'dreamer','erase','eraser']
def reverse_str(str_):
    return str_[::-1]

s = str(input())
s = s[::-1]
find_list = list(map(reverse_str, str_list))
index = 0

while index < len(s):
    loop_flag = False
    for i in range(len(find_list)):
        str_value = find_list[i]
        if str(s[index:index+len(str_value)]) == str_value:

            loop_flag = True
            index += len(str_value)
    if not loop_flag:
        break
if index >= len(s):
    print('YES')
else:
    print('NO')

Solution (Python):
from os.path import commonprefix as cp
import math
INF, MOD = float("inf"), 1e9 + 7
MAX, MIN = -INF, INF
dx1, dy1, dx2, dy2 = [-1, 0, 1, 0], [0, -1, 0, 1], [-1, 0, 1, -1, 1, -1, 0, 1], [-1, -1, -1, 0, 0, 1, 1, 1]

def get_int():
    return int(input())

def get_int_list():
    return list(map(int, input().split()))

while(True):
    try:
        prefixs = ["remaerd", "maerd", "resare", "esare"]
        s = input()[::-1]
        while(len(s)):
            flag = True
            for p in prefixs:
                if cp([s, p]) == p:
                    flag = False
                    s = s[len(p):]
                    break
            if flag:
                print("NO");
                exit()
        print("YES")
    except EOFError:
        exit()

Solution (Python):

import sys
S = input()

while(len(S) > 0):
    if S[0:6] == "eraser":
        S = S[6:]
    elif S[0:5] == "erase":
        S = S[5:]
    elif S[0:5] == "dream":
        tmp = S[5:8]
        if len(tmp) == 0:
            S = S[5:]
        elif tmp == "era":
            S = S[5:]
        elif tmp == "er":
            S = S[7:]
        elif tmp == "ere":
            S = S[7:]
        elif tmp == "erd":
            S = S[7:]
        else:
            S = S[5:]
    else:
        print("NO")
        sys.exit()
print("YES")

Solution (Python):
input_str = input()
new_str = ''.join(list(reversed(input_str)))
candidates = ["dream", "dreamer", "erase", "eraser"]
candidates = [''.join(list(reversed(s))) for s in candidates]

def match_candidates(s, i, candidates):
    for candidate in candidates:
        length = len(candidate)
        if s[i:i+length] == candidate: return length
    return -1

def judge_str(s, candidates):
    i = 0
    while i < len(new_str):
        jump = match_candidates(s, i, candidates)
        if jump == -1:
            return "NO"
        i += jump
    return "YES"
    
print(judge_str(new_str, candidates))

Solution (Python):
def solve(S):
    S = S[::-1]
    for _ in range(len(S) // 5 + 1):
        if S[:5] == "dream"[::-1]:
            S = S[5:]
        if S[:5] == "erase"[::-1]:
            S = S[5:]
        if S[:6] == "eraser"[::-1]:
            S = S[6:]
        if S[:7] == "dreamer"[::-1]:
            S = S[7:]
    if len(S):
        return "NO"
    else:
        return "YES"








if __name__ == "__main__":
    S = input()
    print(solve(S))

Solution (Python):
s = input()

while 1:
    if len(s)<=7:
        if s=='dream' or s=='dreamer' or s=='erase' or s=='eraser':
            print('YES')
            break
    
    elif s[-5:]=='dream':
        s = s[:-5]
        continue
    
    elif s[-7:]=='dreamer':
        s = s[:-7]
        continue
    
    elif s[-5:]=='erase':
        s = s[:-5]
        continue
    
    elif s[-6:]=='eraser':
        s = s[:-6]
        continue
    
    else:
        print('NO')
        break

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

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

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

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

divide = ["dream", "dreamer", "erase", "eraser"]
for i in range(4):
    divide[i] = divide[i][::-1]

length1 = 0
length2 = 0
can = 0
flag = True

while flag:
    length1 = length2
    for i in range(4):
        if S[length1:length1+len(divide[i])] == divide[i]:
            length2 = length1 + len(divide[i])
    if length1 == length2:
        flag = False

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

Solution (Python):
S = input()
S = S[::-1]
while S:
    if S.startswith('maerd'):
        S = S[5:]
    elif S.startswith('remaerd'):
        S = S[7:]
    elif S.startswith('esare'):
        S = S[5:]
    elif S.startswith('resare'):
        S = S[6:]
    else:
        print('NO')
        break
else:
    print('YES')

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

s = ""
for i in range(len(S)):
    s += S[-(i+1)]
    if s[::-1] in a:
        s = ""
    if len(s) > 6:
        print("NO")
        exit()
print("YES")

Solution (Python):
s = list(input())
while len(s) > 0:
    if ''.join(s[-7:]) == 'dreamer':
        del s[-7:]    
    elif ''.join(s[-6:]) == 'eraser':
        del s[-6:]
    elif ''.join(s[-5:]) == 'dream' or ''.join(s[-5:]) == 'erase':
        del s[-5:]
    else:
        break
print('NO' if len(s) != 0 else 'YES')

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

Solution (Python):




def solveProblem(givenStr):

	strT1 = "dream"
	strT2 = "dreamer"
	strT3 = "erase"
	strT4 = "eraser"
	givenStr = givenStr[::-1]
	strList = [strT2[::-1],strT4[::-1],strT1[::-1],strT3[::-1]]
	while len(givenStr) > 0:
		stripFlag = False
		for word in strList:
			givenSubStr=givenStr[0:len(word)]
			if givenSubStr == word:
				givenStr=givenStr.replace(word,"",1)
				stripFlag = True
		if stripFlag == False:
			break
	if len(givenStr) == 0:
		answer="YES"
	else:
		answer="NO"
	return answer

if __name__ == '__main__':
	S = str(input().strip())
	solution = solveProblem(S)
	print("{}".format(solution))

Solution (Python):
s = input()

s = s[::-1]
s = list(s)

count = 0
can = True
jm = []
j = 0
maxcount = 0
for i in s:
    jm.append(i)
    j += 1
    count += 1
    if count > 7:
        can = False

        break
    elif "".join(jm) == "maerd":
        maxcount += count
        count,j = 0,0
        jm = []

    elif "".join(jm) == "remaerd":
        maxcount += count
        count,j = 0,0
        jm = []

    elif "".join(jm) == "esare":
        maxcount += count
        count,j = 0,0
        jm = []

    elif "".join(jm) == "resare":
        maxcount += count
        count,j = 0,0
        jm = []

    else:

        pass




if maxcount == 0:
    can = False

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

Solution (Python):
S = input()

S = S[::-1]

dream = "maerd"
dreamer = "remaerd"
erase = "esare"
eraser = "resare"

if len(S) > 0:
    while len(S) > 0:
        if S.startswith(dream):
            S = S[len(dream):]
        elif S.startswith(dreamer):
            S = S[len(dreamer):]
        elif S.startswith(erase):
            S = S[len(erase):]
        elif S.startswith(eraser):
            S = S[len(eraser):]
        else:
            break
    if len(S) > 0:
        print("NO")
    else:
        print("YES")

else:
    print("NO")

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

Solution (Python):
s=input()
def check(s):
    if s[-5:]=='dream':
        return 'dream'
    elif s[-7:]=='dreamer':
        return 'dreamer'
    elif s[-5:]=='erase':
        return 'erase'
    elif s[-6:]=='eraser':
        return 'eraser'
    else: return 'False'

count=0
while len(s)>0 and count==0:
    if check(s)=='False':
        count+=1
        print('NO')
    elif check(s)=='dream' or check(s)=='erase':
        s=s[:-5]
    elif check(s)=='eraser':
        s=s[:-6]
    elif check(s)=='dreamer':
        s=s[:-7]

if count==0:print('YES')

Solution (Python):
from collections import deque
s = input()
li = ['dream', 'dreamer', 'erase', 'eraser']

ans = 'NO'
Q = deque()
Q.append(0)
while True:
    try:
        k = Q.popleft()
    except:
        break
    if k == len(s):
        ans = 'YES'
        break
    for a in li:
        if a == s[k: k + len(a)]:
            Q.append(k + len(a))

print(ans)

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

while s != '':
    for a in li:
        if s[-len(a):] == a:
            s = s[:-len(a)]
            break
    else:
        break
if s == '':
    print('YES')
else:
    print('NO')

Solution (Python):
s = input()
judge = "YES"

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

if not s.isdigit():
  judge = "NO"
print(judge)

Solution (Python):
s = input()
judge = "YES"

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

if s:
  judge = "NO"

print(judge)

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

Solution (Python):
s=input()
a='YES'
while len(s)!=0:
  if s[len(s)-5:len(s)]=='dream' or s[len(s)-5:len(s)]=='erase':
    s=s[0:len(s)-5]
  elif 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]
  else:
    a='NO'
    break
print(a)

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

Solution (Python):
s = input()
tmp = s
flag=0
while tmp != '':
  if tmp[-7::] =='dreamer':
    tmp = tmp[0:-7]
  elif tmp[-6::] == 'eraser':
    tmp = tmp[0:-6]
  elif tmp[-5::] == 'dream' or tmp[-5::] == 'erase':
    tmp = tmp[0:-5]
  else:
    flag=1
    break
    
if flag ==0:
  print('YES')
else:
  print('NO')

Solution (Python):
a = input()
a = a[::-1]
k = ["dreamer","dream","erase","eraser"]
for i in range(4):
    k[i] = k[i][::-1]
f = 1
i= 0
while f:
    if a[i:i+5] == k[2] or a[i:i+5] == k[1]:
        i +=5
    elif a[i:i+6] == k[3]:
        i +=6
    elif a[i:i+7] == k[0]:
        i+=7
    else:break
if len(a)==i:
    print("YES")
else:
    print("NO")

Solution (Python):
def solve(s):
    stack = [s]
    while stack:
        word = stack.pop()
        if len(word) == 0:
            return True
        for prefix in ('dreamer', 'dream', 'eraser', 'erase'):
            if word.startswith(prefix):
                stack.append(word[len(prefix):])

print('YES' if solve(input()) else 'NO')

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

words = ["dream", "dreamer", "erase", "eraser"]
words = [w[::-1] for w in words]
lens = [len(w) for w in words]

while True:
    for l, w in zip(lens, words):
        if s[:l] == w:
            s = s[l:]
            break
    else:
        print("NO")
        break
    if len(s) == 0:
        print("YES")
        break