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()
N = len(S)
words = ['dream', 'dreamer', 'erase', 'eraser']
stack = [0]
ans = "NO"
while stack:
  ind = stack.pop()
  if ind == N:
    ans = 'YES'
    break
  for w in words:
    end = ind+len(w)
    if S[ind:end] == w:
      stack.append(end)
print(ans)

Solution (Python):
s=input()
s=s[::-1]
tmp=['dream','dreamer', 'erase', 'eraser']
for i in range(4):
  tmp[i]=tmp[i][::-1]
while 1:
  if len(s)>=5:
    if s[:5]==tmp[0]:
      if len(s)>5:
        s=s[5:]
      else:
        s=[]
      continue
    if s[:5]==tmp[2]:
      if len(s)>5:
        s=s[5:]
      else:
        s=[]
      continue
    if len(s)>=6:
      if s[:6]==tmp[3]:
        if len(s)>6:
          s=s[6:]
        else:
          s=[]
        continue
      if len(s)>=7:
        if s[:7]==tmp[1]:
          if len(s)>7:
            s=s[7:]
          else:
            s=[]
          continue
  break

if s==[]:
  print('YES')
else:
  print('NO')

Solution (Python):
S = input()
N = len(S)
i = 0
while i < N:
    sig = 0
    S5 = S[i:i+5]
    if S5 == 'dream':
        sig = 1
        if S[i+5:i+7] == 'er' and S[i+7:i+8] != 'a':
            i += 2
        i += 5
    elif S5 == 'erase':
        sig = 1
        if S[i+5:i+6] == 'r':
            i += 1
        i += 5
    if sig == 0:
        sig = 2
        Answer = 'NO'
        break
if sig != 2:
    Answer = 'YES'
print(Answer)

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

while i<n:
  if s[i:i+10]=="dreamerase":
    i+=5
  elif s[i:i+7]=="dreamer":
    i+=7
  elif s[i:i+5]=="dream":
    i+=5
  elif s[i:i+6]=="eraser":
    i+=6
  elif s[i:i+5]=="erase":
    i+=5
  else:
    break
print("YES" if i==n else "NO")

