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):
input_str = input()
process_flag = 'process'
ans_flag = 'NO'
d_e = ''
ans_list = []

def pre_str_process(now_str):
  ans_list.append(now_str[:5])
  now_str = now_str[5:]
  return now_str

def dream(now_str):
  now_str = pre_str_process(now_str)
  if (now_str == '') or (now_str == 'er'):
    return now_str, 'OK'
  if now_str[:5] == 'dream':
    return now_str, 'dream'
  if now_str[:2] == 'er':
    if now_str[:5] == 'erase':
      return now_str, 'erase'
    if now_str[:7] == 'ererase':
      now_str = now_str[2:]
      return now_str, 'erase'
    if now_str[:7] == 'erdream':
      now_str = now_str[2:]
      return now_str, 'dream'
    else:
      return now_str, 'NG'

  else:
    return now_str, 'NG'

def erase(now_str):
  now_str = pre_str_process(now_str)
  if (now_str == '') or (now_str == 'r'):
    return now_str, 'OK'
  if now_str[0] == 'r':
    now_str = now_str[1:]
    if now_str[:5] == 'dream':
      return now_str, 'dream'
    if now_str[:5] == 'erase':
      return now_str, 'erase'
    else:
      return now_str, 'NG'
  if now_str[:5] == 'erase':
    return now_str, 'erase'
  if now_str[:5] == 'dream':
    return now_str, 'dream'
  else:
    return now_str, 'NG'

first_str = input_str[:5]
process_num = 0
while process_flag == 'process':
  process_num += 1
  if process_num == 1:
    if first_str == 'dream':
      now_str = input_str
      d_e = 'dream'
    elif first_str == 'erase':
      now_str = input_str
      d_e = 'erase'
    else:
      break
  elif d_e == 'dream':
    now_str, d_e = dream(now_str)
  elif d_e == 'erase':
    now_str, d_e = erase(now_str)
  elif d_e == 'OK':
    ans_flag = 'YES'
    break
  elif d_e == 'NG':
    break
print(ans_flag)

Solution (Python):
s = input()
i = 0
while True:
    if s == "":
        print("YES")
        exit(0)
    if i == 0:
        if s[:5] == "dream":
            s = s[5:]
            i = 1
        elif s[:5] == "erase":
            s = s[5:]
            i = 2
        else:
            break
    elif i == 1:
        if s[:5] == "dream":
            s = s[5:]
            i = 1
        elif s[:2] == "er":
            s = s[2:]
            i = 3
        else:
            break
    elif i == 2:
        if s[:5] == "dream":
            s = s[5:]
            i = 1
        elif s[:5] == "erase":
            s = s[5:]
            i = 2
        elif s[:1] == "r":
            s = s[1:]
            i = 0
        else:
            break
    elif i == 3:
        if s[:5] == "dream":
            s = s[5:]
            i = 1
        elif s[:3] == "ase":
            s = s[3:]
            i = 2
        elif s[:5] == "erase":
            s = s[5:]
            i = 2
        else:
            break
print("NO")

Solution (Python):
s = input()
S = ''.join(list(reversed(s)))
N = len(S)

i = 0
while True:
    l = N - i
    if l>=5 and (S[i:i+5]=='maerd' or S[i:i+5]=='esare'):
        i += 5
    elif l>=6 and S[i:i+6]=='resare':
        i += 6
    elif l>=7 and S[i:i+7]=='remaerd':
        i += 7
    else:
        print("NO")
        exit()
    if i == N:
        print('YES')
        exit()

Solution (Python):
s=input()
l=len(s)
def judge():
  cnt=0
  while True:
    if s[l-cnt-6:l-cnt]=='eraser':
      cnt+=6
    elif s[l-cnt-5:l-cnt]=='erase':
      cnt+=5
    elif s[l-cnt-7:l-cnt]=='dreamer':
      cnt+=7
    elif s[l-cnt-5:l-cnt]=='dream':
      cnt+=5
    else:
      print('NO')
      exit()
    if cnt==len(s):
      print('YES')
      exit()

judge()

