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):
def main():
    S = input()

    editorial(S)


def editorial(S):
    """
    それぞれがそれ以外のprefixにならなければ良い
    """
    l = len(S)
    idx = 0
    ok = True
    rs = S[::-1]
    r7 = "dreamer"[::-1]
    r6 = "eraser"[::-1]
    r5_1 = "dream"[::-1]
    r5_2 = "erase"[::-1]

    while idx < l:
        if rs[idx:idx + 7] == r7:
            idx += 7
        elif rs[idx:idx + 6] == r6:
            idx += 6
        elif rs[idx:idx + 5] == r5_1 or \
                rs[idx:idx + 5] == r5_2:
            idx += 5
        else:
            ok = False
            break

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


def RE(S):
    l = len(S)
    if _dfs(S, l, 0):
        print("YES")
    else:
        print("NO")


def _dfs(S, l, idx, pattern=0):
    
    if idx == l:
        return True

    if S[idx:idx + 7] == "dreamer":
        ok = _dfs(S, l, idx + 7, 1)
        if ok:
            return True
    if S[idx:idx + 6] == "eraser":
        ok = _dfs(S, l, idx + 6, 2)
        if ok:
            return True
    if S[idx:idx + 5] == "dream" or \
            S[idx:idx + 5] == "erase":
        ok = _dfs(S, l, idx + 5, 3)
        if ok:
            return True

    return False


def WA(S):
    l = len(S)
    idx = 0
    ok = True
    while idx < l:
        if S[idx:idx + 7] == "dreamer":
            idx += 7
        elif S[idx:idx + 6] == "eraser":
            idx += 6
        elif S[idx:idx + 5] == "dream" or \
                S[idx:idx + 5] == "erase":
            
            idx += 5
        else:
            ok = False
            break

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

if __name__ == '__main__':
    main()

Solution (Python):
val = input()
q = "dream"
w = "dreamer"
e = "erase"
r = "eraser"

while True:
    if val.count(q+r) != 0:
        val = val.replace(q+r, "")
    elif val.count(q+e) != 0:
        val = val.replace(q+e, "")
    elif val.count(w) != 0:
        val = val.replace(w, "")
    elif val.count(q) != 0:
        val = val.replace(q, "")
    elif val.count(r) != 0:
        val = val.replace(r, "")
    elif val.count(e) != 0:
        val = val.replace(e, "")
    else:
        break
print("YES" if len(val) == 0 else "NO")

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

Solution (Python):
import sys
S=raw_input()
judge=["dream","dreamer","erase","eraser"]

V=set()
V.add(0)

cnt=0
while cnt<len(S):
	tmp=set()
	for i in V:
		for j in judge:
			if S[i:i+7].find(j)==0:
				tmp.add(i+len(j))
				if i+len(j)==len(S):
					print "YES"
					sys.exit()
	V=tmp
	cnt+=1
else:
	print "NO"

Solution (Python):
def solve():
    while 1:
        try:
            s = list(input())
            s.reverse()
            s = "".join(s)
            if len(s) < 5:
                raise EndLoop

            l = 0
            r = 5
            while r <= len(s):
                if s[l:r] == "maerd" or s[l:r] == "esare":
                    l += 5
                    r += 5
                elif s[l:r] == "remae":
                    if s[l:r + 2] == "remaerd":
                        l += 7
                        r += 7
                    else:
                        raise EndLoop
                elif s[l:r] == "resar":
                    if s[l:r + 1] == "resare":
                        l += 6
                        r += 6
                    else:
                        raise EndLoop
                else:
                    raise EndLoop

            print("YES")
            break
        except:
            print("NO")
            break


solve()

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

Solution (Python):
S = input()
S = S[::-1]
W = ['dream', 'dreamer', 'erase', 'eraser']
for n in range(4):
    W[n] = W[n][::-1]
check = 0
i = 0
while i < len(S):
    check = 0
    for w in W:
        if w in S[i:i+len(w)]:
            i += len(w)
            check = 1
    if check is 0:
        break
if check is 1:
    print('YES')
else:
    print('NO')

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

Solution (Python):
import sys




def read_int_list():
    return list(map(int, input().split()))


def read_str_list():
    return input().split()


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


def read_str():
    return input()


def solve(s):
    words = ['dream', 'dreamer', 'erase', 'eraser']
    n = len(s)
    m = n
    while m > 0:
        found = False
        for w in words:
            stop = m
            start = m - len(w)
            if start >= 0:
                if s[start:stop] == w:
                    m = start
                    found = True
                    break
        if not found:
            return False
    return True


