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()

import re
output ="NO"
s = re.sub('dreameraser', 'A', s)
s = re.sub('dreamerase', 'A', s)
s = re.sub('dreamer', 'A', s)
s = re.sub('eraser', 'A', s)
s = re.sub('erase', 'A', s)
s = re.sub('dream', 'A', s)
if s.isupper() :
    output = "YES"
print(output)

Solution (Python):
s = input()

import re
output ="NO"
s = re.sub('eraser', '', s)
s = re.sub('erase', '', s)
s = re.sub('dreamer', '', s)
s = re.sub('dream', '', s)
if s == "":
    output = "YES"
print(output)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

S = list(input())

def check(s):
	
	if s == []:
		print("YES")
	elif s[-7:] == list("dreamer"):
		del s[-7:]
		check(S)
	elif s[-6:] == list("eraser"):
		del s[-6:]
		check(s)
	elif any(s[-5:] == list(x) for x in ["dream", "erase"]):
		del s[-5:]
		check(s)
	else:
		print("NO")

check(S)

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

now = 0
while now < len(s):
    if s[now:now + 7] == 'dreamer'[::-1]:
        now += 7
    elif s[now:now + 6] == 'eraser'[::-1]:
        now += 6
    elif s[now:now + 5] == 'dream'[::-1]:
        now += 5
    elif s[now:now + 5] == 'erase'[::-1]:
        now += 5
    else:
        print('NO')
        exit()

print('YES')

Solution (Python):
import re

def main():
    s = list(input())
    sr = ''.join(list(reversed(s)))
    t = ['dream', 'dreamer', 'erase', 'eraser']
    tr = list()
    for i in t:
        tr.append(''.join(list(reversed(i))))
    while sr:
        flag = False
        for i in tr:
            if sr.startswith(i):
                sr = sr.replace(i ,'', 1)
                flag = True
        if not flag:
            print('NO')
            break
        if sr == '':
            print('YES')
            break
if __name__ == '__main__':
    main()

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()

s1 = s.replace("eraser", 'xxx')
s2 = s1.replace("erase", 'xxx')
s3 = s2.replace("dreamer", 'xxx')
s4 = s3.replace("dream", 'xxx')

s5 = s4.strip('x')

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

Solution (Python):
S = input()
S = S[::-1]
a1= 'eraser'
a2= 'erase'
a3= 'dreamer'
a4= 'dream'
a1= a1[::-1]
a2= a2[::-1]
a3= a3[::-1]
a4= a4[::-1]

a = S.replace(a1, '').replace(a2, '').replace(a3, '').replace(a4, '')

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

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


def reverse(t):
    return t[::-1]


RT = [reverse(t) for t in T]
RS = reverse(S)
can = True
i = 0
while i < len(RS):
    can2 = False
    for j in range(4):
        sizeT = len(RT[j])
        if RT[j] == RS[i:i + sizeT]:
            i += sizeT
            can2 = True
            break

    if not can2:
        can = False
        break

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

Solution (Python):
s = input()

s2 = s
word_list = ['dream', 'dreamer', 'erase', 'eraser']
while len(s2) > 0:
  match_flg = False
  for word in word_list:
    if s2[-len(word):] == word:
      s2 = s2[:(len(s2)-len(word))]
      match_flg = True
      break
  if not match_flg:
    break
if len(s2) == 0:
  print('YES')
else:
  print('NO')

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

Solution (Python):
S = input()

while S:
    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]
    else:
        break


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

Solution (Python):
import sys
import math
import numpy as np
import copy

def main():
    s = input()

    a, b, c, d = "dream", "dreamer", "erase", "eraser"
    ra, rb, rc, rd = a[::-1], b[::-1], c[::-1], d[::-1]
    rs = s[::-1]

    while rs:
        flag = False
        for ele in [ra, rb, rc, rd]:
            if rs.startswith(ele):
                rs = rs.replace(ele, "", 1)
                flag = True
        if not flag:
            print("NO")
            return 0
    
    print("YES")
    return 0