Solution (Python):
S = input()
T = ['dream','dreamer','erase','eraser']
while len(S) != 0:
    for t in T:
        if S.endswith(t):
            S = S[:len(S)-len(t)]
            break
    else:
        print('NO')
        exit()
print('YES')

Solution (Python):



def main():
    s = input()
    c = ["dreamer", "dream", "erase", "eraser"]
    n = [7, 5, 5, 6]
    while True:
        f = True
        for i in range(4):
            if s[-n[i] :] == c[i]:
                f = False
                s = s[: -n[i]]
                break
        if f:
            if len(s) == 0:
                print("YES")
            else:
                print("NO")
            return


if __name__ == "__main__":
    main()

Solution (Python):



def main():
    s = list(input())
    c = ["dreamer", "dream", "erase", "eraser"]
    n = [7, 5, 5, 6]
    while True:
        f = True
        for i in range(4):
            if "".join(s[-n[i] :]) == c[i]:
                f = False
                s = s[: -n[i]]
                break
        if f:
            if len(s) == 0:
                print("YES")
            else:
                print("NO")
            return


if __name__ == "__main__":
    main()

Solution (Python):
S = input()
while True:
    l = len(S)
    if l == 0:
        print('YES'); break
    if l-5 >= 0 and S[l-5:] == 'dream':
        S = S[:l-5]; continue
    if l-7 >= 0 and S[l-7:] == 'dreamer':
        S = S[:l-7]; continue
    if l-5 >= 0 and S[l-5:] == 'erase':
        S = S[:l-5]; continue
    if l-6 >= 0 and S[l-6:] == 'eraser':
        S = S[:l-6]; continue
    print('NO')
    break

Solution (Python):
s = input()

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

Solution (Python):
s=input()
while True:
    if len(s) == 0:
        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 = list(input())

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

pointer = len(s)
while pointer > 0:
    if s[pointer-5: pointer] == list(words[0]):
        pointer -= 5
    elif s[pointer-5: pointer] == list(words[2]):
        pointer -= 5
    elif s[pointer-7: pointer] == list(words[1]):
        pointer -= 7
    elif s[pointer-6: pointer] == list(words[3]):
        pointer -= 6
    else:
        ans = 'NO'
        break

print(ans)

Solution (Python):
S = input()

while 1:
    for target in ["dream", "dreamer", "erase", "eraser"]:
        if S.endswith(target):
            S = S[:-len(target)]
            break
    else:
        print("NO")
        break
    if len(S) == 0:
        print("YES")
        break

Solution (Python):
s = input()
s=s[::-1]
i=0
ans="YES"
while True:
    if len(s)==i:
        break
    elif s[i:i+5]=="maerd":
        i+=5
    elif s[i:i+7]=="remaerd":
        i+=7
    elif s[i:i+5]=='esare':
        i+=5
    elif s[i:i+6]=='resare':
        i+=6
    else:
        ans="NO"
        break
print(ans)

Solution (Python):
import re;print(re.match('(dream|dreamer|erase|eraser)*$',input())and'YES'or'NO')

Solution (Python):
S=input()
L=len(S)
X=('dream','dreamer','erase','eraser')
while L>0:
    f=1
    for x in X:
        if S[:L].endswith(x):
            L-=len(x)
            f=0
    if f:
        break
print(['NO','YES'][L==0])

Solution (Python):
S = input()

T = ""

while True:
    T_len = len(T)
    for word in ["dream", "dreamer", "erase", "eraser"]:
        if word + T == S[-1 * len(word + T):]:
            T = word + T
            if T == S:
                print("YES")
                exit()
    if T_len == len(T):
        print("NO")
        exit()

Solution (Python):
a,b,c,d=[i[::-1] for i in ['dream','dreamer','erase','eraser']]
s,j=input()[::-1],True
while j:
  j=False
  for i in [a,b,c,d]:
    if s.startswith(i): s=s[len(i):];j=True
print('YNEOS'[len(s)>0::2])

