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()[::-1]
flag = True
pivot = 0
while pivot<len(s):
	if s[pivot]=='m':				
		t = s[pivot:pivot+5]
		if t=='maerd':
			pivot += 5
		else:
			flag = False
			break
	elif s[pivot]=='e':			
		t = s[pivot:pivot+5]
		if t=='esare':
			pivot += 5
		else:
			flag = False
			break
	elif s[pivot]=='r':
		if s[pivot+2]=='m':		
			t = s[pivot:pivot+7]
			if t=='remaerd':
				pivot += 7
			else:
				flag = False
				break
		elif s[pivot+2]=='s':	
			t = s[pivot:pivot+6]
			if t=='resare':
				pivot += 6
			else:
				flag = False
				break
		else:
			flag = False
			break
	else:
		flag = False
		break
print('YES' if flag else 'NO')

Solution (Python):
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

S = input()
S1 = S2 = S

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

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

if S1 == '' or S2 == '':
	print('YES')
else:
	print('NO')

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

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

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

Solution (Python):


def main():
    given = ["dreamer", "dream", "eraser", "erase"]
    S = input()
    judge = [S.endswith(i) for i in given]
    while len(S) > 0:
        judge = [S.endswith(i) for i in given]
        if any(judge):
            target = given[judge.index(True)]
            S = S[:-len(target)]
            if len(S) == 0:
                print("YES")
                break
        else:
            print("NO")
            break
            
            




if __name__ == '__main__':
    main()

Solution (Python):
s = input()

while "" != s:
    result = False
    for word in ("dream", "dreamer", "erase", "eraser"):
        if s[-len(word):] == word:
            s = s[:-len(word)]
            result = True
            break
    if result == False:
        break
print("YES" if result else "NO")

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

Solution (Python):
def test():
    s = input()
    divide = ["dream", "dreamer", "erase", "eraser"]
    dp = [0 for i in range(100010)]

    dp[0] = 1
    for i in range(len(dp)):
        if dp[len(s)] == 1:
            print('YES')
            return 0
        if dp[i] == 0:
            continue
        for d in divide:
            if s[i:i+len(d)] == d:
                dp[i+len(d)] = 1
    print('NO')

test()

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

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

Solution (Python):
s = input()

while s != "":
	is_match = False
	for word in ("dream", "dreamer", "erase", "eraser"):
		if s[-len(word):] == word:
			s = s[:-len(word)]
			is_match = True
	if is_match == False:
		print("NO")
		exit()
print("YES")

Solution (Python):
S = input()

five_letters = ["dream", "erase"]

found = False

while True:
    if len(S) == 0:
        found = True
        break

    if S[-5:] in five_letters:
        S = S[:-5]

    elif S[-6:] == "eraser":
        S = S[:-6]

    elif S[-7:] == "dreamer":
        S = S[:-7]

    else:
        break

if found:
    print("YES")

else:
    print("NO")

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

Solution (Python):
s=input()[::-1]
i=0
f=True
while 1:
  p=s[i:i+5]
  if not p:break
  if p=="maerd" or p=="esare":
    i+=5
  elif p=="remae":
    if s[i:i+7]=="remaerd":
      i+=7
    else:
      f=False
      break
  elif p=="resar":
    if s[i:i+6]=="resare":
      i+=6
    else:
      f=False
      break
  else:
    f=False
    break
if f:
  print("YES")
else:
  print("NO")

Solution (Python):
S = input()

marker = len(S)
flag = True

while flag and (marker > 0):

    if S[marker-7:marker] == "dreamer":
        marker -= 7
    elif S[marker-6:marker] == "eraser":
        marker -= 6
    elif S[marker-5:marker] == "dream":
        marker -= 5
    elif S[marker-5:marker] == "erase":
        marker -= 5
    else:
        flag = False
if flag:
    print("YES")
else:
    print("NO")

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

Solution (Python):
S=input()

for i in range(100000):
    if S=="":
        print("YES")
        break
    elif S[-5:-1]=='drea':
        S=S[0:len(S)-5]
    elif S[-7:-1]=='dreame':
        S=S[0:len(S)-7]
    elif S[-5:-1]=='eras':
        S=S[0:len(S)-5]
    elif S[-6:-1]=='erase':
        S=S[0:len(S)-6]
        
if S!="":
    print("NO")

Solution (Python):
a = ["eraser", "erase", "dreamer", "dream"]
s = input()
for i in range(4):
    s = s.replace(a[i], "")
