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()
s = s[::-1]
while s:
    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');exit()
print('YES')

Solution (Python):
s = str(input())
no = 0
while 1:
    for data in ["erase", "eraser", "dream", "dreamer"]:
        if s.endswith(data):
            s = s[: - len(data)]
        else:
            no += 1
    if not s:
        print("YES")
        exit()
    if no == 4:
        print("NO")
        exit()
    else:
        no = 0

Solution (Python):
s = str(input())
no = 0
while 1:
    for data in ["erase", "eraser", "dream", "dreamer"]:
        if s.endswith(data):
            s = s[: - len(data)]
        else:
            no += 1
    if not s:
        print("YES")
        exit()
    if no == 4:
        print("NO")
        exit()
    else:
        no = 0

Solution (Python):
s = str(input())
no = 0
while 1:
    for data in ["erase", "eraser", "dream", "dreamer"]:
        if s.endswith(data):
            s = s[: - len(data)]
        else:
            no += 1
    if not s:
        break
    if no == 4:
        print("NO")
        exit()
    else:
        no = 0
print("YES")

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

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

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()


s = v()
rev=s[::-1]
can='YES'
while(len(rev)!=0):
    if(rev[:3]=='rem'):
        if(rev[:7]=='remaerd'):
            rev=rev[7:]

        else:
            can='NO'
            break
    
    elif(rev[:3]=='res'):
        if(rev[:6]=='resare'):
            rev=rev[6:]

        else:
            can='NO'
            break
    
    elif(rev[0]=='e'):
        if(rev[:5]=='esare'):
            rev=rev[5:]

        else:
            can='NO'
            break
    elif(rev[0]=='m'):
        if(rev[:5]=='maerd'):
            rev=rev[5:]

        else:
            can='NO'
            break
    else:
        can='NO'
        break

print(can)

Solution (Python):
s=input()
rev=s[::-1]
can='YES'
while(len(rev)!=0):
    if(rev[:3]=='rem'):
        if(rev[:7]=='remaerd'):
            rev=rev[7:]

        else:
            can='NO'
            break
    
    elif(rev[:3]=='res'):
        if(rev[:6]=='resare'):
            rev=rev[6:]

        else:
            can='NO'
            break
    
    elif(rev[0]=='e'):
        if(rev[:5]=='esare'):
            rev=rev[5:]

        else:
            can='NO'
            break
    elif(rev[0]=='m'):
        if(rev[:5]=='maerd'):
            rev=rev[5:]

        else:
            can='NO'
            break
    else:
        can='NO'
        break

print(can)

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

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

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

Solution (Python):
import collections
q = collections.deque()
q.append(input())

cnt = 0

while q:
    x = q.popleft()
    
    if x in ["dream","dreamer","erase","eraser"]:
        cnt += 1
        
    else:
        if 5 < len(x):
            if x[:5] in ["dream","erase"]:
                q.append(x[5:])
                
        if 6 < len(x):
            if x[:6] == "eraser":
                q.append(x[6:])
                
        if 7 < len(x):
            if x[:7] == "dreamer":
                q.append(x[7:])
                
print("YNEOS"[cnt==0::2])

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

Solution (Python):
s = input()
words = ["dreamer", "dream", "eraser", "erase"]
while len(s) > 0:
    match = [s.endswith(i) for i in words]
    if True in match:
        ans = words[match.index(True)]
        s = s[:-len(ans)]
        if len(s) == 0:
            print("YES")
            break
    
    else:
        print("NO")
        break

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

Solution (Python):
s = input()
s = s[::-1]
t = ["dream", "dreamer", "erase", "eraser"]

i = 0
while i < len(s) - 7 + 1:
    for j in t:
        if s[i:i + len(j)] == j[::-1]:
            i += len(j)
            break
    else:
        print("NO")
        break
else:
    for j in t:
        if s[i:] == j[::-1] or s[i:] == "":
            print("YES")
            break
    else:
        print("NO")

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

Solution (Python):
s = input()
t = ''

i = 0

while True:
    if s == t:
        print('YES')
        exit();
    elif s[i:i+7] == 'dreamer' and s[i+7:i+8] != 'a':
        t += 'dreamer' 
        i += 7
    elif s[i:i+5] == 'dream':
        t += 'dream'
        i += 5
    elif s[i:i+6] == 'eraser':
        t += 'eraser'
        i += 6
    elif s[i:i+5] == 'erase':
        t += 'erase'
        i += 5
    else:
        print('NO')
        exit()

