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()
ls = len(S)
if ls<5:
    print('NO')
else:
    i = 0
    while True:
        if (ls-1>=i+4):
            if S[i:i+5]=='dream':
                if S[i:] == 'dream' or S[i:] == 'dreamer':
                    print('YES')
                    break
                elif ls-1>=i+7:
                    if (S[i+5:i+8]=='dre') or (S[i+5:i+8]=='era'):
                        i+=5
                        continue
                    elif (S[i+5:i+8]=='erd') or (S[i+5:i+8]=='ere'):
                        i+=7
                        continue

            elif S[i:i+5]=='erase':
                if S[i:] == 'erase' or S[i:] == 'eraser':
                    print('YES')
                    break
                elif ls-1>=i+5:
                    if S[i+5]=='r':
                        i += 6
                        continue
                    elif S[i+5]=='d' or S[i+5]=='e':
                        i += 5
                        continue
        print('NO')
        break

Solution (Python):



s = input()[::-1]
collector = ''
t = ''
words = ['remaerd', 'maerd', 'resare', 'esare']

for c in s:
    collector += c
    if collector in words:
        t += collector
        collector = ''

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

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

Solution (Python):
def solve(s):
  while True:
    if s == '':
      return True
    else:
      if s[0:7] == 'dreamer'[::-1]:
        s = s[7:]
      elif s[0:5] == 'dream'[::-1]:
        s = s[5:]
      elif s[0:5] == 'erase'[::-1]:
        s = s[5:]
      elif s[0:6] == 'eraser'[::-1]:
        s = s[6:]
      else:
        return False
  
print('YES' if solve(input()[::-1]) else 'NO')

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

Solution (Python):
import re

S=input()[::-1]

if re.match(r'^(maerd|remaerd|esare|resare)+$',S):
    print('YES')
else:
    print('NO')

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

Solution (Python):
rvrsd_S = input()[::-1]
words = ['dream', 'dreamer', 'erase', 'eraser']
rvrsd_words = [i[::-1] for i in words]
a = 0
sw = True
while sw == True:
    sw = False
    for i in rvrsd_words:
        if rvrsd_S[a:a+len(i)] == i:
            a += len(i)
            sw = True
    if a >= len(rvrsd_S):
        print('YES')
        exit()
print('NO')

Solution (Python):
S = input()

S = S.replace('eraser', '!').replace('erase', '!').replace('dreamer', '!').replace('dream', '!')
S = S.replace('!', '')

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

Solution (Python):
S = input()

S = S.replace('eraser', '').replace('erase', '').replace('dreamer', '').replace('dream', '')

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

Solution (Python):
S = input()


A = list(S)
A =  list(reversed(A))


while len(A)!=0:
    if len(A)>= 5 and A[0]+A[1]+A[2]+A[3]+A[4] == "esare":
        del A[:5]
    elif len(A)>= 5 and A[0]+A[1]+A[2]+A[3]+A[4] == "maerd":
        del A[:5]
    elif len(A)>= 6 and A[0]+A[1]+A[2]+A[3]+A[4]+A[5] =="resare":
        del A[:6]
    elif len(A)>= 7 and A[0]+A[1]+A[2]+A[3]+A[4]+A[5]+A[6] =="remaerd":
        del A[:7]
    else:
        print('NO')
        break;
if len(A) ==0:
    print('YES')

Solution (Python):
import re
s = input()
print('YES' if re.match('^(eraser?|dream(er)?)*$', s) else 'NO')

Solution (Python):
l=['dream','dreamer','erase','eraser']
s=input().rstrip()
x=list(s)
if len(x)<5:
    print("NO")
elif len(x)==5:
    if ''.join(x)=='dream' or ''.join(x)=='erase':
        print("YES")
    else:
        print("NO")
elif len(x)==6:
    if ''.join(x)==l[3]:
        print("YES")
    else:
        print("NO")
elif len(x)==7:
    if ''.join(x)==l[1]:
        print("YES")
    else:
        print("NO")