Solution (Python):
s=input()
s=s.replace('eraser','
s=s.replace('erase','
s=s.replace('dreamer','
s=s.replace('dream','
f=0
for i in s:
    if(i!='
        f=1
        break
if(f==0):
    print("YES")
else:
    print("NO")

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

Solution (Python):
s=input()
s=s.replace('eraser','
s=s.replace('erase','
s=s.replace('dreamer','
s=s.replace('dream','
f=1
for it in s:
    if it !='
        f=0
        break

if f==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","*")
f=1
for i in s:
    if i!='*':
        f=0
        break
if f:
    print("YES")
else:
    print("NO")

Solution (Python):
S = input()
pnt = len(S)
while pnt:
  tmp = pnt
  if pnt >= 7:
    if S[pnt-7:pnt] == "dreamer":
      pnt -= 7
  if pnt >= 6:
    if S[pnt-6:pnt] == "eraser":
      pnt -= 6
  if pnt >= 5:
    if S[pnt-5:pnt] == "dream" or S[pnt-5:pnt] == "erase":
      pnt -= 5
  if tmp == pnt:
    break
if pnt:
  print("NO")
else:
  print("YES")

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    
    S = input().rstrip()

    limit = len(S)//5

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

if __name__=='__main__':
    main()

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

Solution (Python):
s=(input())[::-1]
T=["dream", "dreamer", "erase","eraser"]
T=[x[::-1] for x in T]
cur=""
for e in s:
    cur+=e
    if(cur in T):
        cur=""
print("YES" if cur=="" else "NO")

Solution (Python):
from itertools import product, takewhile


def main():
    words = ["eraser", "erase",  "dreamer", "dream"]
    S = input()[::-1]
    for word in words:
        S = S.replace(word[::-1], '')
    
    if len(S) > 0:
        print("NO")
    else:
        print("YES")


if __name__ == '__main__':
    main()

Solution (Python):


def II(): return int(input())
def MII(): return map(int, input().split())
def LII(): return list(map(int, input().split()))

def main():
    S = input()
    S = S[::-1]
    d = ['maerd', 'remaerd', 'esare', 'resare']

    f = False
    while True:
        for di in d:
            if S.startswith(di):
                f = False
                S = S.replace(di, '', 1)

        if f:
            break
        f = True


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


if __name__ == '__main__':
    main()

Solution (Python):


import sys
read = sys.stdin.read
readline = sys.stdin.readline



"""
a = "dream"[::-1]
b = "dreamer"[::-1]
c = "erase"[::-1]
d = "eraser"[::-1]
"""
s = [*input()]
a = [*"dream"]
b = [*"dreamer"]
c = [*"erase"]
d = [*"eraser"]

while True:
    if not s:
        print("YES")
        break
    elif s[-5:] == a:
        for _ in range(5): s.pop()
    elif s[-5:] == c:
        for _ in range(5): s.pop()
    elif s[-7:] == b:
        for _ in range(7): s.pop()
    elif s[-6:] == d:
        for _ in range(6): s.pop()
    else:
        print("NO")
        break

Solution (Python):
s = input()
x = 0
a = s.count("dream")
b = s.count("dreamer")
c = s.count("erase")
d = s.count("eraser")
reigai1 = s.count("dreamerase")
reigai2 = s.count("dreameraser")
reigai1 = reigai1-reigai2
b = b-reigai1-reigai2
a = a-reigai1-reigai2
c = c-reigai1-reigai2
d = d-reigai2
a2 = a-b
c2 = c-d




if a2*5+b*7+c2*5+d*6+reigai1*10+reigai2*11 == len(s):
    print("YES")
else:
    print("NO")

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

S = S.replace('dream', '
S = S.replace('erase', '.')
S = S.replace('
S = S.replace('.r', '')
S = S.replace('
S = S.replace('.', '')

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

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

Solution (Python):
ar = list(input())
l = len(ar)
if l < 5:
    print('NO')
else:
    while True:
        if l == 0:
            print("YES")
            exit()
        if "".join(ar[l-5:l]) == 'dream' or "".join(ar[l-5:l]) == 'erase':
            l -= 5
        elif "".join(ar[l-6:l]) == 'eraser':
            l -= 6
        elif "".join(ar[l-7:l]) == 'dreamer':
            l -= 7
        else:
            print("NO")
            exit()

Solution (Python):
s = input()
s = s[::-1]
hoge = ['maerd', 'remaerd', 'esare', 'resare']
i = 0
j = 0
while i < len(s):
    ans = ''
    while j < len(s):
        ans += s[j]
        if len(ans) > 7:
            print('NO')
            quit()
        if ans in hoge:
            break
        else:
            j += 1
    j += 1
    i = j
print('YES')

Solution (Python):
s = input()
s = s[::-1]
lis = ['remaerd', 'resare', 'esare', 'maerd']
i = 0
j = 0

while i < len(s):
    tmp = ''
    while j < len(s):
        tmp += s[j]
        
        if len(tmp) == 8:
            print('NO')
            quit()
        elif tmp in lis:
            i = j + 1
            j = i
            break
        else:
            j += 1
print('YES')

Solution (Python):
s = input()
n = len(s)
idx = 0
while idx < n:
    if idx+5 > n:
        break
    if s[idx:idx+5] == 'dream':
        idx += 5
        if idx+2 <= n and s[idx:idx+2] == 'er' and (idx+2 == n or s[idx+2:idx+3] != 'a'):
            idx += 2
    elif s[idx:idx+5] == 'erase':
        idx += 5
        if idx+1 <= n and s[idx:idx+1] == 'r':
            idx += 1
    else:
        break

if idx == n:
    print('YES')
else:
    print('NO')

Solution (Python):
S = input()
T = ""
SS = S[::-1]
d = "maerd"
dd = "remaerd"
e = "esare"
ee = "resare"

cnt1 = 0
cnt2 = 5
for i in range(4, len(S)):
    if(SS[cnt1:cnt2] == dd):
        T += dd
        cnt1 = cnt2
        cnt2 +=1
    elif(SS[cnt1:cnt2] == ee):
        T += ee
        cnt1 = cnt2
        cnt2 += 1
    elif(SS[cnt1:cnt2] == d):
        T += d
        cnt1 = cnt2
        cnt2 += 1
    elif(SS[cnt1:cnt2] == e):
        T += e
        cnt1 = cnt2
        cnt2 += 1
    else:
        cnt2 += 1
    
print("YES" if S == T[::-1] else "NO")

Solution (Python):

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

l = ['maerd','remaerd','esare','resare']
i = 0
while i < len(S):
    for w in l:
        if w == S[i:i+len(w)]:
            i += len(w)
            break
    else:
        print('NO')
        exit()
print('YES')

Solution (Python):
s=input()

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: break


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

Solution (Python):
S=input()
T=""
l=["maerd","remaerd","esare","resare"]
flag=True
for i in range(len(S)):
    T=T+S[len(S)-1-i]
    if T in l:
        T=""
    if len(T)>7:
        flag=False
        break
if flag:
    print("YES")
else:
    print("NO")

Solution (Python):
S = input()
S = S[::-1]
key = [
    "dream"[::-1],
    "erase"[::-1],
    "dreamer"[::-1],
    "eraser"[::-1]
]
f = False
while(S != ""):
    f = False
    for i in range(4):
        tmp = S[0:len(key[i])]
        if (tmp == key[i]):
            S = S[len(key[i]):]
            f = True
            break
    if not(f):
        break
        
if (f):
    print("YES")
else:
    print("NO")

Solution (Python):
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) 
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) 
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]





s=list(ss())
lens=len(s)
flag=True

s.reverse()

word = ("maerd","remaerd","esare","resare")

cnt=0

while cnt <lens:
    cnt_chk=cnt
    if "".join(s[cnt:cnt+5])==word[0]:
        cnt_chk+=5
    elif "".join(s[cnt:cnt+7])==word[1]:
        cnt_chk+=7
    elif "".join(s[cnt:cnt+5])==word[2]:
        cnt_chk+=5
    elif "".join(s[cnt:cnt+6])==word[3]:
        cnt_chk+=6
    if cnt==cnt_chk:
        print("NO")
        exit()
    else:
        cnt=cnt_chk
print("YES")

Solution (Python):
import sys
sys.setrecursionlimit(4100000)
import math
import fractions



'''
1行のint
N, K = map(int, input().split())

1行のstring
S, T = input().split()

1行の整数配列
P = list(map(int,input().split()))

複数行2数値
x = []
y = []
for i in range(5):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)

'''


S = input()


check = len(S)-1

while(check >= 0):

    if S[check] == "r":
        if S[check-2] == "m":
            remove = S[check-6: check+1]
            if remove == "dreamer":
                check -= 7
            else:
                print("NO")
                sys.exit()


        elif S[check-2] == "s":
            remove = S[check-5: check+1]
            
            if remove == "eraser":
                check -= 6
            else:
                print("NO")
                sys.exit()

        else:
            print("NO")
            sys.exit()

    elif S[check] == "e":
        remove = S[check-4: check+1]
            
        if remove == "erase":
            check -= 5
        else:
            print("NO")
            sys.exit()

    elif S[check] == "m":
        remove = S[check-4: check+1]
            
        if remove == "dream":
            check -= 5
        else:
            print("NO")
            sys.exit()

    else:
        print("NO")
        sys.exit()


print("YES")

Solution (Python):

import sys
import math
import os
import itertools
import _collections
import string
from functools import lru_cache
import heapq

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def lcm(a,b):
        return (a * b) // Math.gcd(a,b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3,d + 1,2):
            if n % i == 0:
                return False
        return True


MOD = int(1e09) + 7

def main():
    S = cin.string()
    while len(S) != 0:
        if S[-5:] == "dream":
            S = S[:-5]
        elif S[-7:] == "dreamer":
            S = S[:-7]
        elif S[-5:] == "erase":
            S = S[:-5]
        elif S[-6:] == "eraser":
            S = S[:-6]
        else:
            print("NO")
            return
    print("YES")
    return

if __name__ == "__main__":
    main()

Solution (Python):
S = input()

L = ["eraser","erase","dreamer","dream"]
 
while len(S) > 0:
  for i in range(len(L)):
    if L[i] in S:
      S = S.replace(L[i],"")
  else:break
 
if len(S) == 0:
  print("YES")
else:print("NO")

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

i = len(S)-1
while i >= 4:
    if S[i-4:i+1] == ['d', 'r', 'e', 'a', 'm'] or S[i-4:i+1] == ['e', 'r', 'a', 's', 'e']:
        i -= 5
    elif i-5 >= 0 and S[i-5:i+1] == ["e", "r", "a", "s", "e", "r"]:
        i -= 6
    elif i-6 >= 0 and S[i-6:i+1] == ["d", "r", "e", "a", "m", "e", "r"]:
        i -= 7
    else:
        break
        
if i == -1:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys
input = sys.stdin.readline
S = input()[: -1]
dp = [0] * (len(S) + 1)
dp[0] = 1
for i in range(len(S)):
  if i + 5 <= len(S):
    if S[i: i + 5] == "dream": dp[i + 5] += dp[i]
    if S[i: i + 5] == "erase": dp[i + 5] += dp[i]
  if i + 7 <= len(S):
    if S[i: i + 7] == "dreamer": dp[i + 7] += dp[i]
  if i + 6 <= len(S):
    if S[i: i + 6] == "eraser": dp[i + 6] += dp[i]
if dp[-1]: print("YES")
else: print("NO")

Solution (Python):
s=input()
a=""
for i in s[::-1]:
    a=i+a
    if a in ["dream","dreamer","erase","eraser"]:
        a=""
print("NO" if len(a) else "YES")

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

ws = ['maerd','remaerd','esare','resare']
i = 0
while i < len(S):
    for w in ws:
        if w == S[i:i+len(w)]:
            i += len(w)
            break
    else:
        print('NO')
        exit()
print('YES')

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

rS = str(S[:: -1])
rT = [str(t[:: -1]) for t in T]

now = 0
while now < len(S):
    for t in rT:
        if t == rS[now: now + len(t)]:
            now += len(t)
            break
    else:
        print('NO')
        exit()

print('YES')

Solution (Python):
S = input()
S = "".join(list(reversed(S)))
str_list = ["dream", "dreamer", "erase", "eraser"]
rev_str = []
for i in str_list:
    rev_str.append("".join(list(reversed(i))))
is_OK = True
while len(S) > 0:
    if S[0:5] in rev_str:
        S = S[5:]
    elif S[0:6] in rev_str:
        S = S[6:]
    elif S[0:7] in rev_str:
        S = S[7:]
    else:
        is_OK = False
        break
if is_OK:
    print("YES")
else:
    print("NO")

Solution (Python):
def conform(string, i, Tmember):
    for t in Tmember:
        if s[i] != t:
            print("NO")
            exit()
        else:
            i += 1
    return i


S = str(input())
T = ["maerd", "remaerd", "esare", "resare"]
s = ""
for i in range(1, len(S)+1):
    s += S[-i]
count = 0
while(count < len(s)):
    flag = 4
    if s[count] == "m":
        flag = 0
    elif (s[count] == "r")and(s[count+1] == "e")and(s[count+2] == "m"):
        flag = 1
    elif s[count] == "e":
        flag = 2
    elif (s[count] == "r")and(s[count+1] == "e")and(s[count+2] == "s"):
        flag = 3
    else:
        print("NO")
        exit()
    count = conform(s, count, T[flag])
print("YES")

Solution (Python):
s = input()
while s:
    m = [t for t in ['dream', 'dreamer', 'erase', 'eraser'] if s.endswith(t)]
    if not m:
        print('NO')
        exit()
    s = s[:-len(m[0])]
print('YES')

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

def main():
    s = input().strip()[::-1]

    while True:
        ok = False
        if len(s) >= 7:
            if s[:7] == "remaerd":
                s = s[7:]
                ok = True
        if len(s) >= 6:
            if s[:6] == "resare":
                s = s[6:]
                ok = True
        if len(s) >= 5:
            if s[:5] == "esare" or s[:5] == "maerd":
                s = s[5:]
                ok = True
        if not ok:
            break
    
    
    if len(s) > 0:
        print("NO")
    else:
        print("YES")

if __name__ == "__main__":
    main()

Solution (Python):
S = input()
for i in range(100000):
  if S[len(S)-5:]=="dream" or S[len(S)-5:]=="erase":
    S = S[:len(S)-5]
  elif S[len(S)-7:]=="dreamer":
    S = S[:len(S)-7]
  elif S[len(S)-6:]=="eraser":
    S = S[:len(S)-6]
  elif S == "":
    print("YES")
    break
  else:
    print("NO")
    break

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

Solution (Python):
reverse_s = input().strip()[::-1]

while True:
    if len(reverse_s) == 0:
        print("YES")
        exit()
    if reverse_s.startswith('maerd'):
        reverse_s = reverse_s[len('maerd'):]
    elif reverse_s.startswith('remaerd'):
        reverse_s = reverse_s[len('remaerd'):]
    elif reverse_s.startswith('esare'):
        reverse_s = reverse_s[len('esare'):]
    elif reverse_s.startswith('resare'):
        reverse_s = reverse_s[len('resare'):]
    else:
        print("NO")
        exit()

Solution (Python):
S = input()[::-1]
n = len(S)
count = 0
flag = True

while count<n:
    if S[count:count+5] == "dream"[::-1]:
        count += 5
    elif S[count:count+7] == "dreamer"[::-1]:
        count += 7
    elif S[count:count+5] == "erase"[::-1]:
        count += 5
    elif S[count:count+6] == "eraser"[::-1]:
        count += 6
    else:
        flag = False
        break

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

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

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

ans = 'YES'
l = len(S)
while l>=5:
    if S[l-5:l] == list(A[0]):
        l -= 5
    elif S[l-5:l] == list(A[2]):
        l -= 5
    elif l == 5:
        break
    elif S[l-6:l] == list(A[3]):
        l -= 6
    elif l == 6:
        break
    elif S[l-7:l] == list(A[1]):
        l -= 7
    else:
        ans = 'NO'
        break
if l != 0:
    ans = 'NO'
print(ans)

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

Solution (Python):
S = input()
last_ind = len(S)
yes = True
for i in range(len(S) - 1, -1, -1):
    if S[i:last_ind] == "dream" or S[i:last_ind] == "erase" or S[i:last_ind] == "eraser" or S[i:last_ind] == "dreamer":
        last_ind = i
        continue
    if last_ind - i > 7 or i == 0:
        yes = False
        break
        
if yes:
    print("YES")
else:
    print("NO")

Solution (Python):
S = input()
t = len(S)
L = [("dream", 5), ("dreamer", 7), ("erase", 5), ("eraser", 6)]
while t > 0:
    for w, l in L:
        if S.rfind(w, 0, t) == t - l:
            t -= l
            break
    else:
        print("NO")
        break
else:
    print("YES")

Solution (Python):


import copy
import sys


hoge = input()
n = len(hoge)

while(1):
	n = len(hoge)
	if(n==0):
		print("YES")
		sys.exit()
	if(n<=4):
		print("NO")
		sys.exit()
	else:
		if(hoge[-5:]=="dream"):
			tmp=copy.copy(hoge[0:-5])
			hoge=tmp
		elif(hoge[-5:]=="erase"):
			tmp=copy.copy(hoge[0:-5])
			hoge=tmp
		elif(n<=5):
			print("NO")
			sys.exit()
		else:
			if(hoge[-6:]=="eraser"):
				tmp=copy.copy(hoge[0:-6])
				hoge=tmp
			elif(n<=6):
				print("NO")
				sys.exit()
			else:
				if(hoge[-7:]=="dreamer"):
					tmp=copy.copy(hoge[0:-7])
					hoge=tmp
				else:
					print("NO")
					sys.exit()

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

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

numlist=[0,0,0,0,0,0]
for i in range(len(a)):
    numlist[i]=S.count(a[i])
numlist[0]-=numlist[1]
numlist[2]-=numlist[3]
numlist[4]-=numlist[5]
numlist[1]-=(numlist[4]+numlist[5])   
numlist[0]+=(numlist[4]+numlist[5])   
total=numlist[0]*5+numlist[1]*7+numlist[2]*5+numlist[3]*6
if total==len(S):
    print("YES")
else:
    print("NO")

Solution (Python):
S = input()

C = S[::-1]
while C:
    if C[:5] == "maerd":
        C = C[5:]
    elif C[:7] == "remaerd":
        C = C[7:]
    elif C[:5] == "esare":
        C = C[5:]
    elif C[:6] == "resare":
        C = C[6:]
    else:
        print("NO")
        exit()
print("YES")

Solution (Python):
s = list(input())
s.reverse()
s = ''.join(map(str, s))
count = 0
word = ['maerd', 'remaerd', 'resare', 'esare']

while count <= len(s)-8:
  counttemp = count
  for i in range(4):
    w = word[i]
    if s[count:count+len(w)] == w:
      count += len(w)
  if counttemp == count:
    break

if s[count:] in word or s[count:] =='':
  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):
def solve(S):
    N = len(S)
    i = 0
    while i < N:
        if N - i < 5:
            return False
        if S[i: i + 5] == "dream":
            if i + 7 <= N and S[i + 5: i + 7] == "er":
                if i + 7 == N or S[i + 7] != "a":
                    i += 7
                else:
                    i += 5
            else:
                i += 5
        elif S[i: i + 5] == "erase":
            if i + 5 < N and S[i + 5] == "r":
                i += 6
            else:
                i += 5
        else:
            return False
    return True

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

Solution (Python):

import sys

S = input()
index = len(S)
while index > 7:
    five = S[index-5:index]
    six = S[index-6:index]
    seven = S[index-7:index]
    if five == "dream" or five == "erase":
        index -= 5
    elif six == "eraser":
        index -= 6
    elif seven == "dreamer":
        index -= 7
    else:
        print("NO")
        sys.exit()

last = S[:index]
if last == "dream" or last == "erase" or last == "eraser" or last == "dreamer":
    print("YES")
else:
    print("NO")

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

Solution (Python):
import re

S = input()
flag = True
while(flag):
    if(len(S)==0):
        flag = False
    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(flag):
    print('NO')
else:
    print('YES')

Solution (Python):

import sys
INF = float("inf")

sys.setrecursionlimit(10**5)


def yes():
    print("YES")  


def no():
    print("NO")  


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


def dfs(s, i):
    if i == len(s):
        return True
    for t in template:
        if s[i:].startswith(t):
            if dfs(s, i+len(t)):
                return True
    return False


def solve(S: str):
    if dfs(S, 0):
        yes()
    else:
        no()

    return


def main():

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


if __name__ == '__main__':
    main()

Solution (Python):
S = input()

word_lst = ['dream', 'dreamer', 'erase', 'eraser']
for i in range(len(word_lst)):
  word_lst[i] = word_lst[i][::-1]
  
S = S[::-1]
ans = ''
while len(ans) < len(S) - 4:
  if S[len(ans)] == 'm':
    ans = ans + word_lst[0]
  elif S[len(ans):len(ans)+3] == 'rem':
    ans = ans + word_lst[1]
  elif S[len(ans)] == 'e':
    ans = ans + word_lst[2]
  elif S[len(ans):len(ans)+3] == 'res':
    ans = ans + word_lst[3]
  else:
    break

if ans == 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:
        print('NO')
        break
else:
    print('YES')

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

Solution (Python):
import sys
S = input()
num = len(S)
S = S + "zzzzzzzzzzzzzzzzzzzzzz"
cur = 0
while cur < num:
    if S[cur] == 'd':
        if S[cur:cur+11] == 'dreameraser':
            cur += 11
        elif S[cur:cur+10] == 'dreamerase':
            cur += 10
        elif S[cur:cur+7] == 'dreamer':
            cur += 7
        elif S[cur:cur+5] == 'dream':
            cur += 5
        else:
            print('NO')
            sys.exit()
    elif S[cur] == 'e':
        if S[cur:cur+6] == 'eraser':
            cur += 6
        elif S[cur:cur+5] == 'erase':
            cur += 5
        else:
            print('NO')
            sys.exit()
    else:
        print('NO')
        sys.exit()
    if cur == num:
        print('YES')
        sys.exit()

Solution (Python):
s=raw_input().replace("dream","X").replace("erase","Y").replace("Xer","Z").replace("Yr","T")
for i in s: 
	if i not in "XYZT": 
		print "NO"
		exit(0)
print "YES"

Solution (Python):
s = raw_input()


s = s.replace("dream", "X").replace("erase", "Y")

s = s.replace("Xer", "Z").replace("Yr", "T")

for i in s:
	if i not in "XYZT":
		print "NO"
		exit(0)
print "YES"

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

Solution (Python):
S=input()
while True:
  if S=='':
    print('YES')
    break
  else:
    a = S[-5:] in ('dream', 'erase')
    b = S[-6:] in ('eraser')
    c = S[-7:] in ('dreamer')
    if not (a or b or c):
      print('NO')
      break
    elif a:
      S = S[:-5]
    elif b:
      S = S[:-6]
    elif c:
      S = S[:-7]

Solution (Python):
S = input()
S = S[::-1]
snipetts = [s[::-1] for s in ['dream', 'dreamer', 'erase', 'eraser']]

found = True
index = 0
while found:
    found = False
    for s in snipetts:
        if S[index:index+len(s)] == s:
            index += len(s)
            found = True
            break

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

Solution (Python):
S = input()
S = S[::-1]
 
S = S.replace('resare', '')
S = S.replace('esare', '')
S = S.replace('remaerd', '')
S = S.replace('maerd', '')
 
if S:
    print('NO')
else:
    print('YES')

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

while True:
    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:
        break

if len(S) == 0:
    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):
l = ['maerd', 'remaerd', 'esare', 'resare']
flag = False
S = input()
S = ''.join(list(reversed(S)))
j = 0
for i in range(len(S)+1):
    if S[j:i] in l:
        j=i
        flag = True
    else:
        flag = False
print("YES" if flag else "NO")

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

while True:
    if i==0:
        print('YES')
        break
    if 6<i and s[i-7:i] == 'dreamer':
        i-=7
        continue
    if 5<i and s[i-6:i] == 'eraser':
        i-=6
        continue
    if 4<i and s[i-5:i] in ['dream','erase']:
        i-=5
        continue
    print('NO')
    break

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):
from collections import deque

def solve(s):
    stack = deque()
    stack.append(s)
    while len(stack) > 0:
        top = stack.pop()
        if top == '':
            return 'YES'

        if top[:5] == 'dream':
            if top[5:7] == 'er':
                stack.append(top[7:])
            stack.append(top[5:])
        elif top[:5] == 'erase':
            if len(top) > 5 and top[5] == 'r':
                stack.append(top[6:])
            else:
                stack.append(top[5:])
    return 'NO'

print(solve(input()))

Solution (Python):
S = input()
lst = ["dreamer", "dream", "eraser", "erase"]
lst = [ "".join(list(reversed(k))) for k in lst]
S = "".join(list(reversed(S)))

i  = 0
while i < len(S):
    found = False
    for k in lst:
        try:
            if S[i:].index(k) == 0:
                found = True
                i += len(k)
                break
        except:
            continue

    if not found:
        print("NO")
        exit(0)
print("YES")

Solution (Python):
S = input()

a = len(S)

while a != 0:
    if a - 5 < 0:
        print("NO")
        exit()
    elif S[a - 1] == "m" and S[a - 5:a] == "dream":
        a -= 5
    elif S[a - 1] == "e" and S[a - 5:a] == "erase":
        a -= 5
    elif S[a - 3:a] == "mer" and a - 7 >= 0 and S[a - 7:a] == "dreamer":
        a -= 7
    elif S[a - 3:a] == "ser" and a - 6 >= 0 and S[a - 6:a] == "eraser":
        a -= 6
    else:
        print("NO")
        exit()
print("YES")