Solution (Python):
S = input()

def solve(T) :
    while 1:
        flag = True
        for i in ("dream", "dreamer", "erase", "eraser"):
            if T.endswith(i):
                T = T[: -len(i)]
                flag = False
                break
        
        if flag:
            print("NO")
            break
        
        if len(T) == 0:
            print("YES")
            break

solve(S)

Solution (Python):
S = input()

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

solve(S)

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()
a = s.replace('eraser','')
b = a.replace('erase','')
c = b.replace('dreamer','')
d = c.replace('dream','')
if d == '':
    print('YES')
else:
    print('NO')

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

Solution (Python):
S = input()[::-1]
word1 = "dream"[::-1]
word2 = "dreamer"[::-1]
word3 = "erase"[::-1]
word4 = "eraser"[::-1]
while True:
    if S[:5] == word1:
        S = S[5:]
    elif S[:7] == word2:
        S = S[7:]
    elif S[:5] == word3:
        S = S[5:]
    elif S[:6] == word4:
        S = S[6:]
    elif S == "":
        print("YES")
        break
    else:
        print("NO")
        break

Solution (Python):
S = input()

while len(S) != 0:
    if S[-2:] == 'er':
        S = S[:-2]
        if S[-5:] == 'dream':
            S = S[:-5]
        elif S[-4:] == 'eras':
            S = S[:-4]
        else:
            print('NO')
            exit()
    else:
        if S[-5:] == 'dream' or S[-5:] == 'erase':
            S = S[:-5]
        else:
            print('NO')
            exit()

print('YES')

