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):
from sys import stdin
import sys

S = stdin.readline().rstrip()
T = ''

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

if len(S) < len('dream'):
    print('NO')
    sys.exit()

pos = 0

OTHER=0
DREAM=1
ERASE=2
last_word = OTHER

while pos < len(S):
    if S[pos:pos+len(data[0])] == data[0]:
        pos = pos + len(data[0])
        last_word = DREAM
    elif S[pos:pos+len(data[1])] == data[1]:
        pos = pos + len(data[1])
        last_word = ERASE
    elif S[pos:pos+len(data[2])] == data[2]:
        pos = pos + len(data[2])
        last_word = OTHER
    elif S[pos:pos+len(data[3])] == data[3]:
        pos = pos + len(data[3])
        last_word = OTHER
    elif last_word == DREAM and S[pos:pos+len('er')] == 'er':
        pos = pos + len('er')
        last_word = OTHER
    elif last_word == ERASE and S[pos:pos+len('r')] == 'r':
        pos = pos + len('r')
        last_word = OTHER
    else:
        print('NO')
        sys.exit()


print('YES')

Solution (Python):
from sys import stdin
import sys

S = stdin.readline().rstrip()
T = ''

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

if len(S) < len('dream'):
    print('NO')
    sys.exit()

pos = 0
flag_dream = False
flag_erase = False

while pos < len(S):
    if S[pos:pos+len(data[0])] == data[0]:
        pos = pos + len(data[0])
        flag_dream = True
        flag_erase = False
    elif S[pos:pos+len(data[1])] == data[1]:
        pos = pos + len(data[1])
        flag_erase = True
        flag_dream = False
    elif S[pos:pos+len(data[2])] == data[2]:
        pos = pos + len(data[2])
        flag_dream = False
        flag_erase = False
    elif S[pos:pos+len(data[3])] == data[3]:
        pos = pos + len(data[3])
        flag_dream = False
        flag_erase = False
    elif flag_dream and S[pos:pos+len('er')] == 'er':
        pos = pos + len('er')
        flag_dream = False
        flag_erase = False
    elif flag_erase and S[pos:pos+len('r')] == 'r':
        pos = pos + len('r')
        flag_dream = False
        flag_erase = False
    else:
        print('NO')
        sys.exit()


print('YES')

Solution (Python):
A = ['maerd', 'remaerd', 'esare', 'resare']
S = input()[::-1]
cur = 0
N = len(S)
ans = 'YES'
while cur < N:
    if S[cur: cur + 7] in A:
        cur += 7
    elif S[cur: cur + 6] in A:
        cur += 6
    elif S[cur: cur + 5] in A:
        cur += 5
    else:
        ans = 'NO'
        break

print(ans)

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

Solution (Python):
S = input()
dp = [0] * (len(S) + 1)
dp[0] = 1

words = ["dream", "dreamer", "erase", "eraser"]
done = 'NO'

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:
        done = 'YES'

print(done)

Solution (Python):
S = input()
dp = [0] * (len(S) + 1)
dp[0] = 1

words = ["dream", "dreamer", "erase", "eraser"]
done = 'NO'

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:
        done = 'YES'
        break

print(done)

Solution (Python):

def judge(S):
    S = "zzz" + S
    update = True
    while update:
        update = False
        if S[len(S)-7:] == "dreamer":
            S = S[:len(S)-7]
            update = True
        elif S[len(S)-6:] == "eraser":
            S = S[:len(S)-6]
            update = True
        elif S[len(S)-5:] == "dream" or S[len(S)-5:] == "erase":
            S = S[:len(S)-5]
            update = True
    if len(S) == 3:
        return True
    else:
        return False

S = input()

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