def main():
    s = read_str()
    res = solve(s)
    if res:
        print('YES')
    else:
        print('NO')


main()

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

main=input()

prevl=len(main)

while True:
    for ele in s:
        main=main.replace(ele,'')
    if len(main)==prevl:
        break
    prevl=len(main)
if main=='':
    print("YES")
else:
    print("NO")

Solution (Python):
S = input().replace("dream","&").replace("erase","%").replace("&er","").replace("%r","").replace("&","").replace("%","")
if S:
    print("NO")
else:
    print("YES")

Solution (Python):
S = input().replace("dream","&").replace("erase","%").replace("&er","").replace("%r","").replace("&","").replace("%","")
if S:
    print("NO")
else:
    print("YES")

Solution (Python):
import sys

S = input()

while S:
    prefix = S[0:5]
    if prefix == 'dream':
        if S[5:7] == 'er':
            if len(S) >= 8 and S[7] == 'a':
                S = S[5:]
            else:
                S = S[7:]
        else:
            S = S[5:]
    elif prefix == 'erase':
        if len(S) >= 6 and S[5] == 'r':
            S = S[6:]
        else:
            S = S[5:]
    else:
        print('NO')
        sys.exit(0)
    
print('YES')

Solution (Python):
s = input();

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

while(len(s) > 4):
    if s[-5:] == words[0]:
        s = s[:-5]
    elif s[-7:] == words[1]:
        s = s[:-7]
    elif s[-5:] == words[2]:
        s = s[:-5]
    elif s[-6:] == words[3]:
        s = s[:-6]
    else:
        break

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

Solution (Python):
from collections import defaultdict


def main():
    S = input()
    while len(S) != 0:
        change = False
        for s in ["dream", "dreamer", "erase", "eraser"]:
            if S.endswith(s):
                S = S[:-len(s)]
                change = True
                break
        if not change:
            break

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
s=0
S=str(input())
while len(S)>=0:
  if s==len(S):
    print("YES")
    sys.exit()
  if S[s:s+5]=="dream":
    if len(S)==s+5:
      print("YES")
      sys.exit()
    elif S[s+5]=="d" or S[s+5:s+8]=="era":
      s+=5
    elif S[s+5:s+8]=="erd" or S[s+5:s+8]=="ere":
      s+=7
    elif S[s+5:s+7]=="er" and len(S)==s+7:
      print("YES")
      sys.exit()
    else: 
      print("NO")
      sys.exit()
  elif S[s:s+5]=="erase":
    if len(S)==s+5:
      print("YES")
      sys.exit()
    elif S[s+5]=="d" or S[s+5]=="e":
      s+=5
    elif S[s+5]=="r":
      s+=6
    else: 
      print("NO")
      sys.exit()
  else: 
    print("NO")
    sys.exit()

Solution (Python):
ch = input()
ch = ch[::-1]
flag = 1

while flag>0 and len(ch)>0:
    if ch.find('maerd')==0 or ch.find('esare')==0:
        ch = ch[5:]
    elif ch.find('remaerd')==0:
        ch = ch[7:]
    elif ch.find('resare')==0:
        ch = ch[6:]
    else:
        flag = -1
if flag<0:
    print("NO")
else:
    print("YES")

Solution (Python):
s = raw_input()

while len(s) > 0:
  if s.startswith('eraser'):
    s = s[6:]
  elif s.startswith('erase'):
    s = s[5:]
  elif s.startswith('dreamere') or s.startswith('dreamerd') or s == 'dreamer':
    s = s[7:]
  elif s.startswith('dream'):
    s = s[5:]
  else:
    print 'NO'
    break
else:
  print 'YES'

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

while f > 0 and len(S)>0:
    if S.find('maerd') ==0 or S.find('esare') == 0:
        S = S[5:]
    elif S.find('remaerd') == 0:
        S = S[7:]
    elif S.find('resare') == 0:
        S = S[6:]
    else:
        f = -1

if f > 0:
    print('YES')
else:
    print('NO')

Solution (Python):
S = raw_input()
i = len(S)
while i >= 0:
    hit = False
    for w in ["dreamer", "eraser", "dream", "erase"]:
        if S[i-len(w):i] == w:
            i -= len(w)
            hit = True
            break
    if not hit:
        break
print "YES" if (i == 0) else "NO"

Solution (Python):


s = raw_input()
t = ''

