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



Sample input:
erasedream


Sample output:
YES

Submission status: Memory Limit Exceeded

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

S = str(input())

def check(T,S):
    if T == S:
        return 1
    adds = ["dream","dreamer","erase","eraser"]
    for i,add in enumerate(adds):
        if S[len(T):len(T)+len(add)] == add:
            if check(T+add,S) == 1:
                return 1

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

Submission status: Memory Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(100000)

def str_read(s):

    if len(s) == 0:
        return True
    else:
        flag = False
        if s[0:5] in ['dream', 'erase']:
            flag = flag or str_read(s[5:])
        if s[0:6] in ['eraser']:
            flag = flag or str_read(s[6:])
        if s[0:7] in ['dreamer']:
            flag = flag or str_read(s[7:])
        return flag


si = input()

if str_read(si):
    print('YES')
else:
    print('NO')

Submission status: Memory Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

S = list(input().strip())
P = [list(v) for v in ["dream", "dreamer", "erase", "eraser"]]

def dfs(s):
    if len(s) == 0:
        return True
    
    for v in P:
        if s[0:len(v)] == v:
            if dfs(s[len(v):]):
                return True
    
    return False

if dfs(S.copy()):
    print("YES")
else:
    print("NO")

Submission status: Memory Limit Exceeded

Solution code:
S=input()
w=["dream","dreamer","erase","eraser"]
q=[S]
while q:
    s = q.pop()
    if s == "":
        print("YES")
        break
    for c in w:
        if c==s[:len(c)]:q.append(s[len(c):])
else:
    print("NO")

Submission status: Memory Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10000)

S = input()
P = ['dream', 'dreamer', 'erase', 'eraser']

def makeS(T, flg):
    if len(T) > len(S):
        return 0
    
    for p in P:
        T += p
        if T == S:
            return 1
        else:
            flg += makeS(T, flg)
            T = T[:len(T)-len(p)]
    return flg

if makeS('', 0):
    print('YES')
else:
    print('NO')

Submission status: Memory Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)
a=['dream','dreamer','erase','eraser']
def check(c):
    if c=='':
        return True
    rt=False
    size=len(c)
    for i in a:
        if size>=len(i) and c[-len(i):]==i:
            rt=check(c[:-len(i)]) or rt
    return rt

print('YES' if check(input()) else 'NO')

Submission status: Memory Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7) 
S = input()


def is_ok(s):
    if len(s)==0:
        return 'YES'
    elif len(s)<5:
        return 'NO'
    elif 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:
        return 'NO'
    # import pdb;pdb.set_trace()
    ans = is_ok(s)
    return ans

print(is_ok(S))

Submission status: Memory Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7) 
S = input()


def is_ok(s):
    if len(s)==0:
        return 'YES'
    elif len(s)<5:
        return 'NO'
    elif s[-7:]=='dreamer':
        next_s = s[:-7]
    elif s[-6:]=='eraser':
        next_s = s[:-6]
    elif s[-5:]=='dream' or s[-5:]=='erase':
        next_s = s[:-5]
    else:
        return 'NO'
    # import pdb;pdb.set_trace()
    ans = is_ok(next_s)
    return ans

print(is_ok(S))

Submission status: Memory Limit Exceeded

Solution code:
def DFS(S):
    word_list = ["dream", "dreamer", "erase", "eraser"]
    stack = ["dream", "dreamer", "erase", "eraser"]
    while stack:
        word = stack.pop()
        if word == S[0]:
            return "YES"
        for i in word_list:
            next_word = word + i
            check = S[0][:len(word + i)]
            if next_word != check:
                continue
            if next_word == check:
                stack.append(next_word)
    return "NO"






if __name__ == "__main__":
    S = [input()]
    print(DFS(S))

Submission status: Memory Limit Exceeded

