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: Time Limit Exceeded

Solution code:
s = input()

checkedList = []
noCheckedList = []
tmp_index = 0

while(True) :

	if( s[tmp_index] == 'd' ) :
		if ( not (tmp_index in checkedList) ) and ( s[tmp_index:tmp_index+5] == 'dream' ) and ( s[tmp_index:tmp_index+7] == 'dreamer' ) :
			noCheckedList.append(tmp_index)
			checkedList.append(tmp_index)
			tmp_index += 7
		elif s[tmp_index:tmp_index+5] == 'dream' :
			tmp_index += 5
		elif s[tmp_index:tmp_index+7] == 'dreamer' :
			tmp_index += 7
	elif( s[tmp_index] == 'e' ) :
		if ( not (tmp_index in checkedList) )  and ( s[tmp_index:tmp_index+5] == 'erase' ) and ( s[tmp_index:tmp_index+6] == 'eraser' ) :
			noCheckedList.append(tmp_index)
			checkedList.append(tmp_index)
			tmp_index += 6
		elif s[tmp_index:tmp_index+5] == 'erase' :
			tmp_index += 5
		elif s[tmp_index:tmp_index+6] == 'eraser' :
			tmp_index += 6
	else :
		if len(noCheckedList) != 0 :
			tmp_index = noCheckedList.pop() + 5
			continue
		else :
			print('NO')
			break
	if tmp_index == len(s):
		print('YES')
		break
	elif len(s) - tmp_index < 5 :
		if len(noCheckedList) != 0 :
			tmp_index = noCheckedList.pop() + 5
		else :
			print('NO')
			break

Submission status: Time Limit Exceeded

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


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


if __name__ == '__main__':
    S = input()

    que = queue.Queue()
    que.put("")
    ans = False
    while not que.empty():
        t = que.get()
        if len(S) == len(t):
            if S == t:
                ans = True
                break
        elif len(S) > len(t):
            if S[:len(t) + 7] == t + "dreamer":
                que.put(t + "dreamer")
            if S[:len(t) + 5] == t + "dream":
                que.put(t + "dream")
            if S[:len(t) + 5] == t + "erase":
                que.put(t + "erase")
            if S[:len(t) + 6] == t + "eraser":
                que.put(t + "eraser")

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


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


if __name__ == '__main__':
    S = input()

    que = queue.Queue()
    que.put("")
    ans = False
    while not que.empty():
        t = que.get()
        if len(S) == len(t):
            if S == t:
                ans = True
                break
        elif len(S) > len(t):
            if S[:len(t)] == t:
                que.put(t + "dreamer")
                que.put(t + "dream")
                que.put(t + "erase")
                que.put(t + "eraser")

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


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


if __name__ == '__main__':
    S = input()

    que = queue.Queue()
    que.put("")
    ans = False
    while not que.empty():
        t = que.get()
        if len(S) == len(t):
            if S == t:
                ans = True
                break
        elif len(S) > len(t):
            que.put(t + "dreamer")
            que.put(t + "dream")
            que.put(t + "erase")
            que.put(t + "eraser")

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

Submission status: Time Limit Exceeded

Solution code:
def resolve():
	s = input()
	w = ['dream', 'dreamer', 'erase', 'eraser']
	h = dict()
	for i in range(len(w)):
		h[w[i][::-1]] = True
	s = s[::-1]
	c = ''
	for i in s:
		c += i
		if h.get(c):
			c = ''
	if not len(c):
		print('YES')
	else:
		print('NO')
resolve()

Submission status: Time Limit Exceeded

Solution code:
def resolve():
	s = input()
	w = ['dream', 'dreamer', 'erase', 'eraser']
	for i in range(len(w)):
		w[i] = w[i][::-1]
	s = s[::-1]
	c = ''
	for i in s:
		c += i
		if c in w:
			c = ''
	if not len(c):
		print('YES')
	else:
		print('NO')