print(['NO','YES'][s==""])

Solution (Python):
S = input()
SR = S[::-1]
temp =['dream','dreamer','erase','eraser']
T=""
j=0
count=0
while j<=10**5//5:
    j +=1
    for i in temp:
        if i[::-1] == SR[count:len(i)+count]:
            count+=len(i)
            T+=i[::-1]
            if SR==T:
                print('YES')
                exit()
print('NO')

Solution (Python):
S = input()
S = S[::-1]
C = ['maerd', 'remaerd', 'esare', 'resare']
ans = False
s = 0
for i in range(len(S)+1):
    if S[s:i] in C:
        s = i
        ans = True
    else:
        ans = False
if ans:
    print("YES")
else:
    print("NO")

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

s = ''
for i in range(len(S)):
    s += S[i]
    if s in T:
        s = ''
    
if len(s) == 0:
    re = 'YES'
else:
    re = 'NO'

print(re)

Solution (Python):


""" 逆順使わないでDPでやる版 """

S = input()

dp = [False] * (len(S)+1)

dp[0] = True

for i in range(1, len(dp)):
    if i-5 >= 0:
        if S[i-5:i] == "dream" or S[i-5:i] == "erase":
            
            if dp[i-5]:
                dp[i] = True
    if i-6 >= 0:
        if S[i-6:i] == "eraser":
            if dp[i-6]:
                dp[i] = True
    if i-7 >= 0:
        if S[i-7:i] == "dreamer":
            if dp[i-7]:
                dp[i] = True

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

Solution (Python):


S = input()
T = ""

words = []
words.append("dream")
words.append("dreamer")
words.append("erase")
words.append("eraser")

for i in range(len(words)):
    words[i] = words[i][::-1]

S = S[::-1]

while len(S) != 0:
    if S[:5] == words[0] or S[:5] == words[2]:
        S = S[5:]
    elif S[:6] == words[3]:
        S = S[6:]
    elif S[:7] == words[1]:
        S = S[7:]
    else:
        print("NO")
        exit()
print("YES")

Solution (Python):
S = input()
ans = True
W = ['dream', 'erase', 'eraser', 'dreamer']
while S:
  if S[:5] in W[:2]:
    match = False
    mw = ''
    for w in W:
      if S == w:
        match = True
        mw = w
    for w1 in W:
      for w2 in W:
        if S[:len(w1) + len(w2)] == w1 + w2:
          match = True
          mw = w1
    if match:
      S = S[len(mw):]
    else:
      ans = False
      break
  else:
    ans = False
    break
print('YES' if ans else 'NO')

Solution (Python):
S = input()

S = S[::-1]
T = ''
i = 0
while i < len(S):
    if S[i] == 'm':
        T += 'maerd'
        i += 5
    elif S[i] == 'e':
        T += 'esare'
        i += 5
    elif S[i:i + 3] == 'rem':
        T += 'remaerd'
        i += 7
    elif S[i:i + 3] == 'res':
        T += 'resare'
        i += 6
    else:
        break

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

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

for i, t in enumerate(T):
    T[i] = t[::-1]

cursor = 0
S = S[::-1]

while cursor < len(S):
    for t in T:
        if t == S[cursor:cursor+len(t)]:
            cursor += len(t)
            break
    else:
        print('NO')
        exit()

print('YES')

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

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

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

for i, t in enumerate(T):
    T[i] = t[::-1]

cursor = 0
S = S[::-1]

while cursor < len(S):
    if S[cursor:cursor+7] == T[1]:
        cursor += 7

    elif S[cursor:cursor+6] == T[3]:
        cursor += 6

    elif S[cursor:cursor+5] == T[0]:
        cursor += 5

    elif S[cursor:cursor+5] == T[2]:
        cursor +=5

    else:
        print('NO')
        exit()

print('YES')

Solution (Python):
import re

def solve(s):
	s=re.sub('eraser','',s)
	s=re.sub('erase','',s)
	s=re.sub('dreamer','',s)
	s=re.sub('dream','',s)
	if s:
		return 'NO'
	else:
		return 'YES'

s=input()
print(solve(s))

Solution (Python):


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

s = input()

for i in range(len(words)):
    words[i] = words[i][::-1]
s = s[::-1]
count = 0
ans = 'YES'
while(count < len(s)):
    for word in words:
        if s[count:count+len(word)] == word:
            count += len(word)
            break
    else:
        ans = 'NO'
        break

print(ans)