if __name__ == '__main__':
    sys.exit(main())

Solution (Python):
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
from math import cos,sin,tan
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf

s = sinput()

a = "dream"
b = "dreamer"
c = "erase"
d = "eraser"

s = "".join(s.split(d))
s = "".join(s.split(c))
s = "".join(s.split(b))
s = "".join(s.split(a))

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

Solution (Python):
import numpy as np
import scipy.sparse as sps
import scipy.misc as spm
import collections as col
import functools as func
import itertools as ite
import fractions as frac
import math as ma
from math import cos,sin,tan
import cmath as cma
import copy as cp
import sys
def sinput(): return sys.stdin.readline().strip()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(n=0):
    if n: return [0 for _ in range(n)]
    else: return list(imap())
def farr(): return list(fmap())
def sarr(n=0):
    if n: return ["" for _ in range(n)]
    else: return sinput().split()
def barr(n): return [False for _ in range(n)]
sys.setrecursionlimit(10**7)
MOD = 10**9 + 7; EPS = sys.float_info.epsilon
PI = np.pi; EXP = np.e; INF = np.inf

s = sinput()[::-1]

a = "dream"[::-1]
b = "dreamer"[::-1]
c = "erase"[::-1]
d = "eraser"[::-1]

s = "".join(s.split(d))
s = "".join(s.split(c))
s = "".join(s.split(b))
s = "".join(s.split(a))

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

Solution (Python):
S=input()

reverse_S=S[::-1]

while True:
    if (reverse_S.find('maerd')==0 or
        reverse_S.find('esare')==0
        ):
        reverse_S=reverse_S[5:]

    elif reverse_S.find('remaerd')==0:
        reverse_S=reverse_S[7:]
        
    elif reverse_S.find('resare')==0:
        reverse_S=reverse_S[6:]

    else:
        break

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

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

Solution (Python):

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

while True:
  for i in range(4):
    if s.endswith(words[i]):
      s = s[:-len(words[i])]
      break 
  else:
    print('NO')
    break 
  if not s: 
    print('YES')
    break

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

Solution (Python):
import math
import bisect
from queue import Queue
from collections import deque

s = input()

dp = [0 for _ in range(len(s)+1)]

dp[0] = 1

for i in range(len(s)+1):
    if(dp[i] == 1):
        if(i+4 < len(s)):
            if("".join(s[i:i+5]) == "dream"):
                dp[i+5] = 1
            if ("".join(s[i:i + 5]) == "erase"):
                dp[i+5] = 1
        if (i + 6 < len(s)):
            if ("".join(s[i:i + 7]) == "dreamer"):
                dp[i + 7] = 1
        if (i + 5 < len(s)):
            if ("".join(s[i:i + 6]) == "eraser"):
                dp[i + 6] = 1

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

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

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

key = 0

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

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

X = ['dream', 'dreamer', 'erase', 'eraser']
X = [s[::-1] for s in X]
i = 0
while i < len(S):
  if i+5 <= len(S) and S[i+2] == 'e':
    if S[i:i+5] == X[0]:
      i += 5
    else:
      print('NO')
      break
  elif i+7 <= len(S) and S[i+2] == 'm':
    if S[i:i+7] == X[1]:
      i += 7
    else:
      print('NO')
      break
  elif i+5 <= len(S) and S[i+2] == 'a':
    if S[i:i+5] == X[2]:
      i += 5
    else:
      print('NO')
      break
  elif i+6 <= len(S) and S[i+2] == 's':
    if S[i:i+6] == X[3]:
      i += 6
    else:
      print('NO')
      break
  else:
    print('NO')
    break
else:
  print('YES')

Solution (Python):
s = input()
words = ['maerd', 'remaerd', 'esare', 'resare']
reverse_s = list(reversed(s))
current = 0

while current < len(s):
    diff = 0
    for word in words:
        if "".join(reverse_s[current:current+len(word)]) == word:
            diff = len(word)
            break
    if diff == 0:
        break
    current += diff