Solution (Python):
S =input()
N =len(S)
i=0
while i>=0:
    if len(S)>=i+10 and S[i:i+10]=='dreamerase':
        i=i+5
    elif len(S)>=i+11 and S[i:i+11]=='dreameraser':
        i=i+5
    elif len(S)>=i+10 and S[i:i+10]=='eraseerase':
        i=i+5
    elif len(S)>=i+7 and S[i:i+7]=='dreamer':
        i=i+7
        if i==N:
            print('YES')
            break
    elif len(S)>=i+6 and S[i:i+6]=='eraser':
        i=i+6
        if i==N:
            print('YES')
            break
    elif len(S)>=i+5 and S[i:i+5]=='dream':
        i=i+5
        if i==N:
            print('YES')
            break
    elif len(S)>=i+5 and S[i:i+5]=='erase':
        i=i+5
        if i==N:
            print('YES')
            break
    else:
        print('NO')
        break

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

Solution (Python):
S = input()

Sr = ""

for i in range(len(S)):
    Sr += S[-i-1]

while len(Sr) >= 7:
    if Sr[0:5] == "maerd" :
        Sr = Sr[5:]
    elif Sr[0:5] == "esare":
        Sr = Sr[5:]
    elif Sr[0:6] == "resare":
        Sr = Sr[6:]
    elif Sr[0:7] == "remaerd":
        if len(Sr) != 7:
            Sr = Sr[7:]
        else:
            print("YES")
            break
    else:
        print("NO")
        break
else:
    if len(Sr) == 6:
        if Sr[0:6] == "resare":
            print("YES")
        else:
            print("NO")
    elif len(Sr) == 5:
        if Sr[0:5] == "maerd" :
            print("YES")
        elif Sr[0:5] == "esare":
            print("YES")
        else:
            print("NO")
    elif len(Sr) == 0:
        print("YES")
    else:
        print("NO")

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

Solution (Python):
S=input()
while len(S) >=5:
	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:
		print('NO')
		break
if len(S)==0:
	print('YES')
if 1<=len(S)<=4:
	print('NO')

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

Solution (Python):
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()

Solution (Python):
def main():
    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")

main()

Solution (Python):
import re
S = input()
'''
メタ文字：
^ = 文字列の先頭、
| = OR
+ = 直前のパターンを1回以上繰り返し、
$ = 文字列の末尾
'''
if re.match("^(dream|dreamer|erase|eraser)+$", S):
    print('YES')
else:
    print('NO')

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

Solution (Python):




import re


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


if __name__ == "__main__":
    main()