Solution (Python):
words = ["dream", "dreamer", "erase","eraser"]
reverse_words = []
for word in words:
    reverse_words.append(word[::-1])
S = input()
S = S[::-1]

while len(S) !=0:
    flag = 0
    for word in reverse_words:
        if S.startswith(word):
            S = S[len(word):]
            flag=1
            break
    if (not flag):
        print("NO")
        quit()
print("YES")

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

s.reverse()

f=1
n=len(s)

a1=list("dream")
a2=list("dreamer")
a3=list("erase")
a4=list("eraser")

a1.reverse()
a2.reverse()
a3.reverse()
a4.reverse()

while f>0 and n>0:
    k=-1
    if n>=7:
        if s[0:7]==a2:
            k=7
    if n>=6:
        if s[0:6]==a4:
            k=6
    if n>=5:
        if s[0:5]==a1:
            k=5
        elif s[0:5]==a3:
            k=5
    
    if k>0:
        del s[0:k]
        n=len(s)
    else:
        f=-1

if f>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):
s = input()

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

Solution (Python):
import re
def getinputdata():

    
    array_result = []
    
    data = input()

    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()
            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:


        return array_result

arr_data = getinputdata()

s = arr_data[0][0]

pattern="^(erase|eraser|dream|dreamer){1,}$"

if(re.search(pattern,s)):
    
    print("YES")

else:
    
    print("NO")

Solution (Python):
s=input()
d='dream'
e='erase'
i=0
while i<=len(s)-1:
    if len(s)-1-i<4:
        i=float('inf')
        break
    elif s[i]=='d':
        for j in range(5):
            if s[i+j]!=d[j]:
                i=float('inf')
                break
        else:    
            if i+6<=len(s)-1 and s[i+5]=='e' and s[i+6]=='r':
                if i+7<=len(s)-1 and s[i+7]=='a':
                    i+=5
                else:
                    i+=7
            else:
                i+=5
    elif s[i]=='e':
        for j in range(5):
            if s[i+j]!=e[j]:
                i=float('inf')
                break
        else:
            if i+5<=len(s)-1 and s[i+5]=='r':
                i+=6
            else:
                i+=5
    else:
        i=float('inf')
        break
if i==float('inf'):
    print('NO')
else:
    print('YES')

Solution (Python):
s = input()
stack = []
choices = ['dream', 'dreamer', 'erase', 'eraser']
for c in choices:
    if s[:len(c)] == c:
        stack.append(len(c))

ans = 'NO'
while len(stack) > 0:
    i = stack[-1]
    
    stack = stack[:-1]
    if i == len(s):
        ans = 'YES'
        break
    for c in choices:
        if s[i:i+len(c)] == c:
            stack.append(i+len(c))
                
print(ans)

Solution (Python):
s = input()

def solve(s):
    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

solve(s)

Solution (Python):
strings = input()

rev_list = ["maerd", "esare", "remaerd", "resare"]

match = ""

for s in reversed(strings):  
  match += s
  
  if match in rev_list:
    match = ""

if len(match) == 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):
s = input()

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

while len(s) != 0:
    is_matched = False
    for p in pattern:
        if s.endswith(p):
            s = s[:-len(p)]
            is_matched = True
            break
    if not is_matched:
        break

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

Solution (Python):
s=str(input())
t=''
len1=0
flag=0
l=len(s)
while len1<=len(s):
    if s[l-(len1+7):]=='dreamer'+t:
        t='dreamer'+t
        flag+=1
        len1+=7
    else:
        if s[l-(len1+5):]=='dream'+t:
            t='dream'+t
            flag+=1
            len1+=5
        else:
            if s[l-(len1+6):]=='eraser'+t:
                t='eraser'+t
                flag+=1
                len1+=6
            else:
                if s[l-(len1+5):]=='erase'+t:
                    t='erase'+t
                    flag+=1
                    len1+=5
                else:
                    pass
 
    if flag==0:
        break
    else:
        if s==t:
            print('YES')
            break
        else:
            flag=0
if s==t:
    pass
else:
    print('NO')

Solution (Python):

S = input()

S_inv = S[::-1]

def judge(S):
    cursor = 0
    len_S = len(S)
    
    while cursor < len_S:
        next_str = S[cursor:cursor+7]
        if next_str[:] == "remaerd":
            cursor += 7
        elif next_str[:6] == "resare":
            cursor += 6
        elif next_str[:5] == "esare":
            cursor += 5
        elif next_str[:5] == "maerd":
            cursor += 5
        else:
            return False
        
    if cursor != len_S:
        return False
    
    return True