resolve()

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
s.reverse()
str = ""
for i in s:
    str+=i
str = str.replace("resare","")
str = str.replace("esare","")
str = str.replace("remaerd","")
str = str.replace("maerd","")
if len(str)==0:
    print("YES")
else:
    print("NO")

Submission status: Time Limit Exceeded

Solution code:
s = list(input())
s.reverse()
str = ""
for i in s:
    str+=i

#str_list = ["maerd", "remaerd", "esare", "resare"]
str = str.replace("resare","")
str = str.replace("remaerd","")
str = str.replace("maerd","")
str = str.replace("esare","")

"""while True:
    flag=0
    for l in str_list:
        if str.startswith(l):
            print("str:{},l:{}".format(str,l))
            str=str.lstrip(l)
            #ここで想定よりも１文字多く削除されてしまう(erasedream)
            print("str:{},l:{}".format(str,l))
            flag+=1
    if flag==0:
        break"""

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

Submission status: Time Limit Exceeded

Solution code:
s = list(input())

dream = list("dream")
dreamer = list("dreamer")
erase = list("erase")
eraser = list("eraser")

s.reverse()
dream.reverse()
dreamer.reverse()
erase.reverse()
eraser.reverse()

while s != []:
    if s[0:7] == dreamer:
        del s[0:7]
    elif s[0:6] == eraser:
        del s[0:6]
    elif s[0:5] == dream:
        del s[0:5]
    elif s[0:5] == erase:
        del s[0:5]
    else:
        print("NO")
        exit()

print("YES")

Submission status: Time Limit Exceeded

Solution code:
S = input()
s, t = len(S), len(S)
words = ['dream', 'dreamer', 'erase', 'eraser']
while True:
  flg = True
  for i in range(t, -1, -1):
    if S[i:t] in words:
      t = i 
      flg = False
  if flg:
    print('NO')
    break
  if t == 0:
    print('YES')
    break

Submission status: Time Limit Exceeded

Solution code:
S = input()[::-1]
day_dream_word = ["dreamer", "dream", "eraser", "erase"]
day_dream_word = list(map(lambda s: s[::-1], day_dream_word))
T = ""
res = False
partial_res = True
while not res and partial_res:
  partial_res = False
  for add_word in day_dream_word:
    nextT = T
    nextT += add_word
    if S == nextT:
      res = True
      break
    if S[:len(nextT)] == nextT:
      partial_res = True
      break
  T = nextT

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

Submission status: Time Limit Exceeded

Solution code:
# import bisect
from collections import Counter, deque
# from copy import copy, deepcopy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
# from operator import xor
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

def resolve():

    def main():
        S=str(input())
        d=deque(['dream','dreamer','erase','eraser'])
        long=5

        while long<len(S)*2:
            dd = d.popleft()
            long=len(dd)
            if dd==S:
                return 'YES'

            else:
                d.extend([dd+'dream',dd+'dreamer',dd+'erase',dd+'eraser'])

        return 'NO'
    print(main())





resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)

S = input()
T = ""
words = ["dream", "dreamer", "erase", "eraser"]
n = len(S)
def dfs(T, n):
  if len(T) == n:
    return T == S
  if len(T) < n:
    for i in words:
      nT = T + i
      if dfs(nT, n):
        return True
  return False
 
if dfs(T, n):
  print("YES")
else:
  print("NO")

Submission status: Time Limit Exceeded

Solution code:
x=list(str(input()))

chk=0

while chk==0 and len(x)>=5:

	if x[-5]=='d' and x[-4]=='r' and x[-3]=='e' and x[-2]=='a' and x[-1]=='m' :
		x=x[:-5]

	elif x[-5]=='e' and x[-4]=='r' and x[-3]=='a' and x[-2]=='s' and x[-1]=='e' :
		x=x[:-5]

	elif x[-7]=='d' and x[-6]=='r' and x[-5]=='e' and x[-4]=='a' and x[-3]=='m' and x[-2]=='e' and x[-1]=='r':
		x=x[:-7]

	elif x[-6]=='e' and x[-5]=='r' and x[-4]=='a' and x[-3]=='s' and x[-2]=='e' and x[-1]=='r':
		x=x[:-6]

	else:
		chk=1