front = 0
rear = 7
while (s != t):
    if('dreamer' in s[front:rear]):
        if('eraser' in s[front:rear+4]):
            t += s[front:rear+4]
            front = rear + 4
            rear = front + 7
        elif('erase' in s[front:rear+3]):
            t += s[front:rear+3]
            front = rear + 3
            rear = front + 7
        else:
            t += s[front:rear]
            front = rear
            rear = front + 7
    elif('eraser' in s[front:rear]):
        t += s[front:rear-1]
        front = rear - 1
        rear = front + 7
    elif('dream' in s[front:rear]):
        t += s[front:rear-2]
        front = rear - 2
        rear = front + 7
    elif('erase' in s[front:rear]):
        t += s[front:rear-2]
        front = rear - 2
        rear = front + 7
    else:
        print('NO')
        exit()
print('YES')

Solution (Python):
s = input()

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

while len(s)>4:
    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
    else:
        break

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

Solution (Python):
s = input()



s = s[::-1]





tmp = s

while tmp != '':
    d = tmp[:5] == 'maerd'
    dr = tmp[:7] == 'remaerd'
    e = tmp[:5] == 'esare'
    er = tmp[:6] == 'resare'
    
    

    if d:
        tmp = tmp[5:]
    elif e:
        tmp = tmp[5:]
    elif dr:
        tmp = tmp[7:]
    elif er:
        tmp = tmp[6:]
    else:
        print('NO')
        break

if tmp == '':
    print('YES')

Solution (Python):
a = ["eraser", "erase", "dreamer", "dream"]
s = input()
for i in range(4):
    s = s.replace(a[i], "")
if s == "":
    print("YES")
else:
    print("NO")

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

DREAM   = "dream"
DREAMER = "dreamer"
ERASE   = "erase"
ERASER  = "eraser"

pre = ""
while len(s) > 0 and pre != s:
    pre = s[:]
    if s[0] == "d":
        if len(s) <= 5:
            s = s.replace(DREAM, "", 1)
        elif s[5] != "e":
            s = s.replace(DREAM, "", 1)
        else:
            if s.find(ERASE) == len(DREAM):
                s = s.replace(DREAM, "", 1)
            else:
                s = s.replace(DREAMER, "", 1)
    else:
        if s.find(ERASE) == s.find(ERASER):
            s = s.replace(ERASER, "", 1)
        else:
            s = s.replace(ERASE, "", 1)

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

Solution (Python):
from collections import defaultdict


def main():
    S = input()
    while True:
        change = False
        for x in "dreamer", "eraser", "dream", "erase":
            if x == S[-len(x):]:
                S = S[:-len(x)]
                change = True
                break
        if not change:
            break

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

def solve():
    S = input()
    pos = len(S)

    while pos > 0:
        if S[pos - 5:pos] == 'dream' or S[pos - 5:pos] == 'erase':
            pos -= 5
        elif S[pos - 7:pos] == 'dreamer':
            pos -= 7
        elif S[pos - 6:pos] == 'eraser':
            pos -= 6
        else:
            print('NO')
            return None

    print('YES')


if __name__ == '__main__':
    solve()

Solution (Python):
s = input()

def day_dream(s):
    while not 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:
            return "NO"
    return "YES"

print(day_dream(s))

Solution (Python):
import re

S = input()
x = r"^(dream|dreamer|erase|eraser)*$"

if re.match(x,S):
	print('YES')
else:
	print('NO')

Solution (Python):
S = input()

dreamdeleted=False

while len(S) > 0:
    if dreamdeleted == True:
        if S[0:6] == 'eraser':
            S = S[6:len(S)]
            dreamdeleted=False
            continue
        if S[0:5] == 'erase':
            S = S[5:len(S)]
            dreamdeleted=False
            continue
        if S[0:2] == 'er':
            S = S[2:len(S)]
            dreamdeleted=False
            continue
    if S[0:6] == 'eraser':
        S = S[6:len(S)]
        continue
    if S[0:5] == 'dream':
        S = S[5:len(S)]
        dreamdeleted = True
        continue
    if S[0:5] == 'erase':
        S=S[5:len(S)]
        continue
    print('NO')
    break
if len(S) == 0:
    print ('YES')

Solution (Python):

import re
S = input()
a = re.sub(r'eraser', '', S)
b = re.sub(r'erase', '', a)
c = re.sub(r'dreamer', '', b)
d = re.sub(r'dream', '', c)
if len(d) == 0:
    print("YES")
else:
    print("NO")

Solution (Python):
S = raw_input() + "end!"