if judge(S_inv):
    print("YES")
else:
    print("NO")

Solution (Python):
def judge(S):
    cursor = 0
    len_S = len(S)
    
    while cursor < len_S:
        next_str = S[cursor:cursor+8]
        if next_str[:] == "dreamera":
            cursor += 5
        elif next_str[:7] == "dreamer":
            cursor += 7
        elif next_str[:6] == "eraser":
            cursor += 6
        elif (next_str[:5] == "dream") or (next_str[:5] == "erase"):
            cursor += 5
        else:
            return False
        
    if cursor != len_S:
        return False
    
    return True

S = input()

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

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

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

Solution (Python):
S = input()
a = ''.join(S.split('eraser'))
b = ''.join(a.split('erase'))
c = ''.join(b.split('dreamer'))
d = ''.join(c.split('dream'))
if (d):
	print('NO')
else:
	print('YES')

Solution (Python):
if __name__ == '__main__':
    L = ['dream', 'dreamer', 'erase', 'eraser']
    S = input()
    n = len(S)

    while True:
        if S[-7:] == 'dreamer':
            n -= 7
            S = S[:-7]
        elif S[-6:] == 'eraser':
            n -= 6
            S = S[:-6]
        elif S[-5:] == 'dream' or S[-5:] == 'erase':
            n -= 5
            S = S[:-5]
        else:
            break
    if len(S) == 0:
        print('YES')
    else:
        print('NO')

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

Solution (Python):
S = input()
keywords = ["dream", "dreamer", "erase", "eraser"]
while(True):
    if len(S) == 0:
        print("YES")
        break
    for key in keywords:
        if S.endswith(key):
            S = S[:-len(key)]
            break
    else:
        print("NO")
        break

Solution (Python):
s = input()

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

Solution (Python):
S = input()

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

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

Solution (Python):
s = input()

a = s.split('eraser')

for i in a:
    b = i.split('erase')
    for j in b:
        c = j.split('dreamer')
        for k in c:
            d = k.split('dream')
            for l in d:
                if l != '':
                    print('NO')
                    exit()
print('YES')

Solution (Python):
s = input()

while len(s) >= 5:
    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")
        exit()
        
if len(s) == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys

def text_match_word(text, words):
    for word in words:
        if word == text[:len(word)]:
            return word

words_next = (("dream", "er"), ("erase", "r"))
words_next_dict = dict(words_next)

s = input()

before_match_word = ""
while "" != s:
    search_words = list(words_next_dict.keys())
    if before_match_word in words_next_dict:
        search_words.append(words_next_dict[before_match_word])
    match_word = text_match_word(s, search_words)
    if match_word != None:
        s = s[len(match_word):]
    else:
        print("NO")
        sys.exit()
    before_match_word = match_word
    
print("YES")

Solution (Python):
import re

if re.match("(dream|dreamer|erase|eraser)*$", input()):
    print("YES")
else:
    print("NO")

Solution (Python):
import sys

if __name__ == '__main__':
    S = sys.stdin.readline().rstrip()

    ls = ['dream', 'dreamer', 'erase', 'eraser']
    lenS = len(S)
    dp = [False for i in range(lenS + 10)]

    dp[0] = True

    for i in range(lenS):
        if not dp[i]:
            continue

        for w in ls:
            if S[i:i+len(w)] == w:
                dp[i+len(w)] = True

    r = 'YES' if dp[len(S)] else 'NO'

    print(r)

Solution (Python):
S = list(input())
S = S[::-1]
is_NO = 0
i = 0
while i<len(S):
    if S[i] == 'm':
        if S[i+1] == 'a' and S[i+2] == 'e' and S[i+3] == 'r' and S[i+4] == 'd':
            i += 5
        else:
            is_NO = 1
            break
    elif S[i] == 'r' and S[i+1] == 'e' and S[i+2] == 'm':
        if  S[i+3] == 'a' and S[i+4] == 'e' and S[i+5] == 'r' and S[i+6] == 'd':
            i += 7
        else:
            is_NO = 1
            break
    elif S[i] == 'e':
        if S[i+1] == 's' and S[i+2] == 'a' and S[i+3] == 'r' and S[i+4] == 'e':
            i += 5
        else:
            is_NO = 1
            break
    elif S[i] == 'r' and S[i+1] == 'e' and S[i+2] == 's':
        if S[i+3] == 'a' and S[i+4] == 'r' and S[i+5] == 'e':
            i += 6
        else:
            is_NO = 1
            break
    else:
        is_NO = 1
        break