else:
    i=0;
    G=0;
    while(i<len(x)):
        
        if x[i]=='d':
            t=x[i:i+5]
            p=x[i:i+7]
            if ''.join(p)=='dreamer':
                if (i+7)<len(x) and x[i+7]!='a':
                    i+=7;
                elif (i+7+1)>len(x):
                    break;
                elif (i+7)<len(x) and x[i+7]=='a':
                    i+=5;
            elif ''.join(t)=='dream':
                i+=5;
            else:
                G=1;
                break;
        elif x[i]=='e':
            t=x[i:i+5]
            p=x[i:i+6]
            if ''.join(p)==l[3]:
                i+=6;
            elif ''.join(t)==l[2]:
                i+=5;
            else:
                G=1;
                break;
        else:
            G=1;
            break;
    if G==0:
        print("YES")
    else:
        print("NO")

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

Solution (Python):
import numpy as np
import sys
sys.setrecursionlimit(10 **9)


S = str(input())
dp = [0] * (2 * len(S))


def check(i): 
    flag0 = False
    flag1 = False
    flag2 = False
    flag3 = False
    flag4 = False
    flag5 = False
    if dp[i+5] != 0 or dp[i+6] != 0 or dp[i+7] != 0:
        return dp[i+5] or dp[i+6] or dp[i+7]
    
    if S[i:] == 'dream' or S[i:] == 'dreamer' or S[i:] == 'erase' or S[i:] == 'eraser':
        return True
    if S[i:i+7] == 'dreamer': 
        dp[i+7] = check(i+7)
        flag2 = dp[i+7]
    if S[i:i+5] == 'dream':
        dp[i+5] =  check(i+5)
        flag3 =  dp[i+5]
    if S[i:i+6] == 'eraser':
        dp[i+6] =  check(i+6)
        flag4 =  dp[i+6]
    if S[i:i+5] == 'erase':
        dp[i+5] =  check(i+5)
        flag5 = dp[i+5]
    return flag0 or flag1 or flag2 or flag3 or flag4 or flag5 

if check(0):
    print ('YES')
else:
    print ('NO')

Solution (Python):
import numpy as np
import sys
sys.setrecursionlimit(10 **9)
S = str(input())
A = ['dream', 'dreamer', 'erase', 'eraseer']

N = len(S)
i = 0

def check(i): 
    flag0 = False
    flag1 = False
    flag2 = False
    flag3 = False
    flag4 = False
    flag5 = False
    if S[i:] == 'dream' or S[i:] == 'dreamer' or S[i:] == 'erase' or S[i:] == 'eraser':
        flag1 = True
    if S[i:i+7] == 'dreamer':
        flag2 = check(i+7)
    if S[i:i+5] == 'dream':
        flag3 =  check(i+5)
    if S[i:i+6] == 'eraser':
        flag4 =  check(i+6)
    if S[i:i+5] == 'erase':
        flag5 = check(i+5)
    return flag0 or flag1 or flag2 or flag3 or flag4 or flag5

if check(0):
    print ('YES')
else:
    print ('NO')

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

flag = True
while flag and s != "":
    flag = False
    for i in range(len(w)):       
        if s[-1*len(w[i]):] == w[i]:
            s = s[:-1*len(w[i])]
            flag = True

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

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

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