def solve():
    pos = 0
    while True:
        plus_one_flag = 0
        if S[pos:pos+5] == "dream":
            pos += 5
        elif S[pos:pos+5] == "erase":
            pos += 4
            plus_one_flag = 1
        elif S[pos:pos+6] == "end!":
            return True
        else:
            return False

        if S[pos:pos+4] == "erer" or S[pos:pos+4] == "erdr" or S[pos:pos+4] == "eren":
            pos += 2
            plus_one_flag = 0
        pos += plus_one_flag

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

Solution (Python):
S = input()
T = ''
for i in range(int(len(S)/5)+1):
    if len(S) != len(T):
        if S[len(S)-len(T)-1] == 'm':
            T = 'dream' + T
        elif S[len(S)-len(T)-1] == 'e':
            T = 'erase' + T
        elif S[len(S)-len(T)-3:len(S)-len(T)] == 'mer':
            T = 'dreamer' + T
        elif S[len(S)-len(T)-3:len(S)-len(T)] == 'ser':
            T = 'eraser' + T
        else:
            break

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

Solution (Python):
import re

S = input()

x = r"^(dream|dreamer|erase|eraser)*$"

if re.match(x, S):
    print("YES")
else:
    print("NO")

Solution (Python):
words = ("dream", "erase", "eraser", "dreamer")

s = input()

while len(s) > 0:
    match = False
    for w in words:
        if s.endswith(w):
            s = s[:-len(w)]
            match = True
            break
    if match is False:
        print("NO")
        exit()
print("YES")

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

while e > 0:
    if e >= 5 and (s[e-5:e] == 'dream' or s[e-5:e] == 'erase'):
        e -= 5
    elif e >= 6 and s[e-6:e] == 'eraser':
        e -= 6
    elif  e >= 7 and s[e-7:e] == 'dreamer':
        e -= 7
    else:
        break

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

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

Solution (Python):
import re

text = input()
obj = re.match("^(dream|dreamer|erase|eraser)+$", text)
if obj:
    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 = raw_input()[::-1]
while len(s):
    if s.startswith("maerd"):
        s = s[5:]
    elif s.startswith("remaerd"):
        s = s[7:]
    elif s.startswith("esare"):
        s = s[5:]
    elif s.startswith("resare"):
        s = s[6:]
    else:
        print "NO"
        exit()
print "YES"

Solution (Python):
s = str(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) != 0:
    if s.endswith('dream') or s.endswith('erase'):
        s = s[:-5]
    elif s.endswith('eraser'):
        s = s[:-6]
    elif s.endswith('dreamer'):
        s = s[:-7]
    else:
        print('NO')
        sys.exit()

print('YES')

Solution (Python):

import re


S = ''
T = ''
regex = r'(dream|dreamer|erase|eraser)+$'


S = raw_input()


if re.match(regex,S):
    print 'YES'
else:
    print 'NO'

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

Solution (Python):
class DayDream:

    def is_possible(self, S):

        S = S[::-1]

        candidates = ["dream"[::-1], "dreamer"[::-1], "erase"[::-1], "eraser"[::-1]]

        idx = 0
        while idx < len(S):
            
            if S[idx:idx+5] in candidates:
                idx += 5

            elif S[idx:idx+6] in candidates:
                idx += 6

            elif S[idx:idx+7] in candidates:
                idx += 7

            else:
                return "NO"

        return "YES"

if __name__ == "__main__":

    S = raw_input()

    abc = DayDream()
    print abc.is_possible(S)

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

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

Solution (Python):
str_list = ["dream","dreamer","erase","eraser"]

class stringJudge():
    def __init__(self,string,string_list):
        self.string = string
        self.string_list = string_list
        
    def isNextMatch(self):
        for str_val in self.string_list:
            if self.string.endswith(str_val):
                self.string = self.string.rsplit(str_val,1)[0]
                return True
        return False
               
                    
                    
           
    
instance = stringJudge(input(),str_list)
check = True
while check and len(instance.string) > 0:
    check = instance.isNextMatch()

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

Solution (Python):
str_list = ["dream","erase","er","ase","r",]