if current == len(s):
    print('YES')
else:
    print("NO")

Solution (Python):
import re
s = input()
str = ["dream","dreamer","erase","eraser"]
str = ["maerd","remaerd","esare","resare"]
s=s[::-1]
while len(s)>0:
  if len(s) < 5:
    print("NO")
    break
  if s[0:7] == str[1] :
    s=s[7:]
  elif s[0:6] == str[3]:
    s=s[6:]
  elif s[0:5] == str[0] or s[0:5] == str[2]:
    s=s[5:]
  else:
    print("NO")
    break
  if len(s)==0:
    print("YES")
    break

Solution (Python):
s = input()
def solve(query):
    while 1:
        for frag in ("erase", "eraser", "dream", "dreamer"):
            if query.endswith(frag):
                query = query[:-len(frag)]
                break
        else:
            print("NO")
            break
        if not query:
            print("YES")
            break

solve(s)

Solution (Python):
S = input()

divide = True
while len(S) > 0:
    if S[-5:] in ["dream", "erase"]:
        S = S[:-5]
    elif S[-6:] == "eraser":
        S = S[:-6]
    elif S[-7:] == "dreamer":
        S = S[:-7]
    else:
        divide = False
        break
        
if divide:
    print("YES")
else:
    print("NO")

Solution (Python):
S = input()
while len(S) >= 5:
    if S[:7] == 'dreamer':
        S = S[11:] if S[7:11] == 'aser' else S[10:] if S[7:10] == 'ase' else S[7:]
    elif S[:6] == 'eraser':
        S = S[6:]
    elif S[:5] in ['dream','erase']:
        S = S[5:]
    else:
        break
print('YES') if S == '' else print('NO')

Solution (Python):
S = input().strip()[::-1]
div = [i[::-1] for i in ["dream", "dreamer", "erase", "eraser"]]

idx = 0
ok = False
while True:
    sub_ok = False
    for d in div:
        if S[idx:idx+len(d)] == d:
            idx += len(d)
            sub_ok = True
            break
    if sub_ok != True:
        break
    if idx == len(S):
        ok = True
        break

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

Solution (Python):

S = input()
S2 = S
W_L = ["dream","dreamer","erase","eraser"]
while True:
    cnt = 0
    for w in W_L:
        if S2.endswith(w):
            S2 = S2[:-(len(w))]
            cnt+=1
    if cnt ==0:
        print('NO')
        exit()
            
    if len(S2)<=7:
        break
    
if S2 ==''or S2 in W_L:
    print('YES')
else:
    print('NO')

Solution (Python):

import re
s = input()


while True:
    

    l = len(s)
    if s.rfind('dream') == l-5:
        s = s[:-5]
    elif s.rfind('dreamer') == l-7:
        s = s[:-7]
    elif s.rfind('erase') == l-5:
        s = s[:-5]
    elif s.rfind('eraser') == l-6:
        s = s[:-6]
    else:
        break
        
if len(s)==0:
    print('YES')
else:
    print('NO')

Solution (Python):
def reverse_str(s):
    return ''.join(list(s)[::-1])


words = list(map(reverse_str, 'dream dreamer erase eraser'.split()))
S = reverse_str(input())
ok = True
while S:
    for r in range(5, 8):
        tmp = S[:r]
        if tmp in words:
            
            S = S[r:]
            
            break
    else:
        ok = False
        break
print('YES' if ok else 'NO')

Solution (Python):
from math import *

def cin(): 
    return map(int,input().split())

def cins(): 
    return input.split()

def cino(test=False): 
    if not test:
        return int(input())
    else: 
        return input()

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

def ssplit(): 
    return list(input().split())

def printlist(l): 
    for i in l:
        print(i,end=" ")