Solution (Python):
s = str(input())
z = 'YES'
while len(s) != 0:
    if s[:11] == 'dreameraser':
        s = s[11:]
    elif s[:10] == 'dreamerase':
        s = s[10:]
    elif s[:13] == 'dreamereraser':
        s = s[13:]
    elif s[:12] == 'dreamererase':
        s = s[12:]
    elif s[:7] == 'dreamer':
        s = s[7:]
    elif s[:5] == 'dream':
        s = s[5:]
    elif s[:6] == 'eraser':
        s = s[6:]
    elif s[:5] == 'erase':
        s = s[5:]
    else:
        z = 'NO'
        break
print(z)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

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



if __name__ == "__main__":
    main()

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

def f(x):
    print(x)
    exit()

if N < 5:
    f("NO")
elif S[0] == "d":
    w = 1
elif S[0] == "e":
    w = -1
else:
    f("NO")

i = 0
while True:
    if w > 0:
        if S[i]+S[i+1]+S[i+2]+S[i+3]+S[i+4] == "dream":
            i += 5
            if i == N:
                f("YES")
            elif S[i] == "d":
                pass
            elif S[i] == "e":
                if i+1 <= N-1:
                    if S[i+1] == "r":
                        if i+1 == N-1:
                            f("YES")
                        elif S[i+2] == "a":
                            w *= -1
                        elif S[i+2] == "e":
                            i += 2
                            w *= -1
                        elif S[i+2] == "d":
                            i += 2
                        else:
                            f("NO")
                    else:
                        f("NO")
                else:
                    f("NO")
            else:
                f("NO")
        else:
            f("NO")
    elif w < 0:
        if S[i]+S[i+1]+S[i+2]+S[i+3]+S[i+4] == "erase":
            i += 5
            if i == N:
                f("YES")
            if S[i] == "r":
                i += 1
                if i == N:
                    f("YES")
            if S[i] == "d":
                w *= -1
            elif S[i] == "e":
                pass
            else:
                f("NO")
        else:
            f("NO")
    if i+4 > N-1:
        f("NO")

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

Solution (Python):
from collections import Counter
s = input()
i = 0
while i < len(s):
    if s[i] == 'd':
        S = s[i:i + 5]
        if i + 6 < len(s):
            if S + s[i + 5] + s[i + 6] == "dreamer":
                i += 7
                if i + 3 < len(s):
                    if s[i:i + 4] == "aser":
                        i += 4
                        continue
                if i + 2 < len(s):
                    if s[i:i + 3] == "ase":
                        i += 3
                        continue
                continue
        if S == "dream":
            i += 5
            continue
        break
    elif s[i] == 'e':
        S = s[i:i + 5]
        if i + 5 < len(s):
            if S + s[i + 5] == "eraser":
                i += 6
                continue
        if S == "erase":
            i += 5
            continue
        break
    else:
        break
    if i + 4 >= len(s):
        break
else:
    print("YES")
    exit()
print("NO")

Solution (Python):
txtd = ["dreameraser",
        "dreamerase",
        "dreamer",
        "dream"]
txtd_n = [len(a) for a in txtd]
txtd_l = len(txtd)
txte = ["eraser",
        "erase"]
txte_n = [len(a) for a in txte]
txte_l = len(txte)
        
S = input()
len_idx = len(S)
i = 0
able = True
while i < len_idx:
  old_i = i
  remain = len_idx - i
  
  if S[i] == "d":
    for j in range(txtd_l):
      if remain >= txtd_n[j]:
        if S[i:i+txtd_n[j]] == txtd[j]:
          i += txtd_n[j]
          break
  elif S[i] == "e":
    for j in range(txte_l):
      if remain >= txte_n[j]:
        if S[i:i+txte_n[j]] == txte[j]:
          i += txte_n[j]
          break

  if old_i == i:
    able = False
    break

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

Solution (Python):
S = input()

dm, dr, ee, er = 'dream', 'dreamer', 'erase', 'eraser'

word = [dm[::-1], dr[::-1], ee[::-1], er[::-1], ]

s = S[::-1]

while s:
    for i in range(4):
        if s.startswith(word[i]):
            s = s[len(word[i]):]
            break
    else:
        print('NO')
        break