class stringJudge():
    def __init__(self,string,string_list):
        self.string = string
        self.string_list = string_list
        
    def isNextMatch(self):
        for (i,str_val) in enumerate(str_list):
            if self.string.startswith(str_val):
                self.string = self.string.split(str_val,1)[1]
                if len(self.string) == 0:
                    return True
                if i ==0:
                    if self.string.startswith(self.string_list[0]) or self.string.startswith(self.string_list[2]):
                        return True
                elif i == 1:
                    if self.string.startswith(self.string_list[0]) or self.string.startswith(self.string_list[1]) or self.string.startswith(self.string_list[4]):
                        return True
                elif i == 2:
                    if self.string.startswith(self.string_list[0]) or self.string.startswith(self.string_list[1]) or self.string.startswith(self.string_list[3]):
                        return True
                elif i == 3:
                    if self.string.startswith(self.string_list[0]) or self.string.startswith(self.string_list[1])  or self.string.startswith(self.string_list[4]):
                        return True
                elif i == 4:
                    if self.string.startswith(self.string_list[0]) or self.string.startswith(self.string_list[1]):
                        return True
        return False
                    
                    
           
    
instance = stringJudge(input(),str_list)
check = True
while check and len(instance.string) > 0:
    check = instance.isNextMatch()

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

Solution (Python):
S = input()
W = ["dream","dreamer","erase","eraser"]
while len(S) >= 5:
	if len(S) >= 7 and S[-7:] in W:
		S = S[:-7]
	elif len(S) >= 6 and S[-6:] in W:
		S = S[:-6]
	elif S[-5:] in W:
		S = S[:-5]
	else:
		print("NO")
		exit()
if not S:
	print("YES")
else:
	print("NO")

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

Solution (Python):
import sys
def DFS(list,s,t):
    if s==t:
        print("YES")
        sys.exit()
    if not t in s:
        return

    for i in list:
        DFS(list,s,t+i)

list=["maerd","remaerd","esare","resare"]
s=input()
s=s[::-1]
t=""
c_index=0
while True:
    moto=c_index
    for i in list:
        index=0
        temp=c_index
        while index<len(i) and temp<len(s) and i[index]==s[temp]:
            index+=1
            temp+=1
        if index==len(i):
            c_index=temp
            break
    if c_index==len(s):
        print("YES")
        sys.exit()
    if c_index==moto:
        print("NO")
        sys.exit()

Solution (Python):
s = raw_input()

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

while len(s) >= 5 and flag:
  for i in range(len(words)):
    if s[-len(words[i]):] == words[i]:
      s = s[:-len(words[i])]
      break
  else:
    flag = False
    print 'NO'
    break

if flag:
  print 'YES'

Solution (Python):
s = raw_input()

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

while len(s) >= 5 and flag:
  for i in range(len(words)):
    if s[-len(words[i]):] == words[i]:
      s = s[:-len(words[i])]
      break
  else:
    flag = False
    print 'NO'
    break

if flag:
  print 'YES'

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 s=="":
    print("YES")
else:
    print("NO")

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

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

Solution (Python):
S = raw_input()
S.replace("a","sine")
if "eraser" in S:
    S=S.replace("eraser", "")
if "erase" in S:
    S=S.replace("erase", "")
if "dreamer" in S:
    S=S.replace("dreamer", "")
if "dream" in S:
    S=S.replace("dream","")
print "YES" if len(S)==0 else "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()
flag = 1
i = 0

while i < len(s):
    if s[i:i+5] == 'dream':
        if s[i+5:i+10] != "erase" and s[i+5:i+7] == "er":
            i += 7
        elif s[i+5:i+11] == "eraser":
            i += 11
        elif s[i+5:i+10] == "erase":
            i += 10
        else:
            i += 5
    elif s[i:i+6] == "eraser":
        i += 6
    elif s[i:i+5] == "erase":
        i += 5
    else:
        flag = 0
        break

if(flag == 1):
    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()
head = 0
dreameraser = ["dream","dreamer","erase","eraser"]
for i in range(len(s)):
    for word in dreameraser:
        head = len(s) - len(word)
        cut = s[head:head+len(word)]

        if cut in dreameraser:
            s = s[0:head]
if s == "":
    print("YES")
else:
    print("NO")

Solution (Python):
s = input()

t = ""

a = "dream"
b = "dreamer"
c = "erase"
d = "eraser"

s = s.replace(d,"")
s = s.replace(c,"")
s = s.replace(b,"")
s = s.replace(a,"")

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

Solution (Python):
s =raw_input()[::-1]
t = map (lambda x:x[::-1], ["eraser","erase","dreamer","dream"])
for var in t:
    s = s.replace(var,"")
if s == "":
    print "YES"
else :
    print "NO"