Solution (Python):
s=list(reversed(list(input())))
n=len(s)
for i in range(6):
    s.append("
d=0
f=0
while f==0 and s[d]!="
    if s[d]+s[d+1]+s[d+2]+s[d+3]+s[d+4]=="maerd":
        d+=5

    elif s[d]+s[d+1]+s[d+2]+s[d+3]+s[d+4]+s[d+5]+s[d+6]=="remaerd":
        d+=7

    elif s[d]+s[d+1]+s[d+2]+s[d+3]+s[d+4]=="esare":
        d+=5
    elif s[d]+s[d+1]+s[d+2]+s[d+3]+s[d+4]+s[d+5]=="resare":
        d+=6
    else:
        f=1
print("YES" if f==0 else "NO")

Solution (Python):
s = input()
sr = s[::-1]
d =  "dream"[::-1]
dr = "dreamer"[::-1]
e = "erase"[::-1]
er = "eraser"[::-1]

while True:
  if len(sr) == 0:
    print("YES")
    break
  elif len(sr) < 5:
    print("NO")
    break
  else:
    if sr[:5] == d:
      sr = sr.replace(d, "", 1)
    elif sr[:5] == e:
      sr = sr.replace(e, "", 1)
    elif sr[:7] == dr:
      sr = sr.replace(dr, "", 1)
    elif sr[:6] == er:
      sr = sr.replace(er, "", 1)
    else:
      print("NO")
      break

Solution (Python):
s=input()[::-1]
a="dream"[::-1]
b="dreamer"[::-1]
c="erase"[::-1]
d="eraser"[::-1]
i=0
ans="YES"
while i<len(s):
    if a in s[i:i+5]:
        i+=5
    elif b in s[i:i+7]:
        i+=7
    elif c in s[i:i+5]:
        i+=5
    elif d in s[i:i+6]:
        i+=6
    else :
        ans="NO"
        break

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    S = input()
    
    while True:
        if S[-5:] == "dream" or S[-5:] == "erase":
            S = S[:-5]
            continue
        elif S[-6:] == "eraser":
            S = S[:-6]
            continue
        elif S[-7:] == "dreamer":
            S = S[:-7]
            continue
        else:
            break
    if S == "":
        print("YES")
    else:
        print("NO")
    return


if __name__ == "__main__":
    main()

Solution (Python):
s = input()
s = s[::-1]
import re
a = "dream"[::-1]
b = "dreamer"[::-1]
c = "erase"[::-1]
d = "eraser"[::-1]

ans = "YES"
i = 0
while(i<len(s)):
  if s[i:i+5] == a:
    i = i+5
  elif s[i:i+7] == b:
    i = i+7
  elif s[i:i+5] == c:
    i = i+5
  elif s[i:i+6] == d:
    i = i+6
  else:
    ans = "NO"
    break
print(ans)

Solution (Python):
S=input()

S = S.replace("dream","w")
S = S.replace("erase","x")
S = S.replace("wer","y")
S = S.replace("xr","z")

if len(S) == S.count("w")+S.count("x")+S.count("y")+S.count("z"):
    print("YES")
else:
    print("NO")

Solution (Python):
s = input()
n = len(s)
idx = n
while idx > 0:
    flag = 0
    if idx >= 7:
        if s[idx - 7 : idx] == "dreamer":
            idx -= 7
            flag = 1
    if idx >= 6:
        if s[idx - 6 : idx] == "eraser":
            idx -= 6
            flag = 1
    if idx >= 5:
        if s[idx - 5 : idx] == "dream" or s[idx - 5 : idx] == "erase":
            idx -= 5
            flag = 1
    if idx < 5:
        break
    if not flag:
        break
if not idx:
    print("YES")
else:
    print("NO")

Solution (Python):
S = input()
S = S[::-1]
a = 'dream'[::-1]
b = 'dreamer'[::-1]
c = 'erase'[::-1]
d = 'eraser'[::-1]
words = [a, b, c, d]
t = 0
for i in range(1, len(S) + 1):
    if S[t:i] in words:
        t = i
if t == len(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 len(s) == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys

s = input()

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

q = ['']

while len(q) != 0:
    front = q.pop(0)

    for c in cand:
        start = len(front)
        end = len(front) + len(c)
        if end <= len(s) and s[len(front):len(front) + len(c)] == c:
            next_elm = front + c
            if next_elm == s:
                print('YES')
                sys.exit(0)
            q.append(next_elm)

print('NO')

Solution (Python):
s = str(input())
alist = ["dream", "dreamer", "erase", "eraser"]
ans = False

def backwards(x):
  return x[::-1]
s = backwards(s)
alist = [backwards(i) for i in alist]

t = ""
start = 0
for i in range(len(s)+1):
  if s[start:i] in alist:
    t+=s[start:i]
    start = i
if s==t:
  print("YES")
else:
  print("NO")

Solution (Python):


S = input()

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

dp = [0] * 100010
dp[0] = 1
for i in range(len(S)):
    if dp[i]==0:
        continue
    for w in words:
        if S[i:i+len(w)] == w:
            dp[i+len(w)] = 1


if dp[len(S)] == 1:
    print('YES')
else:
    print('NO')

Solution (Python):
def main():
    s = input()
    num = 0

    while num < len(s) - 7:
        if s[num:num+6] == "dreamd" or s[num:num+8] == "dreamera":   
            num += 5
        elif s[num:num+8] == "dreamerd" or s[num:num+8] == "dreamere":  
            num += 7
        elif s[num:num+6] == "erased" or s[num:num+6] == "erasee":    
            num += 5
            
        elif s[num:num+7] == "eraserd" or s[num:num+7] == "erasere":    
            num += 6
        else:
            print("NO")
            exit()
    if s[num:num+5] == "dream" or s[num:num+7] == "dreamer" or s[num:num+5] == "erase" or s[num:num+6] == "eraser":
        print('YES')
    else:
        print('NO')


if __name__ == "__main__":
    main()

Solution (Python):
s=input()
n=len(s)
t=n-1
for i in range(n):
    if t>=4 and s[t-4:t+1]=="dream":
        t-=5
    elif t>=6 and s[t-6:t+1]=="dreamer":
        t-=7
    elif t>=4 and s[t-4:t+1]=="erase":
        t-=5
    elif t>=5 and s[t-5:t+1]=="eraser":
        t-=6
    else:
        break
    if t<0:
        break
if t==-1:
    print("YES")
else:
    print("NO")

Solution (Python):


S = input()

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

while len(S)>0:
    hit = False
    for w in words:
        s = S[-len(w):]
        if w == s:
            S = S[0:-len(w)]
            hit = True
            break
    
    if not hit:
        break

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

Solution (Python):
S = input()
i = 0
n = len(S)
while i <= n-5:
    if S[i:i+5] == "dream":
        i += 5
        if S[i:i+2] == "er" and S[i:i+5] != "erase":
            i += 2
    elif S[i:i+5] == "erase":
        i += 5
        if S[i:i+1] == "r":
            i += 1
    else:
        break
if i != len(S):
    print("NO")
else:
    print("YES")

Solution (Python):
s=list(input())
ans="YES"
while len(s)!=0:
  if len(s)<5:
    ans="NO"
    break
  elif s[-1:-6:-1]==["m","a","e","r","d"]:
    for i in range(5):
      s.pop(-1)
  elif s[-1:-8:-1]==["r","e","m","a","e","r","d"]:
    for i in range(7):
      s.pop(-1)
  elif s[-1:-6:-1]==["e","s","a","r","e"]:
    for i in range(5):
      s.pop(-1)
  elif s[-1:-7:-1]==["r","e","s","a","r","e"]:
    for i in range(6):
      s.pop(-1)
  else:
    ans="NO"
    break
print(ans)

Solution (Python):
words = ("dream","dreamer","erase","eraser")
import sys
readline = sys.stdin.readline

S = readline().rstrip()

stack = [0]
while stack:
  ind = stack.pop()
  if ind == len(S):
    print("YES")
    break
  for w in words:
    if S[ind:ind + len(w)] == w:
      stack.append(ind + len(w))
else:
  print("NO")

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

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

T = ''
T_next = ''

while True:
    if len(T) + 5 > len(S):
        break
    for i in range(4):
        S_next = S[len(T):len(T + words[i])]
        T_next = words[i]
        flag = False
        
        
        
        if S_next == T_next:
            flag = True
            T += T_next
            
            
            
            if T == S:
                print('YES')
                exit()
            break
    if flag == False:
        break
    

print('NO')

Solution (Python):
mycode = r'''








from libcpp.string cimport string as Str

ctypedef long long LL


import sys
readline = sys.stdin.buffer.readline().rstrip

cdef LL i,j,k,_


cdef Str S = readline()
cdef Str dream = b'dream'
cdef Str dreamer = b'dreamer'
cdef Str erase = b'erase'
cdef Str eraser = b'eraser'




cdef LL idx=S.size()
while idx>7:
    if S.substr(idx-5,5)==dream: 
        idx-=5 
    elif S.substr(idx-5,5)==erase:
        idx-=5 
    elif S.substr(idx-6,6)==eraser:
        idx-=6
    elif S.substr(idx-7,7)==dreamer:
        idx-=7 
    else: break


if S.substr(0, idx) in [dream, dreamer, erase, eraser]:
    print('YES')
else:
    print('NO')



'''

import sys
if sys.argv[-1] == 'ONLINE_JUDGE':  
    import os
    with open('mycode.pyx', 'w') as f:
        f.write(mycode)
    os.system('cythonize -i -3 -b mycode.pyx')

import mycode

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

L = ['maerd', 'remaerd', 'esare', 'resare']
T = ''
for i in range(len(S) - 1, -1, -1):
    T += S[i]
    if T not in L and len(T) > 7:
        print('NO')
        exit()
    elif T in L:
        T = ''

print('YES')

Solution (Python):
S = str(input())
i = 0
error = 0

while i < len(S) and error == 0:

    if i + 5 > len(S):
        error = -1
        break

    if S[i: i + 5] == 'dream':
        i += 5
        if i + 2 <= len(S):
            if S[i: i + 2] == 'er':
                if (i + 2) == len(S):
                    i += 2
                elif S[i + 2] != 'a':
                    i += 2
    elif S[i: i + 5] == 'erase':
        i += 5
        if i + 1 <= len(S):
            if S[i] == 'r':
                if (i + 1) == len(S):
                    i += 1
                elif S[i + 1] != 'a':
                    i += 1
    else:
        error = -1
        break

if error == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
S=input()
i=len(S)
isOK=True
while len(S[0:i])>0:
    if len(S[0:i])<5:
        isOK=False
        break

    if len(S[0:i])>=7:
        st=S[i-7:i]
        if st=="dreamer":
            i=i-7
            continue
    
    if len(S[0:i])>=6:
        st=S[i-6:i]
        if st=="eraser":
            i=i-6
            continue
    if len(S[0:i])>=5:
        st=S[i-5:i]
        if st=="erase" or st=="dream":
            i=i-5
            continue
        else:
            isOK=False
            break


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

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

Solution (Python):
s = input()
A = [[5, "dream"], [7, "dreamer"], [5, "erase"], [6, "eraser"]]
g = 0
while len(s) > 0:
  f = 0
  for i in range(4):
    if len(s) >= A[i][0]:
      if s[-A[i][0]:] == A[i][1]:
        s = s[:-A[i][0]]
        f = 1
        break
  if f == 0:
    print("NO")
    g = 1
    break
if g == 0:
  print("YES")

Solution (Python):
import math
from math import gcd,pi,sqrt
INF = float("inf")

import sys
sys.setrecursionlimit(10**6)
import itertools
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    s = list(input())
    s.reverse()
    len_s = len(s)
    t = 0
    while True:
        if t == len_s:
            print("YES")
            exit()
        elif t > len_s:
            break
        else:
            if "".join(s[t:t+5]) == "maerd":
                t += 5
            elif "".join(s[t:t+7]) == "remaerd":
                t += 7
            elif "".join(s[t:t+5]) == "esare":
                t += 5
            elif "".join(s[t:t+6]) == "resare":
                t += 6
            else:
                break
    print("NO")


if __name__=="__main__":
    main()

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    return C

S=input()
N=len(S)
ans=True
i=N
while ans:
    if i==0:
        break
    elif i>=5 and S[i-5:i]=='dream':
        i-=5
    elif i>=7 and S[i-7:i]=='dreamer':
        i-=7
    elif i>=5 and S[i-5:i]=='erase':
        i-=5
    elif i>=6 and S[i-6:i]=='eraser':
        i-=6
    else:
        ans=False
judge(ans,['YES','NO'])

Solution (Python):
S=input()
N=len(S)
strings = ["maerd", "remaerd", "esare", "resare"]
tmp = ""
for i in range(N-1, -1, -1):
    if tmp not in strings:
        tmp += S[i]
        if tmp in strings:
            tmp = ""
if tmp:
    print("NO")
else:
    print("YES")

Solution (Python):

url = "https://atcoder.jp//contests/abc049/tasks/arc065_a"

def main():
    words = ['dream','dreamer', 'erase', 'eraser']
    s = input()
    rs = s[::-1]
    while rs != '':
        tmp = len(rs)
        for word in words:
            if len(word) > len(rs):
                continue
            rs = rs[0:len(word)].replace(word[::-1], '') + rs[len(word):]
        if tmp == len(rs):
            print('NO')
            exit()
    print('YES')

if __name__ == '__main__':
    main()

Solution (Python):
s = input()
n = len(s)

i = n
while i > 0:
    if i >= 7 and s[i-7:i] == 'dreamer':
        i -= 7
    elif i >= 5 and s[i-5:i] == 'dream':
        i -= 5
    elif i >= 6 and s[i-6:i] == 'eraser':
        i -= 6
    elif i >= 5 and s[i-5:i] == 'erase':
        i -= 5
    else:
        print('NO')
        exit()
print('YES')

Solution (Python):
s = input().replace("eraser","").replace("erase","").replace("dreamer","").replace("dream","")
print('NO' if s else '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 = 'HAKUTYUMU' + input()
 
 
while s != 'HAKUTYUMU':
    next_s = s
 
    if s[-5:] == 'dream':
        next_s = s[:-5]
    if s[-7:] == 'dreamer':
        next_s = s[:-7]
    if s[-5:] == 'erase':
        next_s = s[:-5]
    if s[-6:] == 'eraser':
        next_s = s[:-6]
 
    if s == next_s:
        print('NO')
        exit()
 
    s = next_s
 
print('YES')

Solution (Python):
s = 'HAKUTYUMU' + input()


while s != 'HAKUTYUMU':
    next_s = s

    if s[-5:] == 'dream':
        next_s = s[:-5]
    if s[-7:] == 'dreamer':
        next_s = s[:-7]
    if s[-5:] == 'erase':
        next_s = s[:-5]
    if s[-6:] == 'eraser':
        next_s = s[:-6]

    if s == next_s:
        print('NO')
        exit()

    s = next_s

print('YES')

Solution (Python):
import sys
sys.setrecursionlimit(10000)

s = 'HAKUTYUMU' + input()


while s != 'HAKUTYUMU':
    next_s = s

    if s[-5:] == 'dream':
        next_s = s[:-5]
    if s[-7:] == 'dreamer':
        next_s = s[:-7]
    if s[-5:] == 'erase':
        next_s = s[:-5]
    if s[-6:] == 'eraser':
        next_s = s[:-6]

    if s == next_s:
        print('NO')
        exit()

    s = next_s

print('YES')

Solution (Python):
s = input()
l = len(s)

idx = 0
ans = "NO"
dream = False
erase = False
while idx<l:
    if s[idx]=="d":
        if s[idx:(idx+5)] == "dream":
            idx += 5
            dream = True
            erase = False
        else:
            break
    elif s[idx] == "e":
        if s[idx+1] != "r":
            break
        else:
            if s[idx:(idx+5)] == "erase":
                idx += 5
                erase = True
                dream = False
            elif dream:
                idx += 2
                erase = False
                dream = False
            else:
                break
    elif s[idx]== "r":
        if erase:
            idx += 1
            erase = False
            dream = False
        else:
            break
    else:
        break

if idx == l:
    ans = "YES"

print(ans)

Solution (Python):
s=input()
t=""
i=len(s)
five=["dream","erase"]
flag=0
while s!=t:
  if i>=7 and s[i-7:i]=="dreamer":
    t=s[i-7:i]+t
    i-=7
  elif  i>=6 and s[i-6:i]=="eraser":
    t=s[i-6:i]+t
    i-=6
  elif i>=5 and (s[i-5:i] in five):
    t=s[i-5:i]+t
    i-=5
  else:
    flag=1
    break
if flag==0:
  print("YES")
else:
  print("NO")

Solution (Python):
def solve():
    S = input()[::-1]
    i = 0
    while i < len(S):
        if S[i:i+5] in ['maerd', 'esare']:
            i += 5
        elif S[i:i+6] == 'resare':
            i += 6
        elif S[i:i+7] == 'remaerd':
            i += 7
        else:
            return 'NO'
    return 'YES'

    
if __name__ == '__main__':
    ans = solve()
    print(ans)

Solution (Python):

import sys
from typing import List


def fast_input():
    return sys.stdin.readline()[:-1]


def print_format(b: bool) -> str:
    return "YES" if b else "NO"


def solve(s: str, tokens: List[str], dp_max: int) -> bool:
    dp = [False] * dp_max
    dp[0] = True

    for s_ind in range(len(s)):
        if not dp[s_ind]:
            continue
        for token in tokens:
            if s[s_ind:s_ind + len(token)] == token:
                dp[s_ind + len(token)] = True
    return dp[len(s)]


def main():
    tokens = ["dream", "erase", "eraser", "dreamer"]
    s = fast_input()
    result = solve(s=s, tokens=tokens, dp_max=len(s) + 6)
    print(print_format(result))


main()

Solution (Python):

import sys
from typing import List


def fast_input():
    return sys.stdin.readline()[:-1]


def print_format(b: bool) -> str:
    return "YES" if b else "NO"


def solve(s: str, tokens: List[str], s_max: int) -> bool:
    dp = [False] * s_max
    dp[0] = True

    for s_ind in range(len(s)):
        if not dp[s_ind]:
            continue
        for token in tokens:
            if s[s_ind:s_ind + len(token)] == token:
                dp[s_ind + len(token)] = True
    return dp[len(s)]


def main():
    tokens = ["dream", "erase", "eraser", "dreamer"]
    s = fast_input()
    result = solve(s=s, tokens=tokens, s_max=len(s) + 6)
    print(print_format(result))


main()

Solution (Python):

import itertools
import sys


def fast_input():
    return sys.stdin.readline()[:-1]


def print_format(b: bool) -> str:
    return "YES" if b else "NO"


def solve(s: str) -> bool:
    for p in itertools.permutations(["dreamer", "eraser", "dream", "erase"]):
        work = s
        for token in p:
            work = work.replace(token, '')
        if work == "":
            return True
    return False


def main():
    result = solve(fast_input())
    print(print_format(result))


main()

Solution (Python):
S = input()
dp = [False] * (len(S) + 1)
dp[0] = True
for i in range(len(S)+1):
    if i >= 5 and dp[i-5] and S[i-5:i] == 'erase':
        dp[i] = True
    if i >= 6 and dp[i-6] and S[i-6:i] == 'eraser':
        dp[i] = True
    if i >= 5 and dp[i-5] and S[i-5:i] == 'dream':
        dp[i] = True
    if i >= 7 and dp[i-7] and S[i-7:i] == 'dreamer':
        dp[i] = True
print('YES' if dp[len(S)] else 'NO')

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

Solution (Python):
S = input()
while(len(S) >= 5):
  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(len(S) == 0):
  print("YES")
else:
  print("NO")