Problem:
 Score : 300 points    Problem Statement You are given a string S consisting of lowercase English letters.
Another string T is initially empty.
Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:   Append one of the following at the end of T : dream , dreamer , erase and eraser .       Constraints  1≦|S|≦10^5  S consists of lowercase English letters.         Input The input is given from Standard Input in the following format:  S       Output If it is possible to obtain S = T , print YES . Otherwise, print NO .        Sample Input 1 erasedream      Sample Output 1 YES  Append erase and dream at the end of T in this order, to obtain S = T .       Sample Input 2 dreameraser      Sample Output 2 YES  Append dream and eraser at the end of T in this order, to obtain S = T .       Sample Input 3 dreamerer      Sample Output 3 NO

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

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

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

Solution (Python):
S = input ()
Z = ''.join(list(reversed(S)))
Z = Z.replace('resare', '')
Z = Z.replace('esare', '')
Z = Z.replace('remaerd', '')
Z = Z.replace('maerd', '')
z = len (Z)
if z == 0:
    print ('YES')
else:
    print ('NO')

Solution (Python):
s = input()
 
kouho = ["dream", "dreamer", "erase", "eraser"]
 
s = s.replace(kouho[3], "").replace(kouho[2], "").replace(kouho[1], "").replace(kouho[0], "")
 
if s == "":
    print("YES")
else:
    print("NO")

Solution (Python):
from collections import deque


S=input()

lenS=len(S)
T=deque()
for i in range(lenS):
    T.append(S[i])



A=len(T)

while A>7:
    if T[A-1]== "m" and T[A-2] =="a" and T[A-3] =="e" and T[A-4] =="r" and T[A-5] =="d":
        for i in range(5):
            T.pop()
    elif T[A-1]== "e" and T[A-2] =="s" and T[A-3] =="a" and T[A-4] =="r" and T[A-5] =="e":
        for i in range(5):
            T.pop()
            
    elif T[A-1]== "r" and T[A-2] =="e" and T[A-3] =="s" and T[A-4] =="a" and T[A-5] =="r" and T[A-6] =="e":
        for i in range(6):
            T.pop()
            
    elif T[A-1]== "r" and T[A-2] =="e" and T[A-3] =="m" and T[A-4] =="a" and T[A-5] =="e" and T[A-6] =="r" and T[A-7] =="d":
        for i in range(7):
            T.pop()
    else:
        print("NO")
        exit()
    
    A=len(T)

if A<=4:
    print("NO")
elif A==5 and T[A-1]== "m" and T[A-2] =="a" and T[A-3] =="e" and T[A-4] =="r" and T[A-5] =="d":
    print("YES")
elif A==5 and T[A-1]== "e" and T[A-2] =="s" and T[A-3] =="a" and T[A-4] =="r" and T[A-5] =="e":
    print("YES")
elif A==6 and  T[A-1]== "r" and T[A-2] =="e" and T[A-3] =="s" and T[A-4] =="a" and T[A-5] =="r" and T[A-6] =="e":
    print("YES")
elif A==7 and T[A-1]== "r" and T[A-2] =="e" and T[A-3] =="m" and T[A-4] =="a" and T[A-5] =="e" and T[A-6] =="r" and T[A-7] =="d":
    print("YES")
else:
    print("NO")

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

Solution (Python):
S = input()
'''
dp[i]はSのi文字目（1~Sの長さ)までを表せるかどうか
'''
dp = [0] * (len(S) + 1)
dp[0] = 1
check = "NO"

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

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:
        check = "YES"
        break

print(check)

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


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


solve(s)

Solution (Python):
import sys
import numpy as np
import random
from decimal import Decimal
import itertools
import re
import bisect
from collections import deque, Counter
from functools import lru_cache

sys.setrecursionlimit(10**9)
INF = 10**13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
def SERIES(n): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ')
def GRID(h,w): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ').reshape(h,-1)[:,:w]
def GRIDfromString(h,w): return np.frombuffer(sys.stdin.buffer.read(), 'S1').reshape(h,-1)[:,:w]
MOD = 1000000007