def main():
    a = cino(True)
    check = True
    
    
    i = len(a)-1
    
    while i>=0:
        
        if a[i-5:i+1][::-1]=="resare":
            
            
            i-=6
        elif a[i-6:i+1][::-1]=="remaerd":
            
            i-=7
        elif a[i-4:i+1][::-1] in {"esare","maerd"}:
            
            i-=5
        else:
            check = False
            break
    if check:
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    main()

Solution (Python):
S = input()

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

for i in m:
    S = S.replace(i,"")


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

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

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

for i in m:
    S = S.replace(i[::-1],"")


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

Solution (Python):
S=input()
T=[]

l=-1
while l-2>=-len(S):
    k=S[l:l-3:-1]
    if k=="mae":
        T+="dream"[::-1]
        l-=5
    elif k=="rem":
        T+="dreamer"[::-1]
        l-=7
    elif k=="esa":
        T+="erase"[::-1]
        l-=5
    elif k=="res":
        T+="eraser"[::-1]
        l-=6
    else:
        break
if S=="".join(T[::-1]):
    print("YES")
else:
    print("NO")

Solution (Python):
import sys

input = sys.stdin.readline

dct = ['dream', 'dreamer', 'erase', 'eraser']
rdct = [''.join(reversed(x)) for x in dct]

def main():
    S = input()[:-1]
    rS = ''.join(reversed(S))

    fr = 0
    for _ in range(20005):
        flag = False
        for d in rdct:
            to = fr + len(d)
            if d == rS[fr:to]:
                fr = to
                flag = False
                break
            flag = True
        if flag:
            break
    
    if fr == len(S):
        print('YES')
    else:
        print('NO')
    
if __name__ == "__main__":
    main()

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

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

Solution (Python):
s = str(input())
s1 = s[::-1]
u = ['dream','dreamer','erase','eraser']

while True:
    restart = False
    for i in range(4):
        if u[i][::-1] == s1[:len(u[i])]:
            s1 = s1[len(u[i]):]
            restart = True
            break
    if restart == False:
        break
if len(s1) > 0:
    print('NO')
else:
    print('YES')

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

Solution (Python):
S = input()

while True:
    if len(S) == 0:
        print('YES')
        exit()
    if len(S) < 5:
        print('NO')
        exit()
    if S[:5] == 'dream':
        if len(S) > 6 and S[5:7] == 'er':
            if len(S) > 7 and S[7] == 'a':
                S = S[5:]
            else :
                S = S[7:]
        else :
            S = S[5:]
    elif S[:5] == 'erase':
        if len(S) > 5 and S[5] == 'r':
            S = S[6:]
        else :
            S = S[5:]
    else :
        print('NO')
        exit()

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

DEBUG = False

if DEBUG:
    print("S: {}".format(S))
while True:
    if DEBUG:
        print("S[:7]: {}".format(S[:7]))
    if len(S) >= 5:
        if S[:7] == "remaerd":
            S = S[7:]
        elif S[:6] == "resare":
            S = S[6:]
        elif S[:5] == "maerd" or S[:5] == "esare":
            S = S[5:]
        else:
            print("NO")
            exit()
    else:
        break
if DEBUG:
    print("S: {}".format(S))
if len(S) == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
s = list(input())
n = len(s)
temp = ['maerd', 'remaerd', 'esare', 'resare']
s.reverse()

i = 0
while i < n:
    flag = False
    for t in temp:
        if s[i:i+len(t)] == list(t):
            i += len(t)
            flag = True
            break

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

print("YES")

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

Solution (Python):
s = input()

while True:
    if 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
    elif len(s) == 0:
        print('YES')
        break
    else:
        print('NO')
        break

Solution (Python):
S = input()
L = ['dream', 'dreamer', 'erase', 'eraser']
for i in range(len(L)):
    L[i] = L[i][::-1]
S = S[::-1]
T = ''
for s in S:
    T += s
    for i in L:
        if T == i:
            T = ''
if T == '':
    print('YES')