else:
    print('YES')

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

for i in range(100000):
    if s[:5] == 'maerd':
        s = s[5:]
    if s[:5] == 'esare':
        s = s[5:]
    if s[:6] == 'resare':
        s = s[6:]
    if s[:7] == 'remaerd':
        s = s[7:]

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

Solution (Python):
s=input()
s_re = s[::-1]
list =  ["dream","dreamer","erase", "eraser"]
list_re = [ i[::-1] for i in list]

count=True
while count:
    value=0
    for i in list_re:
        if s_re[:len(i)] == i:
            s_re = s_re[len(i):]
            value=0
        else:
            value+=1
    if value>3:
        count=False

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

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

while S:
    for candidate in candidates:
        if S.endswith(candidate):
            S = S[:-len(candidate)]
            break
    else:
        break

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

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

Solution (Python):
s = input()

ans = True
while s != "":
    words = ["eraser", "dream", "erase", "dreamer"]

    found = False
    for word in words:
        if s[-len(word):] == word:
            s = s[:-len(word)]
            found = True
            break

    if not found:
        ans = False
        break

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

Solution (Python):
S = input()


S = S[::-1] 
words = {'maerd', 'remaerd', 'esare', 'resare'}

while len(S) > 0:
  orig = S
  for w in words:
    if S.startswith(w):
      S = S[len(w):]
      break
  if orig == S:
    break

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

Solution (Python):
S = input()
 


while True:
  if S[-5:] == 'erase': S = S[:-5]
  else:
    if S[-6:] == 'eraser': S = S[:-6]
    else:
      if S[-5:] == 'dream': S = S[:-5]
      else:
        if S[-7:] == 'dreamer': S = S[:-7]
        else: break

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

Solution (Python):


s = input()

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

def check():
    tmp = s
    while(True):
      current_len = len(tmp)
      for w in words:
        if tmp.endswith(w):
          tmp = tmp[:-len(w)]
      if len(tmp) == 0:
          return "YES"

      if current_len == len(tmp):
          return "NO"

print(check())

Solution (Python):
s = input()

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

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

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

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

Solution (Python):
A = input()
A = A[::-1]
divide = [i[::-1] for i  in ["dream", "dreamer", "erase", "eraser"]]

flag = 1
while flag and A:
  flag = 0
  for word in divide:
    if A.startswith(word):
      A = A[len(word):]
      flag = 1

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

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

while len(S) > 0:
  for t in text:
    if S.find(t) == 0:
      S = S[len(t):]
      break
  else:
    print('NO')
    break
else:
  print('YES')

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

Solution (Python):
s = str(input())
r = ''.join(list(reversed(s)))
flag = [0,0,0,0]
t = ['maerd', 'remaerd', 'esare', 'resare']
while len(r) >= 0:
    if r[0:5] == t[0]:
        r = r[5:]
        flag[0] +=1
    elif r[0:7] == t[1]:
        r = r[7:]
        flag[1] +=1
    elif r[0:5] == t[2]:
        r = r[5:]
        flag[2] +=1
    elif r[0:6] == t[3]:
        r = r[6:]
        flag[3] +=1
    elif len(r) == 0:
        print('YES')
        break
    else:
        print('NO')
        break

Solution (Python):
S = list(input())
S.reverse()
S = ''.join(S)
st = ['maerd','remaerd','esare','resare']
ans = 'YES'
while S != '':
    if S[0:5] in st:
        S = S[5:]
    elif S[0:6] in st:
        S = S[6:]
    elif S[0:7] in st:
        S = S[7:]
    else:
        ans = 'NO'
        break
print(ans)

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

a = list("dream")
b = list("dreamer")
c = list("erase")
d = list("eraser")
flag = 0

while(flag == 0):
    if s[-7:] == b:
        del s[-7:]
    elif s[-6:] == d:
        del s[-6:]
    elif s[-5:] == a or s[-5:] == c:
        del s[-5:]
    elif s == []:
        print("YES")
        flag = 1
    else:
        print("NO")
        flag = 1