Solution code:
from collections import deque
def DFS(S):
    word_list = ["dream", "dreamer", "erase", "eraser"]
    stack = deque(word_list)
    while stack:
        word = stack.pop()
        if word == S[0]:
            return "YES"
        for i in word_list:
            next_word = word + i
            check = S[0][:len(word + i)]
            if next_word != check:
                continue
            if next_word == check:
                stack.append(next_word)
    return "NO"






if __name__ == "__main__":
    S = [input()]
    print(DFS(S))

Submission status: Memory Limit Exceeded

Solution code:
rules = ['dream', 'dreamer', 'erase', 'eraser']

stack = [input()]
ok = False
while len(stack) > 0:
    string = stack.pop()
    if string == '':
        print('YES')
        ok = True
        break
    for rule in rules:
        if string.startswith(rule):
            stack.append(string[len(rule):])
if not ok:
    print('NO')

Submission status: Memory Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)

S = input()

def func(S):
    if S == '':
        return True
    if S[0:5] == 'dream':
        if func(S[5:]): return True
        if S[5:7] == 'er':
            if func(S[7:]): return True
        return False
    if S[0:5] == 'erase':
        if func(S[5:]): return True
        if S[5] == 'r':
            if func(S[6:]): return True
        return False

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

Submission status: Memory Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)

S = input()

def func(S):
    if S == '':
        return True
    a, b, c, d = False, False, False, False
    if S[0:5] == 'dream':
        a = func(S[5:])
        if S[5:7] == 'er':
            b =  func(S[7:])
    elif S[0:5] == 'erase':
        c = func(S[5:])
        if S[5] == 'r':
            d = func(S[6:])
    else:
        return False
    return a or b or c or d

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

Submission status: Memory Limit Exceeded

Solution code:
import sys
# import re

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

sys.setrecursionlimit(10000)

def text_match_words(text, words):
	match_words = []
	for word in words:
		if word == text[:len(word)]:
			match_words.append(word)
	return match_words

def is_match_for(text, words):
	if "" == text: 
		return True
	else:
		match_words = text_match_words(text, words)
		if [] == match_words:
			return False
		else:
			return any([is_match_for(text[len(match_word):], words) for match_word in match_words])

words = ("dream", "dreamer", "erase", "eraser")

s = input()

if is_match_for(s, words):
	print("YES")
else:
	print("NO")

Submission status: Memory Limit Exceeded

Solution code:
S = list(input())
erase = 'erase'
eraser = 'eraser'
dream = 'dream'
dreamer = 'dreamer'
er = 'er'
a = 'a'
r = 'r'
counter = 0
def cut(string):
    if "".join(string[:5]) == dream:
        if "".join(string[5:7]) == er:
            if "".join(string[7:8]) == a:
                string = string[5:]
                cut(string)
            else:
                string = string[7:]
                cut(string)
        else:
            string = string[5:]
            cut(string)
    elif "".join(string[:5]) == erase:
        if "".join(string[5:6]) == r:
            if "".join(string[6:7]) == a:
                string = string[5:]
                cut(string)
            else:
                string = string[6:]
                cut(string)
        else:
                string = string[5:]
                cut(string)
    elif string == []:
        print('YES')
    else:
        print('NO')

cut(S)

Submission status: Memory Limit Exceeded

Solution code:
S = list(input())
erase = 'erase'
eraser = 'eraser'
dream = 'dream'
dreamer = 'dreamer'
er = 'er'
a = 'a'
r = 'r'
counter = 0
def cut(string):
    if "".join(string[:5]) == dream:
        if "".join(string[5:7]) == er:
            if "".join(string[7:8]) == a:
                cut(string[5:])
            else:
                cut(string[7:])
        else:
            cut(string[5:])
    elif "".join(string[:5]) == erase:
        if "".join(string[5:6]) == r:
            if "".join(string[6:7]) == a:
                cut(string[5:])
            else:
                cut(string[6:])
        else:
            cut(string[5:])
    elif string == []:
        print('YES')
    else:
        print('NO')

cut(S)