Solution (Python):
import re
import sys
s = input()
while True:
    if s[-5:] in ['dream', 'erase']:
        s = s[0:-5]
    elif s[-6:] in ['eraser']:
        s = s[0:-6]
    elif s[-7:] in ['dreamer']:
        s = s[0:-7]
    else:
        if s == '':
            print('YES')
            sys.exit()
        else:
            print("NO")
            sys.exit()

Solution (Python):


s=raw_input()
t=s.replace('eraser','')
u=t.replace('erase','')
v=u.replace('dreamer','')
w=v.replace('dream','')

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

Solution (Python):
S = input()
w1="dream"
w2="dreamer"
w3="erase"
w4="eraser"
S=S.replace(w4,"").replace(w3,"").replace(w2,"").replace(w1,"")
if len(S)==0:
    print("YES")
else:
    print("NO")

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

preS=''
while len(S)!=0 and S!=preS:
    preS = S;
    pre = S[:7]
    if pre[:7][::-1] == 'dreamer':
        S=S[7:]
    elif pre[:6][::-1] == 'eraser':
        S=S[6:]
    elif pre[:5][::-1] == 'dream':
        S=S[5:]
    elif pre[:5][::-1] == 'erase':
        S=S[5:]

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

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

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

Solution (Python):
s=input()
while True:
    temp = s[:5]
    s = s[5:]
    if temp == 'erase':
        if s[0:1] == 'r':
            s = s[1:]
            continue
        else:
            continue
    elif temp == 'dream':
        if s[:5] == 'erase':
            continue
        elif s[:2] == 'er':
            s = s[2:]
            continue
    elif temp == '':
        print('YES')
        break
    else:
        print('NO')
        break

Solution (Python):
from Queue import * 
from bisect import * 
from collections import * 

import math
import copy
import itertools
import string
import sys
myread = lambda : map(int,raw_input().split())
def solver():
    S = raw_input()
    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__":
    solver()

Solution (Python):
a=["eraser","erase","dreamer","dream"]
s=input()
 
for i in range(4):
    s=s.replace(a[i],"")
if s=="":
    print("YES")
else:
    print("NO")

Solution (Python):


import sys

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

def check(s):
    token = s

    while token:
        if token in TOKENS:
            return "YES"

        if len(token) > 5 and token[-5:] in [TOKENS[0], TOKENS[2]]:
            token = token[0:-5]
        elif len(token) > 6 and token[-6:] == TOKENS[3]:
            token = token[0:-6]
        elif len(token) > 7 and token[-7:] == TOKENS[1]:
            token = token[0:-7]
        else:
            return "NO"

def main(argv):
    for line in sys.stdin:
        print check(line.rstrip("\n"))

if __name__ == "__main__":
    main(sys.argv)

Solution (Python):
S = input()
L = len(S)
index = L-1
flag = 0
searching = 0

while index> 0:
    if S.rfind('dreamer',0,index+1) == index-6 and S.rfind('dreamer',0,index+1) != -1:
        index = index - 7
        
        if index == -1 :
            flag = 1
            break;
        
    elif S.rfind('eraser',0,index+1) == index-5 and S.rfind('eraser',0,index+1) != -1:

        index = index - 6
        
        if index == -1 :
            flag = 1
            break;
    else:
        s1 = S.rfind('dream',0,index+1)  
        s2 = S.rfind('erase',0,index+1)
        if (s1 != -1 or s2 != -1 )and(s1==index-4 or s2 ==index-4):
            

            index = index -5
            
            if index  == -1 :
                flag = 1
                break;
        else :
            break;


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

Solution (Python):



import sys

def match(s):
    if s[:7] == 'dreamer':
        if len(s) > 7 and s[7] == 'a':
            return 5
        else:
            return 7
    if s[:6] == 'eraser':
        return 6
    if s[:5] == 'dream' or s[:5] == 'erase':
        return 5
    return -1

def main():
    s = raw_input()
    offset = 0
    while offset < len(s):
        n = match(s[offset:])
        if n == -1:
            print 'NO'
            sys.exit()
        offset += n
    print 'YES'

if __name__ == '__main__':
    main()

Solution (Python):
s=raw_input()
l=s.split('eraser')
s=''
for i in l:
    s=s+i
l=s.split('erase')
s=''
for i in l:
    s=s+i
l=s.split('dreamer')
s=''
for i in l:
    s=s+i
l=s.split('dream')
if(l.count('')==len(l)):
    print 'YES'
else:
    print 'NO'

Solution (Python):
import re
input_line = input()[::-1]
if len("".join(re.findall('resare|remaerd|esare|maerd|',input_line))) == len(input_line):
    print("YES")
else:
    print("NO")

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