Solution (Python):
s=input()
p=True
t=""
l=s
while p==True:
    p=False
    f=l[0:8]
    if f=="dreamera":
        t +="dream"
        l=l[5:]
        p=True
    f=l[0:7]
    if p==False and f=="dreamer":
        t += "dreamer"
        l=l[7:]
        p=True
    f=l[0:6]
    if p==False and f=="eraser":
        t+="eraser"
        l=l[6:]
        p=True
    f=l[0:5]
    if p==False and f=="erase":
        t+="erase"
        l=l[5:]
        p=True
    if p==False and f=="dream":
        t+="dream"
        l=l[5:]
        p=True
if s==t:
    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):
import re
s=input()
s=re.sub('eraser','',s)
s=re.sub('erase','',s)
s=re.sub('dreamer','',s)
s=re.sub('dream','',s)
if s=='':
    print('YES')
else:
    print('NO')

Solution (Python):

import sys
sys.setrecursionlimit(10**6)

C = ['dream', 'dreamer', 'erase', 'eraser']
S = input()
L = len(S)

def dfs(l):
    if l == L:
        return True
    elif l > L:
        return False

    for c in C:
        if S.find(c, l, l+7) == l:
            if dfs(l+len(c)):
                return True
    return False

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

Solution (Python):

import sys
sys.setrecursionlimit(10**6)

C = ['dream', 'dreamer', 'erase', 'eraser']
S = input()
L = len(S)


def dfs(x, l):
    if l == L:
        return True
    elif l > L:
        return False

    match = S[l:l+7]
    for c in C:
        if match.find(c) == 0:
            if dfs(c, l+len(c)):
                return True
    return False


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

Solution (Python):
words = ['dream','dreamer', 'erase', 'eraser']
s = raw_input()
dp = [False for _ in range(len(s))]

for j in range(len(s)):
	for w in words:
		b = dp[j - len(w)] if j - len(w) -1 >= 0 else True
		b &= j-len(w) +1 >= 0 and s[j- len(w)+1:j+1] == w
		if b:
			dp[j] = True
			break
print 'YES' if dp[-1] else 'NO'

Solution (Python):
import re
                                      
S = input()
S = re.sub("eraser", "", S)
S = re.sub("erase", "", S)
S = re.sub("dreamer", "", S)
S = re.sub("dream", "", S)
print("YES" if len(S) == 0 else "NO")

Solution (Python):
s = input()
s = s[::-1]
t = ["dream", "dreamer", "erase", "eraser"]
for i in range(len(t)):
    t[i] = t[i][::-1]
can = True
i = 0
while i < len(s):
    can2 = False
    for j in range(len(t)):
        d = t[j]
        if s[i:i+len(d)] == d:
            can2 = True
            i += len(d)
    if not can2:
        can = False
        break

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

Solution (Python):

import collections as cl
import sys


def II():
    return int(sys.stdin.readline())


def MI():
    return map(int, sys.stdin.readline().split())


def LI():
    return list(map(int, sys.stdin.readline().split()))


A = "dream"
B = "dreamer"
C = "erase"
D = "eraser"


def main():
    S = input()
    T = []
    i = 0
    while i <= len(S)-1:
        target = S[i:i+5]
        if target == A:
            i += len(A)
            T.append("A")
            continue
        if target == C:
            i += len(C)
            T.append("C")
            continue
        T.append(target[0])
        i += 1

    if T[0] != "A" and T[0] != "C":
        print("NO")
        return

    for i in range(1, len(T)):
        if T[i] == 'r':
            if T[i-1] == "e":
                if i >= 2 and T[i-2] == "A":
                    continue
                print('NO')
                return
            if T[i-1] == "C":
                continue
            print('NO')
            return

    print('YES')


main()

Solution (Python):

S = input()

S = S[::-1]

cand = {'maerd', 'remaerd', 'esare', 'resare'}

can = True

while S not in cand:
    ok = False
    for x in cand:
        if S[:len(x)] == x:
            S = S[len(x):]
            ok = True
            break
    if ok == False:
        can = False
        break

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