Solution (Python):
def Hantei(S):
    res = False
    dream = list("dream")
    dreamer = list("dreamer")
    erase = list("erase")
    eraser = list("eraser")

    flg = 0
    if S[-1] == dream[-1]:
        flg = 1
    elif S[-1] == dreamer[-1] and S[-2] == dreamer[-2] and S[-3] == dreamer[-3]:
        flg = 2
    elif S[-1] == erase[-1]:
        flg = 3
    elif S[-1] == eraser[-1] and S[-2] == eraser[-2] and S[-3] == eraser[-3]:
        flg = 4

    if flg == 1:
        S.pop()
        if S.pop()==dream[-2] and S.pop()==dream[-3] and S.pop()==dream[-4] and S.pop()==dream[-5]:
            res = True
    if flg == 2:
        S.pop()
        S.pop()
        S.pop()
        if S.pop()==dreamer[-4] and S.pop()==dreamer[-5] and S.pop()==dreamer[-6] and S.pop()==dreamer[-7]:
            res = True
    if flg == 3:
        S.pop()
        if S.pop()==erase[-2] and S.pop()==erase[-3] and S.pop()==erase[-4] and S.pop()==erase[-5]:
            res = True
    if flg == 4:
        S.pop()
        S.pop()
        S.pop()
        if S.pop()==eraser[-4] and S.pop()==eraser[-5] and S.pop()==eraser[-6]:
            res = True
            
    return res

S = list(input())
while True:
    if Hantei(S) == False:
        print("NO")
        break
    if len(S) == 0:
        print("YES")
        break

Solution (Python):
s = input()
while True:
    
    
    
    if len(s) >= 6 and s[-6:] == 'eraser':
        s = s[:-6]
    elif len(s) >= 5 and s[-5:] == 'erase':
        s = s[:-5]
    elif len(s) >= 7 and s[-7:] == 'dreamer':
        s = s[:-7]
    elif len(s) >= 5 and s[-5:] == 'dream':
        s = s[:-5]
    elif len(s) > 0:
        
        print('NO')
        import sys
        sys.exit()
    else:break
    
    
    
    
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):
replace_str = ["eraser","erase","dreamer","dream"]
s =  input()

for str in replace_str :
    s = s.replace(str,"")

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

Solution (Python):
S = input()
S = S[::-1]
t = ['maerd','remaerd','esare','resare']
i = 0
frag = 0
while i <= len(S)-1 and frag == 0:
  k = 0
  for j in range(4):
    d = t[j]
    if S[i:len(d)+i] == d:
      i = i + len(d)
    else:
      k = k + 1
      if k == 4:
        frag = 1
        print('NO')
if i == len(S):
  print('YES')

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
  break

answer = 'YES' if not S else 'NO'
print(answer)

Solution (Python):
S=list(input())
S.reverse()
i=0
l=len(S)
while True:
    if i==l:
        print("YES")
        break
    elif ''.join(S[i:i+5])=="esare":
        i=i+5
    elif ''.join(S[i:i+5])=="maerd":
        i=i+5
    elif ''.join(S[i:i+6])=="resare":
        i=i+6
    elif  ''.join(S[i:i+7])=="remaerd":
        i=i+7
    else:
        print("NO")
        break

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

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()[::-1]
de=['maerd','esare']
dr='remaerd'
er='resare'
def check(a,x):
	if a > x:
		print("NO")
		exit(0)
i=0
n=len(s)
while i<n:
	check(i,n-5)
	if s[i:i+5] in de:
		i+=5
		continue
	check(i,n-6)
	if s[i:i+6] == er:
		i+=6
		continue
	check(i,n-7)
	if s[i:i+7]  == dr:
		i+=7
		continue
	print('NO')
	break
else:
	print("YES")

Solution (Python):
s = input().rstrip('\n');
can = 0;
n = len(s);
tsugi = n;
while tsugi >= 1 :
	if s[tsugi - 5 : tsugi] == 'dream' :
		can = 1;
		tsugi = tsugi -5;
	elif s[tsugi - 5 : tsugi] == 'erase' :
		can = 1;
		tsugi = tsugi -5;
	elif s[tsugi - 6 : tsugi] == 'eraser' :
		can = 1;
		tsugi = tsugi -6;
	elif s[tsugi - 7 : tsugi] == 'dreamer' :
		can = 1;
		tsugi = tsugi -7;
	else :
		can = 0;
		tsugi = -1;
if bool(can) :
	print('YES');
else :
	print('NO');

Solution (Python):

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