if chk==0 and len(x)==0:
	print('YES')
else:
	print('NO')

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
# S = list("dreameraser"*2*10**5)
strs = ["dream","dreamer","erase","eraser"]
ind = 0
while True:
    if S[ind] == "e":
        if S[ind:ind+5] == list("erase"):
            if ind+5 == len(S): 
                print("YES")
                exit()
            if S[ind+5] == "r": 
                if ind + 6 ==len(S):
                    print("YES")
                    exit()
                ind+=6 #eraserで確定
            elif S[ind+5] == "d" or S[ind+5]=="e": 
                ind+=5 #dreamかeraser
            else:
                print("NO")
                exit()
        else:
            print("NO")
            exit()
    elif S[ind] == "d":
        if S[ind:ind+5] == list("dream"):
            if ind + 5 == len(S):
                print("YES")
                exit()
            if S[ind+5] == "d" : ind += 5 #"dreamで確定"
            elif S[ind+5] == "e":
                if S[ind+6] == "r":
                    if ind + 7 == len(S):
                        print("YES")
                        exit()
                    if S[ind+7] == "e" or S[ind+7] =="d": ind += 7 #dreamerで確定
                    elif S[ind+7] == "a": ind += 5 #"dream->eraseで確定"
                else:
                    print("NO")
                    exit()
        else:
            print("NO")
            exit()
    else:
        print("NO")
        exit()
    # print(ind,S[ind])


# S = input()
# strs = ["dream","dreamer","erase","eraser"]
# ind = 0
# while True:
#     if S[ind] == "e":
#         if S[ind:ind+5] == "erase":
#             if ind+5 == len(S): 
#                 print("YES")
#                 exit()
#             if S[ind+5] == "r": 
#                 if ind + 6 ==len(S):
#                     print("YES")
#                     exit()
#                 ind+=6 #eraserで確定
#             elif S[ind+5] == "d" or S[ind+5]=="e": 
#                 ind+=5 #dreamかeraser
#             else:
#                 print("NO")
#                 exit()
#         else:
#             print("NO")
#             exit()
#     elif S[ind] == "d":
#         if S[ind:ind+5] == "dream":
#             if ind + 5 == len(S):
#                 print("YES")
#                 exit()
#             if S[ind+5] == "d" : ind += 5 #"dreamで確定"
#             if S[ind+5] == "e":
#                 if S[ind+6] == "r":
#                     if ind + 7 == len(S):
#                         print("YES")
#                         exit()
#                     if S[ind+7] == "e" or S[ind+7] =="d": ind += 7 #dreamerで確定
#                     elif S[ind+7] == "a": ind += 5 #"dream->eraseで確定"
#                 else:
#                     print("NO")
#                     exit()
#         else:
#             print("NO")
#             exit()
#     else:
#         print("NO")
#         exit()
#     # print(ind,S[ind])

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
strs = ["dream","dreamer","erase","eraser"]
ind = 0
while True:
    if S[ind] == "e":
        if S[ind:ind+5] == list("erase"):
            if ind+5 == len(S): 
                print("YES")
                exit()
            if S[ind+5] == "r": 
                if ind + 6 ==len(S):
                    print("YES")
                    exit()
                ind+=6 #eraserで確定
            elif S[ind+5] == "d" or S[ind+5]=="e": 
                ind+=5 #dreamかeraser
            else:
                print("NO")
                exit()
        else:
            print("NO")
            exit()
    elif S[ind] == "d":
        if S[ind:ind+5] == list("dream"):
            if ind + 5 == len(S):
                print("YES")
                exit()
            if S[ind+5] == "d" : ind += 5 #"dreamで確定"
            if S[ind+5] == "e":
                if S[ind+6] == "r":
                    if ind + 7 == len(S):
                        print("YES")
                        exit()
                    if S[ind+7] == "e" or S[ind+7] =="d": ind += 7 #dreamerで確定
                    elif S[ind+7] == "a": ind += 5 #"dream->eraseで確定"
                else:
                    print("NO")
                    exit()
        else:
            print("NO")
            exit()
    else:
        print("NO")
        exit()