Solution (Python):
s = input()
flag = "NO"
s = s[::-1]
for i in range(len(s)//5+1):
    if s[0:5] == "maerd":
        s = s[5:]
    if s[0:7] == "remaerd":
        s = s[7:]
    if s[0:5] == "esare":
        s = s[5:]
    if s[0:6] == "resare":
        s = s[6:]
            
    if s == '':
        flag = "YES"
        
print(flag)

Solution (Python):
s = input()[::-1]  
template = list('dream dreamer erase eraser'.split())
template = [c[::-1] for c in template]  

while s:
    for c in template:
        if s.startswith(c):
            s = s[len(c):]
            break
    else:
        break
print("YES") if s == "" else print("NO")

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

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

Solution (Python):
s = input()
idx = len(s)
ans = True
while idx > 0:
    if idx >= 7:
        if s[idx-7:idx] == "dreamer":
            idx -= 7
            continue
        elif idx == 7:
            ans = False
            break
    if idx >= 6:
        if s[idx-6:idx] == "eraser":
            idx -= 6
            continue
        elif idx == 6:
            ans = False
            break
    if idx >= 5:
        if s[idx-5:idx] in ["erase", "dream"]:
            idx -= 5
            continue
        elif idx == 5:
            ans = False
            break
    ans = False
    break
if ans: 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 s == '':
    print('YES')
else:
    print('NO')

Solution (Python):
import sys
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:
    print("NO")
    sys.exit()
print("YES")

Solution (Python):
s = input()
while 1:
    flag = False
    if s[-5:] in ['dream', 'erase']:
        s = s[:-5]
        flag = True
    elif s[-6:] == 'eraser':
        s = s[:-6]
        flag = True
    elif s[-7:] == 'dreamer':
        s = s[:-7]
        flag = True
    if not flag:
        break
if not s:
    print('YES')
else:
    print('NO')

Solution (Python):
def solve(S):
        index = 0
        while index < len(S):
            if S[index] != "d" and S[index] != "e":
                print("NO")
                return
            if S[index] == "d":
                if index + 6 < len(S) and S[index:index+7] == "dreamer" and (index + 7 == len(S) or (S[index + 7] == "d" or S[index + 7] == "e")):
                    index += 7
                elif index + 4 < len(S) and S[index:index + 5] == "dream":
                    index += 5
                else:
                    print("NO")
                    return
            elif S[index] == "e":
                if index + 5 < len(S) and S[index:index + 6] == "eraser":
                    index += 6
                elif index + 4 < len(S) and S[index:index + 5] == "erase":
                    index += 5
                else:
                    print("NO")
                    return
        print("YES")



S = input()
solve(S)

Solution (Python):
import re

s = input()
ptn = re.compile(r'maerd|remaerd|resare|esare')
tmp = s[::-1]

while(True):
  res = re.match(ptn , tmp)
  if res is None:
    break
  tmp = tmp.replace(res.group(),'',1)

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

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

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

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

Solution (Python):


S = ''.join(list(reversed(input())))

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

while True:
    flag = 0
    if S[:7] == "remaerd":
        S = S[7:]
        flag = 1
        
    if S[:6] == "resare":
        S = S[6:]
        flag = 1
        
    if S[:5] == "esare" or S[:5] ==  "maerd":
        S = S[5:]
        flag = 1
        
    if flag == 0:
        break
if len(S) == 0:
    print("YES")
else:
    print("NO")

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

Solution (Python):
S = input()

ans = 'YES'

while S:
    if S.endswith('eraser'):
        S = S[:-6]
    elif S.endswith('erase'):
        S = S[:-5]
    elif S.endswith('dreamer'):
        S = S[:-7]
    elif S.endswith('dream'):
        S = S[:-5]
    else:
        ans = 'NO'
        break

print(ans)

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

Solution (Python):
l = list(input())
n = len(l)
cursor = n-1
while True:
    if cursor >= 5 and l[cursor] == "r" and l[cursor-1] == "e" and l[cursor-2] == "s" and l[cursor-3] == "a" and l[cursor-4] == "r" and l[cursor-5] == "e":
        cursor = cursor - 6
        continue
    if cursor >= 4 and l[cursor] == "e" and l[cursor-1] == "s" and l[cursor-2] == "a" and l[cursor-3] == "r" and l[cursor-4] == "e":
        cursor = cursor -5
        continue
    if cursor >= 6 and l[cursor] == "r" and l[cursor-1] == "e" and l[cursor-2] == "m" and l[cursor-3] == "a" and l[cursor-4] == "e" and l[cursor-5] == "r" and l[cursor-6] == "d":
        cursor -= 7
        continue
    if cursor >= 4 and l[cursor] == "m" and l[cursor-1] == "a" and l[cursor-2] == "e" and l[cursor-3] == "r" and l[cursor-4] == "d":
        cursor -= 5
        continue
    if cursor == -1:
        print("YES")
        break
    else:
        print("NO")
        break

Solution (Python):
s = input()
s_r_l = list(reversed(s))
s_r = ''.join(list(s_r_l))

t = str()
d = ['dream','dreamer','erase','eraser']
d_r = []
for i in d:
    i_s_l = list(reversed(i))
    i_s = ''.join(list(i_s_l))
    d_r.append(i_s)

while len(s_r)>0:
    judge = [s_r.startswith(i) for i in d_r]
    if any(judge):
        
        target = d_r[judge.index(True)]
        
        s_r = s_r[len(target):]
        
        if len(s_r) == 0:
            print("YES")
    else:
        print("NO")
        break

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

cand = ["dream", "dreamer", "erase", "eraser"]
cand = [c[::-1] for c in cand]

while S:
    for c in cand:
        if S.startswith(c):
            S = S[len(c):]
            break
    else:
        break

print("YES") if S == "" else print("NO")

Solution (Python):
s = input()

while 1:
    for x in ('dream', 'dreamer', 'erase', 'eraser'):
        if s.endswith(x):
            s = s[:-len(x)]
            break
    else:
        print('NO')
        break
    if not s:
        print('YES')
        break

Solution (Python):
s = input()
s_length = len(s)
reversed_s = "".join(list(reversed(list(s))))

while(True):
  s_length = len(reversed_s)
  if s_length == 0:
    break
  else:
    if reversed_s[:7] == "remaerd":
      reversed_s = reversed_s[7:]
    elif reversed_s[:6] == "resare":
      reversed_s = reversed_s[6:]
    elif reversed_s[:5] == "esare":
      reversed_s = reversed_s[5:]
    elif reversed_s[:5] == "maerd":
      reversed_s = reversed_s[5:]
    else:
      break

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

Solution (Python):

import sys

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h,w): return [[i for j in range(w)] for k in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)

MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**5+10)


S = input()
ls = len(S)
S += "      "
dp = [False for i in range(len(S)+10)]
dp[0]=True
for i in range(len(S)+1):
    if S[i:i+5]=='dream' or S[i:i+5]=='erase':
        dp[i+5]=dp[i]
    if S[i:i+7]=='dreamer':
        dp[i+7]=dp[i]
    if S[i:i+6]=='eraser' :
        dp[i+6]=dp[i]
print('YES' if dp[ls]==True else 'NO')

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

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

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

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

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

Solution (Python):
s=input()[::-1]
t=['maerd','remaerd','esare','resare']
i=0
while i<len(s):
  if s[i:i+5]==t[0] or s[i:i+5]==t[2]:
    i+=5
  elif s[i:i+6]==t[3]:
    i+=6
  elif s[i:i+7]==t[1]:
    i+=7
  else:
    print('NO')
    break
else:
  print('YES')

Solution (Python):
s=input()
while len(s)>7:
    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:
        break
if s in ["dreamer","dream","eraser","erase"]:
    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 s == "":
    print("YES")
else:
    print("NO")

Solution (Python):
s = input()

dic = ['dreameraser', 'dreamerase', 'dreamer', 'dream', 'eraser', 'erase']

def check():
    i = 0
    while i < len(s):
        found = False
        for k in dic:
            if s[i:i+len(k)] == k:
                found = True
                break
        if not found:
            return False
        i += len(k)
    return True


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

Solution (Python):
s = input()

dreamer = "dreamer"
eraser = "eraser"
dream = "dream"
erase = "erase"


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

print("YES")

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

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

Solution (Python):
S = input()

L = ["eraser","erase","dreamer","dream"]
 
while len(S) > 0:
  for i in range(len(L)):
    if L[i] in S:
      S = S.replace(L[i],"")
  else:break
 
if len(S) == 0:
  print("YES")
else:print("NO")

Solution (Python):
def main(S):
    
    words = ["dream",  "dreamer",  "erase",  "eraser"]

    idx = 0
    while idx <= len(S) - 1:
        f = False
        for word in words:
            if idx != 0 and word == S[-len(word) - idx : -idx]:
                idx += len(word)
                f = True
            elif idx == 0 and word == S[-len(word) - idx : ]:
                idx += len(word)
                f = True             

        if not f:
            return False
    return True
        

if __name__ == "__main__":
    S = input()
    if main(S):
        print("YES")
    else:
        print("NO")

Solution (Python):
def main():
    s = input()
    dream = 0
    erase = 1
    work = []
    temp1 = 0
    temp2 = 0
    kind = -1
    append = work.append
    for c in s:
        if c == 'd':
            if kind != -1:
                append([kind, temp2-temp1])
            kind = dream
            temp1 = temp2
        elif c == 's':
            if kind != -1:
                append([kind, temp2-temp1-3])
            kind = erase
            temp1 = temp2-3
        temp2 += 1
    append([kind, len(s)-temp1])
    result = []
    append = result.append
    for i, j in work:
        if i == dream:
            if j <= 5:
                append("dream")
            else:
                append("dreamer")
        else:
            if j <= 5:
                append("erase")
            else:
                append("eraser")
    result = "YES" if "".join(result)==s else "NO"
    print(result)