def main():
	s = S().rstrip()
	while len(s) > 0:
		if 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:
			print('NO')
			break
	else:
		print('YES')

if __name__ == '__main__':
    main()

Solution (Python):
S = input()

S = S[::-1]

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

ind = 0
f = True

while True:
    if ind == len(S):
        f = True
        break
    if S[ind:ind+5] in check:
        ind += 5
    elif S[ind:ind+6] in check:
        ind += 6
    elif S[ind:ind+7] in check:
        ind += 7
    else:
        f = False
        break

if f:
    print('YES')
else:
    print("NO")

Solution (Python):





import math
import fractions
import sys
import bisect
import heapq  
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr



def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod



def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a


inf = 10 ** 18
mod = 10 ** 9 + 7

s = ''.join(reversed(list(input())))
n = len(s)
ind = 0
flag = True
while ind+4 < n:
    if ind+5 < n and s[ind:ind+6] == 'resare':
        ind+=6
    elif ind+6 < n and s[ind:ind+7] == 'remaerd':
        ind+=7
    elif s[ind:ind+5] == 'maerd' or s[ind:ind+5] == 'esare':
        ind+=5
    else:
        flag=False
        break
if ind == n and flag:
    print('YES')
else:
    print('NO')

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 = input()[::-1]
d = {'maerd', 'remaerd', 'esare', 'resare'}

cnt = 0
while cnt + 5 <= len(s):
    if s[cnt:cnt + 5] in d:
        cnt = cnt + 5
    elif cnt + 6 <= len(s) and s[cnt:cnt + 6] in d:
        cnt = cnt + 6
    elif cnt + 7 <= len(s) and s[cnt:cnt + 7] in d:
        cnt = cnt + 7
    else:
        print('NO')
        break
else:
    print('YES')

Solution (Python):
s = input()[::-1]
d = {'maerd', 'remaerd', 'esare', 'resare'}

cnt = 0
while cnt < len(s):
    if s[cnt:cnt + 5] in d:
        cnt += 5
    elif s[cnt:cnt + 6] in d:
        cnt += 6
    elif s[cnt:cnt + 7] in d:
        cnt += 7
    else:
        print('NO')
        break
else:
    print('YES')

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

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

Solution (Python):
S = input()[::-1]
ss = ['maerd', 'remaerd', 'esare', 'resare']

while True:
    for s in ss:
        if S.startswith(s):
            S = S[len(s):]
            break
    else:
        if S != '':
            print('NO')
            break

    if S == '':
        print('YES')
        break

Solution (Python):
import sys

s = input()
data = {"dream","dreamer","erase","eraser"}

while True:
    for i in data:
        if s.endswith(i):
            s = s[:-len(i)]
            break
    else:break

if s == "":
    print("YES")
    sys.exit()
else:
    print("NO")
    sys.exit()

Solution (Python):
S = list(input())
while True:
  if len(S) >= 8:
    if S[:8] == ["d", "r", "e", "a", "m", "e", "r", "a"]:
      del S[:5]
    elif S[:7] == ["d", "r", "e", "a", "m", "e", "r"]:
      del S[:7]
    elif S[:6] == ["e", "r", "a", "s", "e", "r"]:
      del S[:6]
    elif S[:5] == ["d", "r", "e", "a", "m"]:
      del S[:5]
    elif S[:5] == ["e", "r", "a", "s", "e"]:
      del S[:5]
    else:
      print("NO")
      break
  elif len(S) == 7:
    if S[:7] == ["d", "r", "e", "a", "m", "e", "r"]:
      del S[:7]
  elif len(S) == 6:
    if S[:6] == ["e", "r", "a", "s", "e", "r"]:
      del S[:6]
  elif len(S) == 5:
    if S[:5] == ["d", "r", "e", "a", "m"]:
      del S[:5]
    elif S[:5] == ["e", "r", "a", "s", "e"]:
      del S[:5]
  elif len(S) == 0:
    print("YES")
    break
  else:
    print("NO")
    break

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