Submission status: Time Limit Exceeded

Solution code:
S = input()
strs = ["dream","dreamer","erase","eraser"]
ind = 0
while True:
    # print(ind,S[ind])
    if S[ind] == "e":
        if S[ind:ind+5] == "erase":
            if ind+5 == len(S): 
                print("YES")
                exit()
            if S[ind+5] == "r": 
                if ind + 6 ==len(S):
                    print("YES")
                    exit()
                ind+=6 #eraserで確定
            elif S[ind+5] == "d" or S[ind+5]=="e": 
                ind+=5 #dreamかeraser
            else:
                print("NO")
                exit()
        else:
            print("NO")
            exit()
    elif S[ind] == "d":
        if S[ind:ind+5] == "dream":
            if ind + 5 == len(S):
                print("YES")
                exit()
            if S[ind+5] == "d" :ind += 5 #"dreamで確定"
            if S[ind+5] == "e":
                if S[ind+6] == "r":
                    if ind + 7 == len(S):
                        print("YES")
                        exit()
                    if S[ind+7] == "e" or S[ind+7] =="d": ind += 7 #dreamerで確定
                    elif S[ind+7] == "a": ind += 5 #"dream->eraseで確定"
                else:
                    print("NO")
                    exit()
        else:
            print("NO")
            exit()
    else:
        print("NO")
        exit()
    # print(ind,S[ind])

Submission status: Time Limit Exceeded

Solution code:
#049_C
import sys
sys.setrecursionlimit(100000)

s = input()

def dfs(x):
    if x == s:
        return True
    if len(x) > len(s):
        return False

    if any (dfs(x + k) for k in ['dream', 'dreamer', 'erase', 'eraser']):
        return True
    
    return False

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

Submission status: Time Limit Exceeded

Solution code:
import re
S = input()
while True:
    if S[-3:] == "mer": 
        S = re.sub(r"dreamer$", "", S)
    elif S[-3:] == "ser": 
        S = re.sub(r"eraser$", "", S)
    elif S[-3:] == "eam": 
        S=re.sub(r"dream$", "", S)
    elif S[-3:] == "ase": 
        S=re.sub(r"erase$", "", S)
    else:
        break
print("NO") if S else print("YES")

Submission status: Time Limit Exceeded