print("NO" if is_NO else "YES")

Solution (Python):
S = input()

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

for i in a:
    S = S.replace(i,'')
    if S == '':
        print('YES')
        exit()
print('NO')

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

Solution (Python):
import sys
S = input()[::-1]
ls = ["maerd", "remaerd", "esare", "resare"]
n = 0
while n != len(S):
    flag = False
    for i in ls:
        tmp_n = len(i)
        if S[n:n+tmp_n] == i:
            n += len(i)
            flag = True
            break
    if not flag:
        print("NO")
        sys.exit()
print("YES")

Solution (Python):
s = raw_input()

while len(s) > 0:
	if s[-5:] == 'erase' :
		s = s[:-5]

	elif s[-5:] == 'dream' :
		s = s[:-5]

	elif s[-7:] == 'dreamer' :
		s = s[:-7]

	elif s[-6:] == 'eraser' :
		s = s[:-6]

	else :
		print 'NO'
		break

if len(s) == 0 :
	print 'YES'

Solution (Python):
S = input()
flag = False
is_dream, is_erase = False, False

while len(S) >= 5:
    is_dream = S[:5] == 'dream'
    is_erase = S[:5] == 'erase'

    if len(S) == 5:
        if is_dream or is_erase:
            flag = True
        else:
            flag = False
        break

    if len(S) == 6:
        if is_erase and S[-1] == 'r':
            flag = True
        else:
            flag = False
        break

    if len(S) == 7:
        if is_dream and S[-2:] == 'er':
            flag = True
        else:
            flag = False
        break


    if len(S) in [8, 9]:
        flag = False
        break

    if len(S) >= 10:
        S1 = S[5:10]

        if S1 in ['dream', 'erase']:
            S = S[5:]
            continue
        elif is_erase:
            if S[5] == 'r':
                S = S[6:]
                continue
        elif is_dream:
            if S[5:7] == 'er':
                S = S[7:]
                continue
        else:
            flag = False
            break
    flag = False
    break

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

Solution (Python):
import sys
S = sys.stdin.readline().strip()
dp = [False] * (len(S) + 1)
dp[0] = True

ws = ['dream', 'dreamer' ,'erase' ,'eraser']
for i, c in enumerate(S):
    for w in ws:
        j = i-len(w) + 1
        if j >= 0 and dp[j] and S[j:i+1] == w:
            dp[i+1] = True
        if dp[i+1]:
            break
print('YES' if dp[len(S)] else 'NO')

Solution (Python):
S = input()

S = S.replace("dream",'/*/').replace('erase','/+/')
S = [i for i in S.split('/') if i != '']
for i in range(len(S)):
    if S[i] == '*' or S[i] == '+':
        continue
    if S[i] == 'r':
        if i != 0 and S[i-1] == '+':
            continue
    elif S[i] == 'er':
        if i != 0 and S[i-1] == '*':
            continue
    print('NO')
    exit()
print('YES')

Solution (Python):
def m():
    s = str(input())
    d = ["dream", "dreamer"]
    e = ["erase", "eraser"]
    i = len(s)
    while i >= 5:
        if s[i-5:i] == d[0]:
            i -= 5
            continue
        if s[i-5:i] == e[0]:
            i -= 5
            continue
        if s[i-7:i] == d[1]:
            i -= 7
            continue
        if s[i-6:i] == e[1]:
            i -= 6
            continue
        return "NO"
    return "YES"

print(m())

Solution (Python):
import re
print(['NO','YES'][''==''.join(re.split('maerd|remaerd|esare|resare',input()[::-1]))])

Solution (Python):
S = input()[::-1]
A = ["dream"[::-1], "dreamer"[::-1], "erase"[::-1], "eraser"[::-1]]
s = 0
t = 1
while(s != len(S)):
    t2 = 0
    for a in A:
        if S[s:s+len(a)] == a:
            s += len(a)
            t2 = 1

    if t2 ==0:
        t = 0
        break
    
if t:
    print("YES")
else:
    print("NO")

Solution (Python):
s = input()
 
stack = []
for c in reversed(s):
  stack.append(c)
  if len(stack) > 7:
    print('NO')
    break  
  if len(stack) >= 5 and ''.join(reversed(stack)) in ('erase', 'dream', 'eraser', 'dreamer'):
    stack = []