else:
    print('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):


import sys






























def main():
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())


    def rev_strip(s):
        dream_rev = ''.join(reversed(list('dream')))
        dreamer_rev = ''.join(reversed(list('dreamer')))
        erase_rev = ''.join(reversed(list('erase')))
        eraser_rev = ''.join(reversed(list('eraser')))
        if s[:5] == dream_rev:
            return True, s[5:]
        elif s[:7] == dreamer_rev:
            return True, s[7:]
        elif s[:5] == erase_rev:
            return True, s[5:]
        elif s[:6] == eraser_rev:
            return True, s[6:]
        else:
            return False, ''
    
    def solve(s):
        while s:
            result, s = rev_strip(s)
            if not result:
                return False
        return True
    
    
    s = input()
    s_rev = ''.join(reversed(list(s)))
    print('YES') if solve(s_rev) else print('NO')



if __name__ == "__main__":
    main()

Solution (Python):

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

S = input()
words = ('dream', 'dreamer', 'erase', 'eraser')

while True:
    if S == '':
        print("YES")
        break

    for word in words:
        if S.endswith(word):
            S = S[:-len(word)]
            break
    else:
        print("NO")
        break

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

Solution (Python):






import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache






MOD = 10**9 +7
INF = float('inf')






def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr


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





S = inputS()

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

while S:
  flag = 0
  for i in range(4):
    word = l[i]
    if S[-len(word):] == word:
      S = S[:-len(word)]
      flag = 1
  if flag == 0:
    print("NO")
    exit()

print("YES")

Solution (Python):
s = input()

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

cnt = 0
t = ""
while cnt <= len(s) - 8:
    if s[cnt] == "d":
        if s[cnt+5] == "d":
            cnt += 5
            t += vocabs[0]
        elif s[cnt+5] == "e":
            if s[cnt+7] == "a":
                cnt += 5
                t += vocabs[0]
            else:
                cnt += 7
                t += vocabs[1]
        else:
            break
    elif s[cnt] == "e":
        if s[cnt+5] == "r":
            cnt += 6
            t += vocabs[3]
        else:
            cnt += 5
            t += vocabs[2]
    else:
        break

if s[cnt:] == vocabs[0]:
    t+= vocabs[0]
elif s[cnt:] == vocabs[1]:
    t+= vocabs[1]
elif s[cnt:] == vocabs[2]:
    t+= vocabs[2]
elif s[cnt:] == vocabs[3]:
    t+= vocabs[3]


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

Solution (Python):
import sys
S=list(input())
S.reverse()
S="".join(S)


while S:
  if len(S)<5:
    print("NO")
    sys.exit(0)  
  
  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")
    sys.exit(0)
    
print("YES")

Solution (Python):
s = input()


def solve(x):
    while 1:
        for frag in ("erase", "eraser", "dream", "dreamer"):
            if x.endswith(frag):
                x = x[:-len(frag)]
                break
        else:
            print("NO")
            break
        if not x:
            print("YES")
            break



solve(s)

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

Solution (Python):
def main():
  S = input()
  while len(S)>=5:
    if len(S)>=7 and S[-7:]=="dreamer":
      S = S[:-7]
      continue
    
    elif 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')
    
main()

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

while len(S) > 0:
  a = len(S)
  for i in k:
    if S[-1*len(i):] == i:
      S = S[0:-1*len(i)]
      break
  if a == len(S):
    break
  elif len(S) == 0:
    ans="YES"
print(ans)

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

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

for i in range(4):
    char = divide[i][::-1]
    line[i] = char
s_str = S[::-1]
char = ''
for i in range(len(s_str)):
    char += s_str[i]
    if char in line:
        char = ''
if char == '':
    print('YES')
else:
    print('NO')

Solution (Python):
S = input()
str_list = ['dream', 'erase', 'dreamer', 'eraser']
start = 0
success_flag = False
continue_flag = False


def reverse_str(var: str):
    return ''.join(list(reversed(var)))