Solution code:
s=list(str(input()))
b=0
while len(s)>=5:
  if s[-1]=='r' and len(s)>=7:
    sd=[s[-7],s[-6],s[-5],s[-4],s[-3],s[-2],s[-1]]
    se=[s[-6],s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='dreamer':
      del s[-7:]
    elif ''.join(se)=='eraser':
      del s[-6:]
    else:
      print('NO')
      b+=1
      break
  elif len(s)==6:
    if ''.join(s)=='eraser':
      print('YES')
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='m':
    sd=[s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='dream':
      del s[-5:]
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='e':
    sd=[s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='erase':
      del s[-5:]
    else:
      print('NO')
      b+=1
      break  
  else:
    print('NO')
    b+=1
    break
if len(s)==0:
  print('YES')
elif b!=1:
  print('NO')

Submission status: Time Limit Exceeded

Solution code:
s=list(str(input()))
b=0
while len(s)>=5:
  if s[-1]=='r' and len(s)==6:
    if ''.join(s)=='eraser':
      print('YES')
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='r':
    sd=[s[-7],s[-6],s[-5],s[-4],s[-3],s[-2],s[-1]]
    se=[s[-6],s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='dreamer':
      del s[-7:]
    elif ''.join(se)=='eraser':
      del s[-6:]
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='m':
    sd=[s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='dream':
      del s[-5:]
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='e':
    sd=[s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='erase':
      del s[-5:]
    else:
      print('NO')
      b+=1
      break  
  else:
    print('NO')
    b+=1
    break
if len(s)==0:
  print('YES')
elif b!=1:
  print('NO')

Submission status: Time Limit Exceeded

Solution code:
s=list(str(input()))
b=0
while len(s)>=5:
  if s[-1]=='r' and len(s)>=7:
    sd=[s[-7],s[-6],s[-5],s[-4],s[-3],s[-2],s[-1]]
    se=[s[-6],s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='dreamer':
      del s[-7:]
    elif ''.join(se)=='eraser':
      del s[-6:]
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='r':
    if ''.join(s)=='eraser':
      print('YES')
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='m':
    sd=[s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='dream':
      del s[-5:]
    else:
      print('NO')
      b+=1
      break
  elif s[-1]=='e':
    sd=[s[-5],s[-4],s[-3],s[-2],s[-1]]
    if ''.join(sd)=='erase':
      del s[-5:]
    else:
      print('NO')
      b+=1
      break  
  else:
    print('NO')
    b+=1
    break
if len(s)==0:
  print('YES')
elif b!=1:
  print('NO')

Submission status: Time Limit Exceeded

Solution code:
S = input()

# S = 'dreamerer'

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

flag = False
while True:
    for word in words:
        # word=words[0]
        length = len(word)
        if S[-length:] == word:
            S = S[:len(S) - length]
            flag = True
            break
    if flag:
        if S == '':
            print('YES')
            break
        else:
            continue
    else:
        print('NO')
        break

Submission status: Time Limit Exceeded

Solution code:
S0 = input()
S = S0[::-1]
cands = ["dream"[::-1],"dreamer"[::-1],"erase"[::-1],"eraser"[::-1]]
i = 0
while i < len(S):
  for word in cands:
    #print(S[i:],word)
    if S[i:].startswith(word):
    	i += len(word)
    	continue
#print(i)
if i == len(S):
  print("YES")
else:
  print("NO")

Submission status: Time Limit Exceeded

Solution code:
def resolve():
    S = list(input())
    S.reverse()
    substr = ''
    p = ['dream', 'dreamer', 'erase', 'eraser']
    for i in S:
        substr = i + substr
        if substr in p:
            substr = ''
    ans = 'YES' if substr == '' else 'NO'
    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Time Limit Exceeded

Solution code:
S = input()

# S = 'erasedream'

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

flag = False
while True:
    for word in words:
        # word=words[0]
        length = len(word)
        if S[-length:] == word:
            S = S[:len(S)-length]
            flag = True
            break
    if flag:
        if S == '':
            print('YES')
            break
        else:
            continue
    else:
        print('NO')
        break

Submission status: Time Limit Exceeded

Solution code:
S=input()[::-1]
T=""
L=["dream","dreamer","erase","eraser"] 
for s in S:
    T=T+s
    if T[::-1] in L:
        T=""
print("YES" if T=="" else "NO")

Submission status: Time Limit Exceeded

Solution code:
S=input()
T=""
L=["dream","dreamer","erase","eraser"] 
for s in S[::-1]:
    T=T+s
    if T[::-1] in L:
        T=""
print("YES" if T=="" else "NO")

Submission status: Time Limit Exceeded

Solution code:
s = input()

while len(s) > 0:
  if s == "":
    print('YES')
    break
  s.replace('dreamer','')
  s.replace('eraser','')
  s.replace('dream','')
  s.replace('erase','')
  
print('NO')

Submission status: Time Limit Exceeded

Solution code:
C = ["dream","dreamer","erase","eraser"]
S = input()
N = len(S)
T = [(0,"")]
ans = "NO"
while T:
    t = T.pop()
    if t[0]>N:continue
    if t[0]==N and t[1]==S:
        ans="YES"
    if t[1]!=S[:t[0]]:continue
    for c in C:
        T.append((t[0]+len(c),t[1]+c))
print(ans)

Submission status: Time Limit Exceeded

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

S = input()

def check(s):
  if len(s) >= 5 and (s[-5:] == 'erase' or s[-5:] == 'dream'):
    return check(s[:-5])
  elif len(s) >= 6 and s[-6:] == 'eraser':
    return check(s[:-6])
  elif len(s) >= 7 and s[-7:] == 'dreamer':
    return check(s[:-7])
  elif len(s) == 0:
    return True
  else:
    return False

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

Submission status: Time Limit Exceeded

Solution code:
string = input()

flg = True
while len(string) > 0:
  if string.endswith('dream'):
    string = string[:-5]
  elif string.endswith('dreamer'):
    string = string[:-7]
  elif string.endswith('erase'):
    string = string[:-6]
  elif string.endswith('eraser'):
    string = string[:-6]
  else:
    print('NO')
    flg = False
if flg:
  print('YES')

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 5)
def dfs(si, t):
    for subs in ['dream', 'dreamer', 'erase', 'eraser']:
        if si + len(subs) <= N and S[si:si + len(subs)] == subs:
            if si + len(subs) == N:
                print('YES')
                exit()
            else:
                dfs(si + len(subs), t + subs)


S = input()
N = len(S)
dfs(0, '')
print('NO')

Submission status: Time Limit Exceeded

Solution code:
s = input()
ss = "".join(list(reversed(s)))
a = ["maerd","esare","resare","remaerd"]
flg = 0
while flg == 0:
	if ss[:5] ==a[0] or ss[:5] == a[1]:
		ss = ss[5:]
	elif ss[:6] == a[2]:
		ss = ss[6:]
	elif ss[:7] == a[3]:
		ss = ss[:7]
	else:
		flg = 1
	if len(ss) == 0:
		print ("YES")
		exit()
print ("NO")

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
S = list(input().rstrip())

while S:
    if S[-1] == "m":
        if S[-5:] == ["d", "r", "e", "a", "m"]:
            S = S[:-5]
        else:
            print("NO")
            sys.exit()
    elif S[-1] == "e":
        if S[-5:] == ["e", "r", "a", "s", "e"]:
            S = S[:-5]
        else:
            print("NO")
    elif S[-6:] == list("eraser"):
        S = S[:-6]
    elif S[-7:] == list("dreamer"):
        S = S[:-7]
    else:
        print("NO")
        sys.exit()
print("YES")

Submission status: Time Limit Exceeded

Solution code:
S = input()

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

#Sの反転
S = S[::-1]

#Tの反転
for i in range(len(T)):
    T[i] = T[i][::-1]


idx = 1
for _ in range(len(S)):
    if S[:idx] in T:
        S = S[idx:]
        idx = 0
    idx += 1

if S[:idx] in T:
    s = []

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

Submission status: Time Limit Exceeded

Solution code:
T=[]
S=input()
Sr=list(reversed(S))
w1=list(reversed('dream'))
w2=list(reversed('dreamer'))
w3=list(reversed('erase'))
w4=list(reversed('eraser'))

while True:
    if Sr[:5]==w1:
        Sr=Sr[5:]
    elif Sr[:5]==w3:
        Sr=Sr[5:]
    elif Sr[:6]==w4:
        Sr=Sr[6:]
    elif Sr[:7]==w2:
        Sr=Sr[7:]
    else:
        break
if Sr==T:
    print('YES')
else:
    print('NO')

Submission status: Time Limit Exceeded

Solution code:
S = input()

i = 0
while i <= len(S) - 1:
    if S[i] == "d":
        if i + 10 < len(S):
            if S[i + 10]  == "r":
                if S[i:i+11] != "dreameraser":
                    print("NO")
                    exit()
                else:
                    i += 11
        elif i + 6 < len(S):
            if S[i+6] == "r":
                if S[i:i+7] != "dreamer":
                    print("NO")
                    exit()
                else:
                    i += 7
            elif S[i:i+6] != "dream":
                print("NO")
                exit()
            else:
                i += 6
        elif S[i:i+6] != "dream":
                print("NO")
                exit()
        else:
            i += 6
    elif S[i] == "e":
        if i + 5 < len(S):
            if S[i+5] == "r":
                if S[i:i+6] != "eraser":
                    print("NO")
                    exit()
                else:
                    i += 6
            elif S[i:i+5] != "erase":
                print("NO")
                exit()
            else:
                i += 5
        elif S[i:i+5] != "erase":
                print("NO")
                exit()
        else:
            i += 5
    else:
        print("NO")
        exit()
print("YES")

Submission status: Time Limit Exceeded

Solution code:
S = input()

candidate = ["dream", "dreamer", "erase", "eraser", "dreameraser"]

i = 0
while i <= len(S) - 1:
    if S[i] == "d":
        if i + 10 < len(S):
            if S[i + 10]  == "r":
                if not S[i:i+11] in candidate:
                    print("NO")
                    exit()
                else:
                    i += 11
        elif i + 6 < len(S):
            if S[i+6] == "r":
                if not S[i:i+7] in candidate:
                    print("NO")
                    exit()
                else:
                    i += 7
            elif not S[i:i+6] in candidate:
                print("NO")
                exit()
            else:
                i += 6
        elif not S[i:i+6] in candidate:
                print("NO")
                exit()
        else:
            i += 6
    elif S[i] == "e":
        if i + 5 < len(S):
            if S[i+5] == "r":
                if not S[i:i+6] in candidate:
                    print("NO")
                    exit()
                else:
                    i += 6
            elif not S[i:i+5] in candidate:
                print("NO")
                exit()
            else:
                i += 5
        elif not S[i:i+5] in candidate:
                print("NO")
                exit()
        else:
            i += 5
    else:
        print("NO")
        exit()
print("YES")

Submission status: Time Limit Exceeded

Solution code:

s = input()
words = ['dream','dreamer','erase','eraser']
words_r = [w[::-1] for w in words]
s = s[::-1]

ans = 'NO'
i = 0
while i < len(s):
  p = -1
  for w in words_r:    
    p = s.find(w,i)    
    if p == i:
      i = p + len(w)
      if i == len(s):
        ans ='YES'
      break
  if (p == -1) or ans=='YES':
    break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
S = input()
length_substring_S = len(S)
test_str_list = ['dream', 'dreamer', 'erase', 'eraser']
flag = True

while flag == True:
  flag = False
  for i in range(len(test_str_list)):
    if S[0:length_substring_S].endswith(test_str_list[i]):
      length_substring_S -= len(test_str_list[i])
      flag = True
      break

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

Submission status: Time Limit Exceeded

Solution code:
S = input()
length_substring_S = len(S)
test_str_list = ['dream', 'dreamer', 'erase', 'eraser']
flag = True

while flag == True:
  for i in range(len(test_str_list)):
    if S[0:length_substring_S].endswith(test_str_list[i]):
      length_substring_S -= len(test_str_list[i])
      flag = True
      break
    flag = False

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

Submission status: Time Limit Exceeded

Solution code:
S = list(input())
string = [list("dream") ,  list("dreamer") , list("erase") ,list("eraser")]
for i in range(4):
    string[i] = string[i][::-1]
#print(string)
S = S[::-1]
#print(S[0:5])
while len(S) != 0:
    if S[0:5] == string[0]:
        for _ in range(5):
            S.pop(0)
    elif S[0:7] == string[1]:
        for _ in range(7):
            S.pop(0)
    elif S[0:5] == string[2]:
        for _ in range(5):
            S.pop(0)
    elif S[0:6] == string[3]:
        for _ in range(6):
            S.pop(0)
    else:
        print("NO")
        exit()
print("YES")

Submission status: Time Limit Exceeded

Solution code:
S = input()
S = list(S)
s = list(reversed(S))
def check(s):
  if len(s) >= 6:

    j1 = ''.join(s[:5])
    j2 = ''.join(s[:6])
    j3 = ''.join(s[:7])
  elif len(s) == 5:
    j1 = ''.join(s[:5])
    j2 = ''.join(s[:6])
    j3 = ''
  else:
    j1 = ''.join(s[:5])
    j2 = ''
    j3 = ''  
  if (j1 == 'esare') or (j1 == 'maerd'):
    x =  ''.join(s[5:])
    return x
  elif j2 == 'resare':
    x = ''.join(s[6:])
    return x
  elif j3 == 'remaerd':
    x = ''.join(s[:7])
    return x
  else:
    return 'no'

while True:
  s = list(check(s))
  if s == list('no'):
    print('NO')
    break
  if s == []:
    print('YES')
    break

Submission status: Time Limit Exceeded

Solution code:
words = input()
word_list = [i for i in words[::-1]]
ans_a = ["dreamer","eraser","dream","erase"]
ans_b = [j[::-1] for j in ans_a]

answer = "YES"
a = 0
b = 1
while True:
  if b > len(word_list):
    break
  else:
    now_word = "".join(word_list[a:b])
    if (now_word == ans_b[0])or(now_word == ans_b[1])or(now_word == ans_b[2])or(now_word == ans_b[3]): 
      answer = "YES"
      a = b
    else:
      answer = "NO"
    b += 1

print(answer)

Submission status: Time Limit Exceeded

Solution code:
words = input()
word_list = [i for i in words[::-1]]
ans_a = ["dreamer","eraser","dream","erase"]
ans_b = [j[::-1] for j in ans_a]

answer = "YES"
a = 0
b = 1
while True:
  if b > len(word_list):
    break
  else:
    now_word = "".join(word_list[a:b])
    if (now_word == ans_b[0])or(now_word == ans_b[1])or(now_word == ans_b[2])or(now_word == ans_b[3]): 
      answer = "YES"
      a = b
    else:
      answer = "NO"
    b += 1

print(answer)

Submission status: Time Limit Exceeded

Solution code:
words = input()
word_list = [i for i in words[::-1]]
ans_a = ["dreamer","eraser","dream","erase"]
ans_b = [j[::-1] for j in ans_a]

answer = "YES"
a = 0
b = 1
while True:
  if b > len(word_list):
    break
  else:
    now_word = "".join(word_list[a:b])
    for c in range(0,4):
        ans = ans_b[c]
        if now_word == ans:
          answer = "YES"
          a = b
          break
        else:
          answer = "NO"
    b += 1

print(answer)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque

S = input()

cands = ["dream", "dreamer", "erase", "eraser"]
searched = {}

queue = deque([""])
find_flag = 0
last_s = ""
while len(queue) != 0:
    s = queue.popleft()
    searched[s] = 1
    if s == S:
        find_flag = 1
        break
        
    s_ = cands[0]
    if (s_ == S[len(s):len(s)+len(s_)]) & (not searched.get(s+s_)):
        queue.append(s+s_)

    s_ = cands[1]
    if (s_ == S[len(s):len(s)+len(s_)]) & (not searched.get(s+s_)):
        queue.append(s+s_)
        
    s_ = cands[2]
    if (s_ == S[len(s):len(s)+len(s_)]) & (not searched.get(s+s_)):
        queue.append(s+s_)
        
    s_ = cands[3]
    if (s_ == S[len(s):len(s)+len(s_)]) & (not searched.get(s+s_)):
        queue.append(s+s_)
        
#     # 追加できる候補が無くなったら抜ける
#     break

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