Solution (Python):
s=input()
l=['dreamer','eraser','dream','erase']
t=''
a=0
for c in s[::-1]:
    t=c+t
    if t in l:
        a+=len(t)
        t=''
print('YES'if a==len(s)else'NO')

Solution (Python):
def main2():
    S = input()
    idx = 0
    while idx < len(S):
        if S[idx:].startswith("dream"):
            e = S[idx+5:idx+5+3]

            if len(e) == 3:
                if e.startswith("d") or e == "era":
                    idx += 5

                elif e == "erd" or e == "ere":
                    idx += 7
                    
                else:
                    idx += 5

            elif len(e) == 2:
                if e == "er":
                    idx += 7
                else:
                    idx += 5

            elif len(e) == 1:
                idx += 5
            
            else:
                idx += 5

        elif S[idx:].startswith("eraser"):
            idx += 6

        elif S[idx:].startswith("erase"):
            idx += 5

        else:
            print("NO")
            exit()

    print("YES")

if __name__ == "__main__":
    main2()

Solution (Python):
S=input()
def solve(query):
    while 1:
        if not query:
            print("YES")
            break

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

solve(S)

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

Solution (Python):

 
I = ["dream", "dreamer", "erase", "eraser"]
I2 = input()
 
if I2.replace(I[1], "").replace(I[0], "").replace(I[3], "").replace(I[2], "").strip() == "":
  print("YES")
elif I2.replace(I[0], "").replace(I[2], "").replace(I[1], "").replace(I[3], "").strip() == "":
  print("YES")
elif I2.replace(I[2], "").replace(I[0], "").replace(I[3], "").replace(I[1], "").strip() == "":
  print("YES")
elif I2.replace(I[3], "").replace(I[2], "").replace(I[1], "").replace(I[0], "").strip() == "":
  print("YES")
else:
  print("NO")

Solution (Python):
S = input()
words = ['dreamer','dream','eraser','erase']
i = len(S)
while i>0:
    update = False
    for w in words:
        if S[:i].endswith(w):
            i -= len(w)
            update =True
            break
    if not update:
        break
print('YES' if i == 0 else 'NO')

Solution (Python):
S = input()
words = ['dreameraser','dreamerase','dreamer','dream','eraser','erase']
i = 0
while i < len(S):
    update = False
    for w in words:
        if S[i:].startswith(w):
            i += len(w)
            update =True
            break
    if not update:
        break
print('YES' if i == len(S) else 'NO')

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

Solution (Python):
import re

s = input()
s = re.sub("eraser","",s)
s = re.sub("erase","",s)
s = re.sub("dreamer","",s)
s = re.sub("dream","",s)

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

Solution (Python):
S = input()
N = len(S)
i = 0
while N-i >4:
    if S[N-i-5:N-i] == "dream":
        i+=5
    elif N-i>6 and S[N-i-7:N-i]=="dreamer":
        i+=7
    elif S[N-i-5:N-i] == "erase":
        i += 5
    elif N-i>5 and S[N-i-6:N-i] =="eraser":
        i+=6
    else:
        break

if N == i:
    print("YES")
else:
    print("NO")

Solution (Python):
from collections import defaultdict,deque
import math
def main():
    s = input()
    lis = ["maerd", "remaerd", "esare", "resare"]
    s = s[::-1]

    i = 0
    while(i < len(s)-4):
        if(s[i:i+5] in lis):
            i+=5
        elif(i+7 <= len(s) and s[i:i+7]=="remaerd"):
            i += 7
        elif(i+6<=len(s) and s[i:i+6]=="resare"):
            i+=6
        else:
            print("NO")
            exit()
    print("YES")
if __name__ == '__main__':
    main()