check=0
i=0
a=[]
while check==0:
    if i==len_s: 
        a.append(1)
        break
    if len_s-i <5: 
        check=1
        a.append(2)
        break
    if S[i:i+5] == divide[0]:
        i=i+5
        a.append(3)
        continue
    if S[i:i+5] == divide[2]:
        i=i+5
        a.append(4)
        continue
    if len_s-i <6: 
        check=1
        a.append(5)
        break
    if S[i:i+6] == divide[3]:
        i=i+6
        a.append(6)
        continue
    if len_s-i <7: 
        check=1
        a.append(7)
        break
    if S[i:i+7] == divide[1]:
        i=i+7
        a.append(8)
        continue
    check=1 

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

Solution (Python):
def resolve():
    s = input()

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

    s = s[::-1]
    i = 0

    while i < len(s):

        flag = False

        for devider in deviders:

            if s[i:i + len(devider)] == devider:

                i += len(devider)
                flag = True
                break

        if not flag:
            print('NO')
            return

    print('YES')

    return
  
resolve()

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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  s=list(S())

  l=[['d', 'r', 'e', 'a', 'm'],
  ['d', 'r', 'e', 'a', 'm', 'e', 'r'],
  ['e', 'r', 'a', 's', 'e'],
  ['e', 'r', 'a', 's', 'e', 'r']]

  while True:
    if len(s)<=4:
      break
    else:
      if l[0]==s[-5:]:
        [s.pop() for _ in ' '*5]
      elif l[1]==s[-7:]:
        [s.pop() for _ in ' '*7]
      elif l[2]==s[-5:]:
        [s.pop() for _ in ' '*5]
      elif l[3]==s[-6:]:
        [s.pop() for _ in ' '*6]
      else:
        return 'NO'

  if len(s)==0:
    return 'YES'
  return 'NO'

print(main())

Solution (Python):
s = input().replace('eraser', '').replace('erase', '').replace('dreamer', '').replace('dream', '')

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

Solution (Python):


def solve():
    S = input()
    T = ""
    while True:
        if S == "":
            break
        
        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")

if __name__ == "__main__":
    solve()

Solution (Python):



s = input()
while True:
    if len(s) == 0:
        print("YES")
        exit()
    elif len(s) <= 4:
        print("NO")
        exit()
    else:
        
        if s[-7:] == "dreamer":
            s = s[:-7]
        elif s[-6:] == "eraser":
            s = s[:-6]
        elif s[-5:] == "dream" or s[-5:] == "erase":
            s = s[:-5]
        else:
            print("NO")
            break

Solution (Python):
S = input()