main()

Solution (Python):

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

for key in keys:
	S = S.replace(key, '')

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

Solution (Python):


S = input()
N = len(S)

A = ["dream", "erase", "eraser", "dreamer"]
dp = [1] + [0]*N
for i in range(5,N+1):
    if i >= 5 and S[i-5:i] == A[0]: dp[i] = dp[i-5]
    if i >= 5 and S[i-5:i] == A[1]: dp[i] = dp[i-5]
    if i >= 6 and S[i-6:i] == A[2]: dp[i] = dp[i-6]
    if i >= 7 and S[i-7:i] == A[3]: dp[i] = dp[i-7]

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

Solution (Python):
S = input()

yes = True
while S != "":
    if S.endswith('dream'):
        S = S[:-5]
    elif S.endswith('dreamer'):
        S = S[:-7]
    elif S.endswith('erase'):
        S = S[:-5]
    elif S.endswith('eraser'):
        S = S[:-6]
    else:
        yes = False
        break
if yes:
    print("YES")
else:
    print("NO")

Solution (Python):
import sys
sys.setrecursionlimit(1000000)

S_list = list(input())

def f(S_list):
  if S_list == []:
    print("YES")
    exit()
  elif "".join(S_list[0:11]) == "dreameraser":
    del S_list[0:11]

    f(S_list)
  elif "".join(S_list[0:10]) == "dreamerase":
    del S_list[0:10]

    f(S_list)
  elif "".join(S_list[0:7]) == "dreamer":
    del S_list[0:7]

    f(S_list)
  elif "".join(S_list[0:6]) == "eraser":
    del S_list[0:6]

    f(S_list)
  elif "".join(S_list[0:5]) == "dream" or "".join(S_list[0:5]) == "erase":
    del S_list[0:5]

    f(S_list)
  else:
    print("NO")
    exit()

f(S_list)

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

Solution (Python):

S_input = input()
S = S_input
for i in range(len(S_input)):
    S = S[:i]+S_input[len(S_input)-1-i]+S[i+1:]


    

end_flg = 1
while end_flg:
    if S.startswith('maerd'):
        S = S.replace('maerd', '', 1)
    elif S.startswith('remaerd'):
        S = S.replace('remaerd','',1)
    elif S.startswith('esare'):
        S = S.replace('esare','',1)
    elif S.startswith('resare'):
        S = S.replace('resare', '', 1)
    else:
        end_flg = 0
if len(S) == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
s = input() 



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

while s:
    for w in words:
        if s[-len(w):] == w:
            s = s[:-len(w)]
            break
    else:
        print('NO')
        break
else:
    print('YES')

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

d = []
for i in b:
  a = a.replace(i, "")

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

Solution (Python):
s = input()


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



solve(s)

Solution (Python):
S = input()
t = 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

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):
import sys

t = set('dream, dreamer, erase, eraser'.split(', '))

def obtainable(s):
  while True:
    for i in range(5, 8):
      if s[-i:] in t:
        s = s[:-i]
        break
    else:
      return False
    if not s:
      return True
    
s = sys.stdin.readline().rstrip()

def main():
  print("YES" if obtainable(s) else "NO")

if __name__ ==  '__main__':
  main()

Solution (Python):
import sys

t = 'eraser, erase, dreamer, dream'.split(', ')
def obtainable(s):
    for x in t:
        s = s.replace(x, '')
    return not s

s = sys.stdin.readline().rstrip()

def main():
    print("YES" if obtainable(s) else "NO")

if __name__ ==  '__main__':
    main()

Solution (Python):
S = input()