reverse_S = reverse_str(S)
while True:
    for str_name in str_list:
        if reverse_S[start:start + len(str_name)] == reverse_str(str_name):
            if len(S) == start + len(str_name):
                success_flag = True
                break
            start += len(str_name)
            continue_flag = True
    if success_flag:
        print('YES')
        break
    if not continue_flag:
        print('NO')
        break
    else:
        continue_flag = False

Solution (Python):
s = input()

length = len(s)
t = ''
word = ''

for a in range(length-1, -1, -1):
    t += s[a]
    if t == 'maerd' or t == 'remaerd' or t == 'esare' or t == 'resare':
        word += t
        t = ''

if s == word[::-1]:
    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 = str(input())
S = S[::-1]
D = ["dream", "dreamer", "erase", "eraser"]
for i, d in enumerate(D):
    D[i] = d[::-1]

can = True
i = 0
while i<len(S):
    can2 = False
    for d in D:
        if S[i:i+len(d)]==d:
            can2 = True
            i += len(d)
    if can2!=True:
        can = False
        break

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

Solution (Python):

import sys
import re
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    YESNO(re.match(r'^(dream(er)?|eraser?)+$',input()))

if __name__ == '__main__':
    main()

Solution (Python):
import sys

S = sys.stdin.readline().strip()

S = S[::-1]
i = 0
while i < len(S):
    if S[i:i+5] == "maerd":
        i += 5
    elif S[i:i+7] == "remaerd":
        i += 7
    elif S[i:i+5] == "esare":
        i += 5
    elif S[i:i+6] == "resare":
        i += 6
    else:
        break

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

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

Solution (Python):
from collections import deque

def bfs(string):
    can = ["dream", "dreamer", "erase", "eraser"]
    queue = deque([])
    queue.append("dream")
    queue.append("dreamer")
    queue.append("erase")
    queue.append("eraser")
    while queue:
        judge = queue.popleft()
        if judge == string:break
        for temp in can:
            ans = judge + temp
            if ans == string[:len(ans)]:
                queue.append(ans)
    return "YES" if judge == string else "NO"

if __name__ == '__main__':
    s = input()
    print(bfs(s))

Solution (Python):
def judge(s):
    flag = True
    s = s[::-1]
    ans = ""
    pivot = 0
    while pivot < len(s):
        if s[pivot] == "m":
            ans += "maerd"
            pivot += 5
        elif s[pivot] == "e":
            ans += "esare"
            pivot += 5
        elif s[pivot:pivot+3] == "rem":
            ans += "remaerd"
            pivot += 7
        elif s[pivot:pivot+3] == "res":
            ans += "resare"
            pivot += 6
        else:
            flag = False
            break
    if flag == False:
        return "NO"
    elif s == ans:
        return "YES"
    else:
        return "NO"

if __name__ == '__main__':
    s = input()
    print(judge(s))

Solution (Python):
target = input()

while len(target)>=5:
    if target[-7:] == "dreamer":
        target = target[:-7]
    elif target[-6:] == "eraser":
        target = target[:-6]
    elif target[-5:]=="dream" or target[-5:]=="erase":
        target = target[:-5]
    else:
      break
if len(target)==0:
    print("YES")
else:
    print("NO")

Solution (Python):
S = input()
dream = "dream"
dreamer = "dreamer"
erase = "erase"
eraser = "eraser"


while True:
    if S.find(dream, len(S) - len(dream), len(S)) == len(S) - len(dream):
        S = S[:len(S) - len(dream)]
        continue
    elif S.find(dreamer, len(S) - len(dreamer), len(S)) == len(S) - len(dreamer):
        S = S[:len(S) - len(dreamer)]
        continue
    elif S.find(erase, len(S) - len(erase), len(S)) == len(S) - len(erase):
        S = S[:len(S) - len(erase)]
        continue
    elif S.find(eraser, len(S) - len(eraser), len(S)) == len(S) - len(eraser):
        S = S[:len(S) - len(eraser)]
        continue
    elif len(S) == 0:
        print("YES")
        break
    else:
        print("NO")
        break