flag = True
while len(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:
        flag = False
        break

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

Solution (Python):
S = input()
word = ['dream', 'dreamer', 'erase', 'eraser'][::-1]
 
for w in word:
    S = S.replace(w, '')
 
if S == '':
    print('YES')
else:
    print('NO')

Solution (Python):
S=input()
i=0
while i<=len(S):
    if i==len(S):
        print("YES")
        exit()
    if S[i:i+11] == "dreameraser":
        i+=11
    elif S[i:i+10] == "dreamerase":
        i+=10
    elif S[i:i+7] == "dreamer":
        i+=7
    elif S[i:i+6] == "eraser":
        i+=6
    elif S[i:i+5] in ["dream","erase"]:
        i+=5
    else:
        print("NO")
        exit()

Solution (Python):
S=input()
i=0
while i<=len(S):
    if i==len(S):
        print("YES")
        exit()

    if S[i:i+11] == "dreameraser":
        i+=11
        continue
    elif S[i:i+10] == "dreamerase":
        i+=10
        continue
    elif S[i:i+7] == "dreamer":
        i+=7
        continue
    elif S[i:i+6] == "eraser":
        i+=6
        continue
    elif S[i:i+5] in ["dream","erase"]:
        i+=5
        continue
    else:
        print("NO")
        exit()

Solution (Python):
S=input()
i=0
while i<=len(S):
    if i==len(S):
        print("YES")
        exit()
    if S[i:i+11] == "dreameraser":
        i+=11
        continue
    elif S[i:i+10] == "dreamerase":
        i+=10
        continue
    elif S[i:i+7] == "dreamer":
        i+=7
        continue
    elif S[i:i+6] == "eraser":
        i+=6
        continue
    elif S[i:i+5] in ["dream","erase"]:
        i+=5
        continue
    else:
        print("NO")
        exit()
print("NO")

Solution (Python):
import sys
S = str(input())
while S != '':
    if (S[:5] == 'dream' and S[5:11] == 'eraser') or (S[:5] == 'dream' and S[5: 10] == 'erase') or (S[:5] == 'dream' and S[5:] == '') or (S[:5] == 'dream' and S[5:10] == 'dream'):
        S = S[5:]
    elif (S[:5] == 'erase' and S[5:] != '' and S[5] != 'r') or (S[:5] == 'erase' and S[5:] == ''):
        S = S[5:]
    elif S[:6] == 'eraser':
        S = S[6:]
    elif S[:7] == 'dreamer':
        S = S[7:]
    else:
        print('NO')
        sys.exit()
        break
        
print('YES')

Solution (Python):
import re  
import sys 


def i_input(): return int(input())
def s_input(): return map(int,input().split())
def l_input(): return list(map(int, input().split()))


def main():
    S = input()

    while 1:
        if S == '':
            print('YES')
            sys.exit()
        elif end_match('dreamer', S):
            S = S[:-7]
            
        elif end_match('eraser', S):
            S = S[:-6]
            
        elif end_match('dream', S):
            S = S[:-5]
            
        elif end_match('erase', S):
            S = S[:-5]
            
        else:
            print('NO')
            sys.exit()

def end_match(query, S):
    length = len(query)
    end = S[-length:]
    
    if end == query:
        return True
    else:
        return False

if __name__ == "__main__":
    main()

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

while s:
  if s[0:7] == b:
    s = s.replace(b,"",1)
  elif s[0:6] == d:
    s = s.replace(d,"",1)
  elif s[0:5] == a or s[0:5] == c:
    s = s.replace(s[0:5],"",1)
  else:
    print("NO")
    break

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

Solution (Python):
S = input()

S = S.replace('eraser','')
S = S.replace('erase','')
S = S.replace('dreamer','')
S = S.replace('dream','')

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

Solution (Python):
def word_break(s, words):
    ok = [True]
    word_length = max(len(word) for word in words)
    for i in range(1, len(s) + 1):
        ok += any(ok[j] and s[j:i] in words for j in range(max(0, i - word_length), i)),
    return ok[-1]


s = input()
words = {"eraser", "erase", "dreamer", "dream"}
print("YES" if word_break(s, words) else "NO")

Solution (Python):
def word_break(s, words):
    ok = [True]
    word_length = max(len(word) for word in words)
    for i in range(1, len(s)+1):
        ok += any(ok[j] and s[j:i] in words for j in range(max(0, i - word_length), i)),
    return ok[-1]
 
 
s = input()
words = ["eraser", "erase", "dreamer", "dream"]
print("YES" if word_break(s, words) else "NO")

Solution (Python):
s = input()
s = s.replace('eraser','z')
s = s.replace('erase','z')
s = s.replace('dreamer','z')
s = s.replace('dream','z')
for  S in s:
    if S != 'z':
        print('NO')
        exit()
print('YES')

Solution (Python):
if __name__ == '__main__':
    S = input()
    A = ['eraser', 'erase', 'dreamer', 'dream']
    for x in A:
        S = S.replace(x, '')
    if S == '':
        print('YES')
    else:
        print('NO')

Solution (Python):

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

Solution (Python):
s = input()[::-1]
l = ["resare","esare","remaerd","maerd"]
i = 0

while not i == len(s):
    for a in l:
        if s[i:i+len(a)] == a:
            i += len(a)
            break
    else:
        print("NO")
        exit()
print("YES")

Solution (Python):
S = input()
flag = True
while flag:
    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]
    elif len(S)==0:
        break
    else:
        flag = False
 