def check(S):
  DP = [False]*len(S)
  if S[:5] == 'dream' or S[:5] == 'erase':
    DP[4] = True
  if S[:7] == 'dreamer':
    DP[6] = True
  if S[:6] == 'eraser':
    DP[5] = True
  
  for i in range(7,len(S)):
    DP[i] = (DP[i-5]&((S[i-4:i+1]=='dream')|(S[i-4:i+1]=='erase')))|(DP[i-6]&(S[i-5:i+1]=='eraser'))|(DP[i-7]&(S[i-6:i+1]=='dreamer'))
  return DP[-1]
if check(S):
  print('YES')
else:
  print('NO')

Solution (Python):
S = input()[::-1]
P = ["dream"[::-1], "dreamer"[::-1], "erase"[::-1], "eraser"[::-1]]
i = 0
while i < len(S):
    for p in P:
        if S[i: i + len(p)] == p:
            i += len(p)
            break
    else:
        break
    continue
else:
    print("YES")
    exit()
print("NO")

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

Solution (Python):
S = input()
k=0
t=len(S)
while k<len(S):
    if(S[-(k+5):t-k] == 'dream' or S[-(k+5):t-k] == 'erase')and k+5<len(S)+1:
        k+=5
        continue
    elif(S[-(k+6):t-k] == 'eraser')and k+6<len(S)+1:
        k+=6
        continue
    elif(S[-(k+7):t-k] == 'dreamer')and k+7<len(S)+1:
        k+=7
        continue
    else:
        print('NO')
        break
else:
    print('YES')

Solution (Python):


vocab = set(['dream', 'dreamer', 'erase', 'eraser'])

def parse(s):
    stack = [0]
    while stack:
        i = stack.pop()
        if i == len(s):
            return True
        for w in vocab:
            if s[i:].startswith(w):
                stack.append(i + len(w))
    return False

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

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

Solution (Python):
s = input()[::-1]
a = ""
for i in range(len(s)):
    a = s[i] + a
    if a == "dreamer" or a == "dream" or a == "erase" or a == "eraser":
        a = ""
    
if not a:
    print("YES")
else:
    print("NO")

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    S=readline().strip()
    now=len(S)
    while now>0:
        if S[now-6:now]=="eraser":
            now-=6
        elif S[now-7:now]=="dreamer":
            now-=7
        elif S[now-5:now]=="erase":
            now-=5
        elif S[now-5:now]=="dream":
            now-=5
        else:
            print("NO")
            break
    else:
        print("YES")
        
if __name__=="__main__":
    main()

Solution (Python):
import re

def main():
    S = input()
    pattern = r'^(dream|dreamer|erase|eraser)+$'
    rep = re.compile(pattern)
    result = rep.match(S)
    if result:
        print("YES")
    else:
        print("NO")
    pass

if __name__ == "__main__":
    main()
    pass

Solution (Python):

s = input()

ok = 1
idx = 0
le = len(s)
while idx < le:
    
    if s[idx] == "e":
        if le >= idx+5 and s[idx:idx+5] == "erase":
            if le >= idx+6 and s[idx+5] == "r":
                
                idx = idx+6
            else: 
                idx = idx+5
        else:
            ok = 0
            break
    elif s[idx] == "d":
        if le >= idx+5 and s[idx:idx+5] == "dream":
            if le >= idx+7 and s[idx:idx+7] == "dreamer":
                if le >= idx + 8 and s[idx+7] == "a":
                    idx = idx + 5
                else:
                    idx = idx + 7
            else: 
                idx = idx + 5
        else: 
            ok = 0
            break
    else: 
        ok = 0
        break
if ok == 0:
    print("NO")
else: 
    print("YES")

Solution (Python):
def main():
    s = input().strip()

    while len(s):
        if s[-5:] in ("dream", "erase"):
            s = s[:-5]
        elif s[-6:] == "eraser":
            s = s[:-6]
        elif s[-7:] == "dreamer":
            s = s[:-7]
        else:
            print("NO")
            return

    print("YES")


if __name__ == "__main__":
    main()

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

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