a = [x[::-1] for x in a]

s = input()

s = s[::-1]
n = len(s)
i = 0
while i < n:
    if s[i] == 'm':
        if s[i:i+5] == 'maerd':
            i += 5
        else:
            print('NO')
            exit()
    elif s[i] == 'e':
        if s[i:i+5] == 'esare':
            i += 5
        else:
            print('NO')
            exit()
    elif s[i] == 'r':
        if s[i+2] == 'm':
            if s[i:i+7] == 'remaerd':
                i += 7
            else:
                print('NO')
                exit()
        elif s[i+2] == 's':
            if s[i:i+6] == "resare":
                i += 6
            else:
                print('NO')
                exit()
        else:
            print('NO')
            exit()
    else:
        print('NO')
        exit()
print('YES')

Solution (Python):
import sys
sys.setrecursionlimit(10**7) 
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right

from collections import deque, defaultdict


from collections import Counter

from itertools import accumulate,combinations,permutations

from heapq import heapify,heappop,heappush


from functools import reduce,lru_cache

from decimal import Decimal

def input(): 
    x=sys.stdin.readline()
    return x[:-1] if x[-1]=="\n" else x
def printe(*x):print("
def printl(li): _=print(*li, sep="\n") if li else None
def argsort(s, return_sorted=False): 
    inds=sorted(range(len(s)), key=lambda k: s[k])
    if return_sorted: return inds, [s[i] for i in inds]
    return inds
def alp2num(c,cap=False): return ord(c)-97 if not cap else ord(c)-65
def num2alp(i,cap=False): return chr(i+97) if not cap else chr(i+65)
def matmat(A,B):
    K,N,M=len(B),len(A),len(B[0])
    return [[sum([(A[i][k]*B[k][j]) for k in range(K)]) for j in range(M)] for i in range(N)]
def matvec(M,v):
    N,size=len(v),len(M)
    return [sum([M[i][j]*v[j] for j in range(N)]) for i in range(size)]
def T(M):
    n,m=len(M),len(M[0])
    return [[M[j][i] for j in range(n)] for i in range(m)]
def main():
    mod = 1000000007
    

    
    
    
    
    
    s=input()
    dp=[0]*(len(s)+1)
    dp[0]=1
    for i in range(5,len(s)+1):
        f=0
        if s[i-5:i]=="erase":
            dp[i]=dp[i]|dp[i-5]
        if s[i-5:i]=="dream":
            dp[i]=dp[i]|dp[i-5]
        if i>=6 and s[i-6:i]=="eraser":
            dp[i]=dp[i]|dp[i-6]
        if i>=7 and s[i-7:i]=="dreamer":
            dp[i]=dp[i]|dp[i-7]

    if dp[-1]:
        print('YES')
    else:print('NO')





if __name__ == "__main__":
    main()

Solution (Python):






cs = ["dream", "dreamer", "erase", "eraser"]
S = input().strip()

while True:

    changed = False

    for c in cs:
        if S.endswith(c):
            S = S[:-len(c)]
            changed = True
            break
    
    if len(S) == 0:
        print("YES")
        break

    if not changed:
        print("NO")
        break

Solution (Python):
import sys
cin = lambda: sys.stdin.readline().strip()
cout = sys.stdout.write

s = cin()

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

if(s == ''):
	cout(f"YES"+'\n')
else:
	cout(f"NO"+'\n')

Solution (Python):
import sys
from collections import deque
s = input()

queue = deque()
queue.append(0)
candidates = ['dream', 'dreamer', 'erase', 'eraser']

index = 0
while queue:
    index = queue.popleft()
    match = False
    for candidate in candidates:
        if s[index:index + len(candidate)] == candidate:
            match = True
            if index + len(candidate) == len(s):
                print('YES')
                sys.exit(0)
            queue.append(index + len(candidate))
print('NO')

Solution (Python):
import re

s = input()
flag = re.match('^(dream|dreamer|erase|eraser)+$',s)

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

Solution (Python):
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


s = S()

s = s[::-1]
t = ''
ans = 'YES'
i = 0
while True:
    
    
    if s[i: i + 7] == 'dreamer'[::-1]:
        i += 7
    elif s[i: i + 6] == 'eraser'[::-1]:
        i += 6
    elif s[i: i + 5] == 'dream'[::-1]:
        i += 5
    elif s[i: i + 5] == 'erase'[::-1]:
        i += 5
    elif i >= len(s):
        break
    else:
        ans = 'NO'
        break
print(ans)

Solution (Python):
S = str(input())

T = len(S)
K = T//5+1

for i in range (0, K+1):
	T = len(S)
	if S[-5::] == 'dream':
		S = S[0:T-5]
	elif S[-5::] == 'erase':
		S = S[0:T-5]
	elif S[-7::] == 'dreamer':
		S = S[0:T-7]
	elif S[-6::] == 'eraser':
		S = S[0:T-6]

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

Solution (Python):
S = input()

flag = 0

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

while len(S) >= 5:

    for i in words:
        N = len(i)
        if S[-N:] in words:
            n = len(S) - N
            S = S[:n]
            flag = 1
            break
        else:
            flag = 0
            continue


    if flag == 0:
        break



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

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(map(int,input().split()))
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
INF = 10**9
mod = 10**9+7

S = list(s())
S.reverse()
for i in range(10**5):
    if not S:
        break

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

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    s = input()
    words = ["dream", "dreamer", "erase", "eraser"]

    while s:
        for word in words:
            if len(s) >= len(word):
                if s[-len(word):] == word:
                    s = s[:-len(word)]
                    break
        else:
            print("NO")
            sys.exit()

    print("YES")

if __name__ == '__main__':
    main()

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

s = s[::-1]
ans = ""
for i in range(4): div[i] = div[i][::-1]

for i in range(sl):
    ans += s[i]
    if ans in div:
        ans = ""

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

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

dreams = ['dream', 'dreamer', 'erase', 'eraser']
dreams = [dream[::-1] for dream in dreams]

i = 0
while i < len(S):
    flag = False
    for j in dreams:
        if S[i:i+len(j)] == j:
            i = i + len(j)
            flag = True
    if not flag:
        break

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

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    s = input()
    s = s[::-1]
    flag = True

    i = 0
    while i < len(s):
        if s[i:i + 5] == 'maerd' or s[i:i + 5] == 'esare':
            i += 5
        elif s[i:i + 6] == 'resare':
            i += 6
        elif s[i:i + 7] == 'remaerd':
            i += 7
        else:
            flag = False
            break
    print('YES' if flag else 'NO')

if __name__ == '__main__':
    main()

Solution (Python):
s = input()[::-1]
l = [ 'dream', 'dreamer', 'erase', 'eraser']
for i in range(4):
	s1 = l[i][::-1]
	l[i] = s1

n = len(s)
i = 0
t = tuple(l)

while i < n:
	judge = 0
	s_l = []
	for j in (5,6,7):
		if n-i >= j:
			s1 = s[i:i+j]
			if s1 in t:
				i += j
				judge = 1
	
	if judge == 0:
		break

if i == n:
	print('YES')
else:
	print('NO')

Solution (Python):
print('YNEOS'[input().replace('eraser','').replace('erase','').replace('dreamer','').replace('dream','')>''::2])

Solution (Python):
print('YNEOS'[input().replace('eraser','').replace('erase','').replace('dreamer','').replace('dream','')!=''::2])

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

Solution (Python):
S = input()


S = S[::-1]

while 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:
        print("NO")
        exit()

print("YES")

Solution (Python):
import sys
S = input()
S = S[::-1]
while 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:
        print('NO')
        exit()
print('YES')

Solution (Python):
a = input()

b = a[::-1]

i = 0
while i  < len(b):
    if b[i:i+5] == "esare":
        i += 5
    elif b[i:i+5] == "maerd":
        i += 5
    elif b[i:i+6] == "resare":
        i += 6
    elif b[i:i+7] == "remaerd":
        i += 7
    else:
        ans = "NO"
        break

if i  == len(b):
    ans = "YES"
else:
    ans = "NO"

print(ans)

Solution (Python):
S = input()

import re

if re.fullmatch(r'(dream|dreamer|erase|eraser)*', S):
    print('YES')
else:
    print('NO')

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

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

Solution (Python):
s=input()

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

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

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

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

Solution (Python):
S = input()
tmp = ''.join(list(reversed(S)))
s = 0
ans = 'YES'

for i in range(len(S)):
    if tmp[s:i] == 'maerd' or tmp[s:i] == 'remaerd'or tmp[s:i] == 'esare' or tmp[s:i] == 'resare':
        s = i
        ans = 'YES'
        continue
    
    if len(tmp[s:i]) > 7:
        ans = 'NO'
        break
        
print(ans)

Solution (Python):
str = input()
words = ["dream", "dreamer", "erase", "eraser"]

def replace(S, s):
    reversedS = S[::-1]
    reverseds = s[::-1]
    replaced = reversedS.replace(reverseds, "", 1)
    return replaced[::-1]

while True:
    tmp = str
    for word in words:
        if str.endswith(word):
            str = replace(str, word)
            break
    if tmp == str:
        print("NO")
        break
    if str == "":
        print("YES")
        break
    tmp = str

Solution (Python):


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    '''
    dreamer
      dream
     eraser
      erase
    '''
    strings = ['dreamer', 'dream', 'eraser', 'erase']
    for i in range(len(strings)):
        strings[i] = ''.join(reversed(strings[i]))
    S = input().strip()
    S = ''.join(reversed(S))
    i = 0
    while i < len(S):
        found_string = None
        for string in strings:
            if S[i:].startswith(string):
                found_string = string
                break
        if found_string is None:
            return 'NO'
        i += len(found_string)
    return 'YES'


if __name__ == '__main__':
    print(solve())

Solution (Python):
s = input()

flag = 0

while(len(s)>0):
  if (s[-6:] == 'eraser'):
    s = s[:-6]
  elif (s[-5:] == 'erase'):
    s = s[:-5]
  elif (s[-7:] == 'dreamer'):
    s = s[:-7]
  elif (s[-5:] == 'dream'):
    s = s[:-5]
  else:
    flag = 1
    break

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

Solution (Python):
from sys import stdin
data = stdin.readline().rstrip()

def shorten_phrase(x):
    l = len(x)
    if x.startswith("eraser"):
        x = x[6:]
    elif x.startswith("erase"):
        x = x[5:]
    elif x.startswith("dreamera"):
        x = x[5:]
    elif x.startswith("dreamer"):
        x = x[7:]
    elif x.startswith("dream"):
        x = x[5:]
    m = len(x)
    return l != m

while shorten_phrase(data):
    if data.startswith("eraser"):
        data = data[6:]
    elif data.startswith("erase"):
        data = data[5:]
    elif data.startswith("dreamera"):
        data = data[5:]
    elif data.startswith("dreamer"):
        data = data[7:]
    elif data.startswith("dream"):
        data = data[5:]
if len(data) == 0:
    print("YES")
else:
    print("NO")

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

Solution (Python):
s = input()[::-1]
n = len(s)
words = ['maerd', 'remaerd', 'esare', 'resare']
while True:
    if s[:5] == words[0]:
        s = s[5:]
        continue
    elif s[:7] == words[1]:
        s = s[7:]
        continue
    elif s[:5] == words[2]:
        s = s[5:]
        continue
    elif s[:6] == words[3]:
        s = s[6:]
        continue
    break
print('NO' if s else 'YES')

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

le = len(s)

while le>=5:
    fiv = s[-5:]
    six = s[-6:]
    sev = s[-7:]

    if fiv=="dream" or fiv=="erase":
        s = s[:-5]
    elif six=="eraser":
        s = s[:-6]
    elif sev=="dreamer":
        s = s[:-7]
    else:
        print("NO")
        exit()
    
    le = len(s)

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

Solution (Python):
S = input()[::-1]
word = ['dream'[::-1], 'dreamer'[::-1], 'erase'[::-1], 'eraser'[::-1]]
judge = [True, True, True, True]
count = 0
while any(judge):
  for i in range(len(word)):
    if S[:len(word[i])] == word[i]:
      S = S[len(word[i]):]
    else:
      count += 1
    if S == '':
      print('YES')
      exit()
    if count == 4:
      print('NO')
      exit()
  count = 0

Solution (Python):
S=input()[::-1]
num=(len(S)//5)+1
while num > 0:
  for i in ['dream'[::-1], 'dreamer'[::-1], 'erase'[::-1], 'eraser'[::-1]]:
    if S[:len(i)] == i:
      S = S[len(i):]
    if S == '':
      print('YES')
      exit()
  num -= 1
print('NO')

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

n = len(s)
k = 0

flg = True
while k < n:
    if s[k : min(k + 7, n)] == 'remaerd':
        k += 7
    elif s[k : min(k + 6, n)] == 'resare':
        k += 6
    elif s[k : min(k + 5, n)] == 'maerd' or s[k : min(k + 5, n)] == 'esare':
        k += 5
    else:
        flg = False
        break

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

Solution (Python):
import sys

S = input()

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

while len(S) > 0:
    if len(S) < 5:
        print('NO')
        sys.exit(0)
    no_match = True
    for word in words:
        if S[-3] == word[-3]:
            if len(S) < len(word) or S[-len(word):] != word:
                print('NO')
                sys.exit(0)
            S = S[:-len(word)]
            no_match = False
            break
    if no_match:
        print('NO')
        sys.exit(0)

print('YES')

Solution (Python):
import sys

S = input()

while len(S) > 0:
    if len(S) < 5:
        print('NO')
        sys.exit(0)
    third = S[-3]
    if third == 'e':
        if len(S) < 5 or S[-5:] != 'dream':
            print('NO')
            sys.exit(0)
        S = S[:-5]
        continue
    if third == 'a':
        if len(S) < 5 or S[-5:] != 'erase':
            print('NO')
            sys.exit(0)
        S = S[:-5]
        continue
    if third == 'm':
        if len(S) < 7 or S[-7:] != 'dreamer':
            print('NO')
            sys.exit(0)
        S = S[:-7]
        continue
    if third == 's':
        if len(S) < 6 or S[-6:] != 'eraser':
            print('NO')
            sys.exit(0)
        S = S[:-6]
        continue
    else:
        print('NO')
        sys.exit(0)

print('YES')

Solution (Python):
import sys
input = sys.stdin.readline




'''
a=[]
b=[]
for i in range():
    A, B = map(int, input().split())
    a.append(A)
    b.append(B)'''

s=input().rstrip("\n")
e=len(s)
l=["dream","dreamer","erase","eraser"]
for i in range(len(s))[::-1]:
    
    
    if s[i:e] in l:
        e=i
        s=s[:i]  
        continue
    if len(s[i:e])>7:
        print("NO")
        sys.exit()
if len(s)>0 and s not in l:
    print("NO")
else:
    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):
import re
print("YES" if re.fullmatch("(erase(r)?|dream(er)?)+", input())else "NO")

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

Solution (Python):
def resolve():
    S = input()
    while len(S) >= 5:
        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:
            break
    print("YES" if len(S) == 0 else "NO")
resolve()

Solution (Python):
can = ["dream", "dreamer", "erase", "eraser"]
ss = input().rstrip('\n')

pos = len(ss)
while True:
    found = False
    for cc in can:
        lenz = len(cc)
        if (pos-lenz >= 0) and (ss[pos-lenz:pos] == cc):
            pos = pos - lenz
            found = True
            break
    if (not found) or (pos <= 0): break

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

Solution (Python):
s = list(input())
index = 0
while index < len(s):
    if "".join(s[index:index+7]) == "dreamer":
        index += 7
    elif "".join(s[index:index+6]) == "eraser":
        index += 6
    elif "".join(s[index:index+5]) == "erase" or "".join(s[index:index+5]) == "dream":
        index += 5
    elif "".join(s[index:index+4]) == "aser":
        index += 4
    elif "".join(s[index:index+3]) == "ase":
        index += 3
    else:
        print("NO")
        exit()
print("YES")

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

s = s.replace("eraser"[::-1], "")
s = s.replace("erase"[::-1], "")
s = s.replace("dreamer"[::-1], "")
s = s.replace("dream"[::-1], "")

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

Solution (Python):
S = input()
S = S[::-1]
s1 = 'maerd'
s2 = 'esare'
s3 = 'remaerd'
s4 = 'resare'

while(1):
	if S[:5] == s1:
		S = S[5:]
	elif S[:5] == s2:
		S = S[5:]
	elif S[:7] == s3:
		S = S[7:]
	elif S[:6] == s4:
		S = S[6:]
	else:
		break

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

Solution (Python):
import re
if re.fullmatch(r'(dream|dreamer|erase|eraser)*', input().strip()):
    print('YES')
else:
    print('NO')

Solution (Python):
s = input()
n = len(s)
while 0 < n:
    if s[n-5:n] == 'dream' or s[n-5:n] == 'erase':
        n -= 5
        continue
    elif s[n-6:n] == 'eraser':
        n -= 6
        continue
    elif s[n-7:n] == 'dreamer':
        n -= 7
        continue
    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=input()
s=s[::-1]

a=0
while a<len(s):
    if s[a:a+5]=="esare" or s[a:a+5]=="maerd":
        a+=5
    elif s[a:a+6]=="resare":
        a+=6
    elif s[a:a+7]=="remaerd":
        a+=7
    else:
        print("NO")
        exit()

print("YES")

Solution (Python):
from collections import deque

s = input()
q = deque(list(s))

while q:
  if len(q) >= 5:
    subs = q.popleft()+q.popleft()+q.popleft()+q.popleft()+q.popleft()
    if subs != 'erase' and subs != 'dream':
      print('NO')
      exit()
    if subs == 'erase' and len(q) != 0 and q[0] == 'r':
      q.popleft()
    if subs == 'dream' and len(q) == 2 and q[0]+q[1] == 'er':
      q.popleft()
      q.popleft()
    if subs == 'dream' and len(q) >= 3 and q[0]+q[1] == 'er' and q[2] != 'a':
      q.popleft()
      q.popleft()
  else:
   
    print('NO')
    exit()
    
print('YES')

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

Solution (Python):
S=input()
check=["dream","dreamer","erase","eraser"]
S=S[::-1]
for i in range(len(check)):
  check[i]=check[i][::-1]
nagasa=len(S)
cur=0
while cur<nagasa:
  flag=False
  for x in check:
    find_pos=S.find(x,cur)
    if cur==find_pos:
      flag=True
      cur=find_pos+len(x)
      break
  if flag==False:
    print("NO")
    exit()
print("YES")

Solution (Python):
import re
s = list(input())
s.reverse()

s = "".join(s)
s = re.split('maerd|remaerd|esare|resare', s)
if len(s) == s.count(""):
    print("YES")
else:
    print("NO")

Solution (Python):
S = list(input())
import re
S.reverse()
S = "".join(S)
S = re.split('maerd|remaerd|esare|resare', S)
if len(S) == S.count("") :
  print("YES")
else:
  print("NO")