Solution (Python):
s=list(input())
n=len(s)
t=n-1
while t>=0:
    if s[t]=='r':
        if t<5:
            print('NO')
            exit()
        elif s[t-1]=='e' and s[t-2]=='s' and s[t-3]=='a' and s[t-4]=='r' and s[t-5]=='e':
            t-=6
        elif t<6:
            print('NO')
            exit()
        elif s[t-1]=='e' and s[t-2]=='m' and s[t-3]=='a' and s[t-4]=='e' and s[t-5]=='r' and s[t-6]=='d':
            t-=7
        else:
            print('NO')
            exit()
    elif s[t]=='e':
        if t<4:
            print('NO')
            exit()
        elif s[t-1]=='s' and s[t-2]=='a' and s[t-3]=='r' and s[t-4]=='e':
            t-=5
        else:
            print('NO')
            exit()
    elif s[t]=='m':
        if t<4:
            print('NO')
            exit()
        elif s[t-1]=='a' and s[t-2]=='e' and s[t-3]=='r' and s[t-4]=='d':
            t-=5
        else:
            print('NO')
            exit()
    else:
        print('NO')
        exit()
print('YES')

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

Solution (Python):
s,f=input()[::-1],-1
while f<0:
    if s:
        if s[:5]=='maerd':
            s=s[5:]
        elif s[:5]=='esare':
            s=s[5:]
        elif s[:6]=='resare':
            s=s[6:]
        elif s[:7]=='remaerd':
            s=s[7:]
        else:
            f=0
    else:
        f=1
print(['NO','YES'][f])

Solution (Python):
import re


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


if __name__ == "__main__":
    main()

Solution (Python):
import re


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


if __name__ == "__main__":
    main()

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

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

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

s = s[::-1]

while True:
    if s.startswith(list[0][::-1]):
        n -= 5
        s = s[5:]
    elif s.startswith(list[1][::-1]):
        n -= 7
        s = s[7:]
    elif s.startswith(list[2][::-1]):
        n -= 5
        s = s[5:]
    elif s.startswith(list[3][::-1]):
        n -= 6
        s = s[6:]
    else:
        break

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

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

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

s = "".join(reversed(list(s)))
while n:
    if s.startswith("maerd"):
        n -= 5
        s = s[5:]
    elif s.startswith("remaerd"):
        n -= 7
        s = s[7:]
    elif s.startswith("esare"):
        n -= 5
        s = s[5:]
    elif s.startswith("resare"):
        n -= 6
        s = s[6:]
    else:
        break
print("YES" if n == 0 else "NO")

Solution (Python):
s=input()
s=s[::-1]
f=0
l=['dream' ,'dreamer', 'erase','eraser']
l=[c[::-1] for c in l]
while(f==0):
    if(s[:7] in l):
        s=s[7:]
    elif(s[:6] in l):
        s=s[6:]
    elif(s[:5] in l):
        s=s[5:]
    else:
        f=1
if(len(s)==0):
    print("YES")
else:
    print("NO")

Solution (Python):
def main(S):
  while len(S) > 0:
    if S[-1] == "m":
      if S[-5:] == "dream":
        S = S[:-5]
      else:
        print("NO")
        return 0
    elif S[-1] == "e":
      if S[-5:] == "erase":
        S = S[:-5]
      else:
        print("NO")
        return 0
    elif S[-1] == "r":
      if S[-6:] == "eraser":
        S = S[:-6]
      elif S[-7:] == "dreamer":
        S = S[:-7]
      else:
        print("NO")
        return 0
    else:
      print("NO")
      return 0
  else:
    print("YES")
    
main(input())

Solution (Python):
S = input()
S = S[::-1]
candidacies = ["dream", "dreamer", "erase", "eraser"]
candidacies = [c[::-1] for c in candidacies]

fi_flg = False
while fi_flg is False:
  if S[:7] in candidacies:
    S = S[7:]
  elif S[:6] in candidacies:
    S = S[6:]
  elif S[:5] in candidacies:
    S = S[5:]
  else:
    fi_flg = True

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

Solution (Python):
S = input()
strs = list(reversed(S))
rev_S = ''.join(strs)
while True:
  if rev_S[:6] == 'resare':
    rev_S = rev_S[6:]
    continue
  elif rev_S[:7] == 'remaerd':
    rev_S = rev_S[7:]
    continue
  elif rev_S[:5] == 'esare' or rev_S[:5] == 'maerd':
    rev_S = rev_S[5:]
    continue
  else:
    break

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