if flag:
    print("YES")
else:
    print("NO")

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  s=S()

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

  while True:
    a=s[-5:]
    b=s[-7:]
    c=s[-6:]

    if a==l[0]:
      s=s[:-5]
    elif b==l[1]:
      s=s[:-7]
    elif a==l[2]:
      s=s[:-5]
    elif c==l[3]:
      s=s[:-6]
    else:
      if s=='':
        return 'YES'
      else:
        return 'NO'

print(main())

Solution (Python):




def solve(s):
  s = s.replace("dream","D")
  s = s.replace("erase","E")
  s = s.replace("Der","")
  s = s.replace("Er","")
  s = s.replace("D","")
  s = s.replace("E","")
  if len(s) == 0:
    return "YES"
  else:
    return "NO"

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

Solution (Python):
S = input()
flag = True
while flag:
    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]
    elif len(S)==0:
        break
    else:
        flag = False

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

Solution (Python):

"""
C - 白昼夢 / Daydream
https://atcoder.jp/contests/abc049/tasks/arc065_a

"""
import sys


def solve(S):
    while S:
        if S.endswith('dreamer'):
            S = S[:-7]
        elif S.endswith('eraser'):
            S = S[:-6]
        elif S.endswith('dream'):
            S = S[:-5]
        elif S.endswith('erase'):
            S = S[:-5]
        else:
            return 'NO'
    return 'YES'


def main(args):
    S = input()
    ans = solve(S)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Solution (Python):
s = input()
tt = ['dream', 'dreamer', 'erase', 'eraser']
t = ''
i = len(s) - 1
while (i > 0):
    if s[i] == 'm':
        t = tt[0] + t
        i -= 5
    elif s[i] == 'e':
        t = tt[2] + t
        i -= 5
    elif s[i] == 'r':
        if s[i - 2] == 'm':
            t = tt[1] + t
            i -= 7
        elif s[i - 2] == 's':
            t = tt[3] + t
            i -= 6
        else:
            break
    else:
        break
if s == t:
    print('YES')
else:
    print('NO')

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) 

S = str(input())
S = list(S)
S.reverse()
S = "".join(S)

def check(T):
    if len(S) == 0:
        return 1
    adds = ["maerd","remaerd","esare","resare"]
    q = copy.deepcopy(adds)
    while len(q) > 0:
        t = q.pop()
        if t == S:
            return 1
        for add in adds:
            if S[len(t):len(t+add)] == add:
                q.append(t+add)
    return 0

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

Solution (Python):


a = str(input())
tmp = ''
for i in range(len(a)):
    tmp = a[len(a)-1] + tmp
    a = a[:-1]
    if tmp == 'dream' or tmp == 'dreamer' or tmp == 'erase' or tmp == 'eraser':
        tmp = ''

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

Solution (Python):
S = input()
T_list = ['dream', 'eraser', 'dreamer', 'erase']
S = S[::-1]
while len(S) > 0:
    if S[:5] == 'maerd' or S[:5] == 'esare':
        S = S[5:]
    elif S[:6] == 'resare':
        S = S[6:]
    elif S[:7] == 'remaerd':
        S = S[7:]
    else:
        break
if len(S) > 0:
    print('NO')
else:
    print('YES')

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

Solution (Python):
s = input()

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

Solution (Python):
import re

S = input()

if re.match('(dream|dreamer|erase|eraser)+$', S):
    print('YES')
else:
    print('NO')

Solution (Python):
S = input()

S = S[::-1]

word_list1 = "dream"
word_list2 = "dreamer"
word_list3 = "erase"
word_list4 = "eraser"

s = ""
ans = ""

word_list = [word_list1[::-1],word_list2[::-1],word_list3[::-1],word_list4[::-1]]

for i in S:
    s += i
    if s in word_list:
        ans += s
        s = ""

if S == ans:
    print('YES')
else:
    print('NO')