Submission status: Memory Limit Exceeded

Solution code:
S = raw_input()
D = ["dream", "dreamer", "erase", "eraser"]

que = []
que.append(S)
while len(que) > 0:
    s = que.pop()
    if s == "":
        print "YES"
        exit()
    for d in D:
        if s.find(d) == 0:
            que.append(s[len(d):])
print "NO"

Submission status: Accepted

Solution code:
s = input()
s = s[::-1]
while s:
    if s[:5]=='maerd':
        s = s[5:]
    elif s[:7] =='remaerd':
        s = s[7:]
    elif s[:5] =='esare':
        s = s[5:]
    elif s[:6] =='resare':
        s = s[6:]
    else:
        print('NO');exit()
print('YES')

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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

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

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

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

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

print(can)

Submission status: Accepted

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

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

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

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

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

print(can)

Submission status: Accepted

Solution code:
S = list(input())

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

i = 0

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

Submission status: Accepted

Solution code:
S = input()

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

solve(S)

Submission status: Accepted

Solution code:
S = input()

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

solve(S)

Submission status: Accepted

Solution code:
s = input()


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



solve(s)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()

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

print('YES')

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(100000)


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


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


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


def main():
    S = input()
    # 後ろから貪欲に評価すればよい
    while True:
        if S[-5:] == "dream" or S[-5:] == "erase":
            S = S[:-5]
            continue
        elif S[-6:] == "eraser":
            S = S[:-6]
            continue
        elif S[-7:] == "dreamer":
            S = S[:-7]
            continue
        else:
            break
    if S == "":
        print("YES")
    else:
        print("NO")
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
S=input()

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
S = S[::-1]
a = 'dream'[::-1]
b = 'dreamer'[::-1]
c = 'erase'[::-1]
d = 'eraser'[::-1]
words = [a, b, c, d]
t = 0
for i in range(1, len(S) + 1):
    if S[t:i] in words:
        t = i
if t == len(S):
    print('YES')
else:
    print('NO')

Submission status: Accepted

Solution code:
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")

Submission status: Accepted

Solution code:
import sys

s = input()

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

q = ['']

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

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

print('NO')

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

S = input()

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

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


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

Submission status: Accepted

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

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

S = input()

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

S = readline().rstrip()

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

Submission status: Accepted

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

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

T = ''
T_next = ''

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

print('NO')

Submission status: Accepted

Solution code:
mycode = r'''
# distutils: language=c++
# cython: language_level=3
# cython: boundscheck=False
# cython: wraparound=False
# cython: infer_types=True
# cython: cdivision=True
# False:Cython はCの型に対する除算・剰余演算子に関する仕様を、(被演算子間の符号が異なる場合の振る舞いが異なる)Pythonのintの仕様に合わせ、除算する数が0の場合にZeroDivisionErrorを送出します。この処理を行わせると、速度に 35% ぐらいのペナルティが生じます。 True:チェックを行いません。

from libcpp.string cimport string as Str
# define 
ctypedef long long LL

# 読み込みはpython側で行う
import sys
readline = sys.stdin.buffer.readline().rstrip

cdef LL i,j,k,_


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

# 文字列処理は流石にpypy提出のほうが楽そう
# 致命的な点としては、libcpp.stringがeraseなどの文字列削除操作に対応していない点だ

cdef LL idx=S.size()
while idx>7:
    if S.substr(idx-5,5)==dream: #idxから後ろ5文字以降を表示
        idx-=5 
    elif S.substr(idx-5,5)==erase:
        idx-=5 
    elif S.substr(idx-6,6)==eraser:
        idx-=6
    elif S.substr(idx-7,7)==dreamer:
        idx-=7 
    else: break

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



'''

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

import mycode

Submission status: Accepted

Solution code:
S = input()

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

print('YES')

Submission status: Accepted

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

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

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

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

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

Submission status: Accepted

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

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


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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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


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


if __name__=="__main__":
    main()