else:
  print('NO' if stack else 'YES')

Solution (Python):
s = input()

stack = []
for c in reversed(s):
  stack.append(c)
  if len(stack) > 7:
    print('NO')
    break  
  if ''.join(reversed(stack)) in ('erase', 'dream', 'eraser', 'dreamer'):
    stack = []
else:
  print('NO' if stack else 'YES')

Solution (Python):
S = input()
T = ''
t = ''
result = 'YES'
dream_list = ['maerd', 'remaerd', 'esare', 'resare']

for i in range(1, len(S)+1):
    t += S[-i]
    if t in dream_list:
        T += t
        t = ''

    if len(t) >7:
        result = 'NO'
        break

if t != '':
    result = 'NO'

print(result)

Solution (Python):

S = input()

while 1:
    if S == "":
        print("YES")
        break
    elif 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

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

Solution (Python):
S = input()

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

S_rev = ""
div_rev = []

S_rev = S[::-1] 
for i in range(4):
    div_rev.append(div[i][::-1]) 

str_len = len(S)
div_len = 0
cnt = 0
temp = cnt
flag = True

while(cnt < str_len):
    temp = cnt
    for j in range(4):
        div_len = len(div_rev[j])
        if(S_rev[cnt:(cnt+div_len)] == div_rev[j]):
            cnt += div_len
    if(temp == cnt):
        flag = False
        break

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

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

Solution (Python):
S=input()
T=""
Vocab=["dream", "dreamer", "erase", "eraser"]
Reverse=[]
for word in Vocab:
    Reverse.append(word[::-1])

SReverse = S[::-1]

def searchWord(TargetString, WordList):
    for word in WordList:
        index=TargetString.find(word)
        if index == 0:
            return True, index+len(word)
        else:
            pass
    return False, index

def cutString(TargetString, slicePoint):
    replacement = TargetString[slicePoint:]
    return replacement

while True:
    if searchWord(SReverse, Reverse)[0]:
        index = searchWord(SReverse, Reverse)[1]
        SReverse = cutString(SReverse, index)
    else:
        break

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

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

while S:
    for w in ws:
        if S.endswith(w):
            S = S[:-len(w)]
            break
    else:
        print('NO')
        exit()
print('YES')

Solution (Python):
s=input()
while True:
    if len(s)==0:
        print("YES")
        break
    elif s[:7]=="dreamer":
        if s[7:11]=="aser":
            s=s[11:]
        elif s[7:10]=="ase":
            s=s[10:]
        
        else:
            s=s[7:]
    elif s[:6]=="eraser":
        s=s[6:]
    elif s[:5]=="dream" or s[:5]=="erase":
        s=s[5:]
    else:
        print("NO")
        break

Solution (Python):
def main():
    s = str(input())
    while(len(s) != 0):
        if s[-6:] == "eraser":
            s = s[:-6]
            continue
        elif s[-7:] == "dreamer":
            s = s[:-7]
            continue
        elif s[-5:] == "erase":
            s = s[:-5]
            continue
        elif s[-5:] == "dream":
            s = s[:-5]
            continue
        else:
            return print("NO")
    print("YES")
    


if __name__ == '__main__':
    main()

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

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

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

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

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

while s:
    tmp = s
    for b in a:
        if s[-len(b):] == b:
            s = s[:-len(b)]
    if tmp == s:
        break

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

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

i = 0
while i < len(S):
    for kw in kws:
        if S[i:i+len(kw)] == kw:
            i += len(kw)
            break
    else:
        print('NO')
        exit()
print('YES')

Solution (Python):
import math
import numpy as np

S = str(input())

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

S_reverse = S[::-1]
can = 1
i = 0

while i < len(S):
    can2 = 0
    for j in range(4):
        d = divider[j]
        if S_reverse[i:i+len(d)] == d:
            can2 = 1
            i += len(d)
            break
    if can2 == 0:
        can = 0
        break

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

Solution (Python):
from sys import stdin

S = stdin.readline().rstrip()[::-1]

result = ''

while True:
    if 'remaerd' == S[:7]:
        S = S[7:]
    elif 'maerd' == S[:5]:
        S = S[5:]
    elif 'esare' == S[:5]:
        S = S[5:]
    elif 'resare' == S[:6]:
        S = S[6:]
    else:
        if len(S) == 0:
            result = 'YES'
        else:
            result = 'NO'
        break;
print(result)