Solution (Python):
if __name__ == '__main__':
    S = str(input())
    word_list = ['dream', 'dreamer', 'erase', 'eraser']
    rev_S = S[::-1]
    word_list = list(map(lambda x : x[::-1], word_list))

    answer = 'YES'
    while len(rev_S) >= 1:
        for i in range(len(word_list)):
            word = word_list[i]
            if rev_S.startswith(word):
                rev_S = rev_S[len(word)::]
                break
            elif i == len(word_list)-1:
                answer = 'NO'
                break
        if answer == 'NO':
            break

    print(answer)

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

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

Solution (Python):
import re

s = input()[::-1]
while len(s) > 0:
    for ptn in ['dream', 'dreamer', 'erase', 'eraser']:
        if re.match(ptn[::-1], s) is not None:
            s = s[len(ptn):]
            break
    else:
        break
print('YES' if len(s) == 0 else 'NO')

Solution (Python):
S = list(input())[::-1]
L,i = len(S),0
words = [list(word) for word in ['maerd', 'esare', 'remaerd', 'resare'] ]

while i < L-7:
    if S[i:i+5] in words[:2]:  i += 5
    elif S[i:i+6] == words[3]: i += 6
    elif S[i:i+7] == words[2]: i += 7
    else: break

if S[i:] in words:  print('YES')
else:   print('NO')

Solution (Python):

from re import match









def main():
    S = input()

    if match('(dreamer|eraser|erase|dream)+$', S):
        print("YES")
    else:
        print("NO")

        
        
        
        
        

        
        
        
        
        
        
        
        
        
        
        

        
        
        


if __name__ == "__main__":
    main()

Solution (Python):

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

        if len(S) == 0:
            print("YES")
            break


if __name__ == "__main__":
    main()

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

def str_read(s):
    while True:
        if len(s) == 0:
            return True
        else:
            if s[0:5] in ['maerd', 'esare']:
                s = s[5:]
            elif s[0:6] in ['resare']:
                s = s[6:]
            elif s[0:7] in ['remaerd']:
                s = s[7:]
            else:
                return False


si = input()
if str_read(si[::-1]):
    print('YES')
else:
    print('NO')

Solution (Python):
s = input()[::-1]
keys = ["dream", "dreamer", "erase", "eraser"]
keys_ = [i[::-1] for i in keys]
i = 0
t = ''

for _ in range(len(s)):
  for key in keys_:
    if s[i:].startswith(key):
      t += key
      i += len(key)
    if t == s:
      print('YES')
      exit()
else:
  print('NO')

Solution (Python):
import re
s=input()
pat=r'^(dream(er)?|erase(r)?)*$'
a=re.match(pat,s)
print('YES' if a else 'NO')

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

Solution (Python):
S=list(input())
i=0
while i!=len(S):
    if i+6<=len(S) and S[i:i + 6] == ['e', 'r', 'a', 's', 'e', 'r']:
        i += 6
    elif i+5<=len(S) and S[i:i+5]==['e','r','a','s','e']:
        i+=5
    elif i+7<=len(S) and S[i:i+7]==['d','r','e','a','m','e','r'] and (len(S[i:])==7 or (S[i+7]!='a')):
        i+=7
    elif i+5<=len(S) and S[i:i+5]==['d','r','e','a','m']:
        i+=5
    else:
        print('NO')
        exit()
print('YES')

Solution (Python):
S = input()
while len(S) > 0:
    if S[-5:] == 'dream' or 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()


S = S.replace('eraser', '').replace('erase', '').replace('dreamer', '').replace('dream', '')

if S is '':
    print('YES')
else:
    print('NO')

Solution (Python):
s = input()
n = len(s)
def judge(n,s):
    if s[n-5:n] == "erase":
        return n-5
    elif s[n-5:n] == "dream":
        return n-5
    elif s[n-6:n] == "eraser":
        return n-6
    elif s[n-7:n] == "dreamer":
        return n-7
    else:
        return -1

while n>=5:
    n = judge(n,s)

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