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()
print('YES' if s.replace('eraser','').replace('erase','').replace('dreamer','').replace('dream','')=='' else 'NO')

Solution (Python):


def YESNO(ans, yes="YES", no="NO"):
    print([no, yes][ans])

II = lambda: int(input())
MI = lambda: map(int, input().split())
MIL = lambda: list(MI())
MIS = lambda: input().split()


def check(s):
    if s == "":
        return True
    if s[:5] in ["dream", "erase"]:
        return True
    return False


def main():
    S = input()
    i = 0
    l = len(S)
    while i < l:
        if S[i:i + 7] == "dreamer" and check(S[i + 7:]):
            i += 7
        elif S[i:i + 6] == "eraser" and check(S[i + 6:]):
            i += 6
        elif check(S[i:]):
            i += 5
        else:
            return False
    return True

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

Solution (Python):
s=list(input())
c=0
while len(s)>0:
    p="1"
    q="1"
    r="1"
    if len(s)<5:
        break
    if len(s)<=7:
        if len(s)==5:
            if s==list("dream") or s==list("erase"):
                c=1
                break
        elif len(s)==6:
            if s==list("eraser"):
                c=1
                break
        elif len(s)==7:
            if s==list("dreamer"):
                c=1
                break
    else:
        p=s[len(s)-5:len(s)]
        q=s[len(s)-6:len(s)]
        r=s[len(s)-7:len(s)]
        if p==list("dream") or p==list("erase"):
            del s[len(s)-5:len(s)]
        elif q==list("eraser"):
            del s[len(s)-6:len(s)]
        elif r==list("dreamer"):
            del s[len(s)-7:len(s)]
        else:
            break
print("YES" if c==1 else"NO")

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

Solution (Python):


"""
Created on Fri Feb  8 05:12:38 2019

@author: shinjisu
"""


def db(x):
    global debug
    if debug:
        print(x)



def gen(tstr):
    if len(tstr) <= len(S):
        for w in words:
            yield tstr + w
            for w2 in gen(tstr + w):
                yield w2



def solve():
    ans = False
    for genstr in gen(''):
        db(genstr)
        if genstr == S:
            ans = True
            break
    return ans



def solve2():
    stack = list(words)
    while stack != []:
        str = stack.pop()
        db(('pop', str))
        if len(str) < len(S):
            for w in words:
                stack.append(str+w)
        elif len(str) == len(S) and str == S:
            return True
    return False



def solve3():
    string = S + ' '
    n = len(string)
    while n > 1:
        for w in words:
            db((string.rfind(w, 0, n),len(w),n))
            if string.rfind(w, 0, n) + len(w) + 1 == n:
                break
        else:
            return False
        db((n,w))
        n -= len(w)
    return True


debug = False
words = ('dream', 'dreamer', 'erase', 'eraser')
S = input()
db(S)
if solve3():
    print('YES')
else:
    print('NO')

Solution (Python):
s = input()

search = []
search.append(s)
found = False

while len(search) > 0:
    se = search[0]
    
    search = search[1:]
    if se in ("dream", "dreamer", "erase", "eraser"):
        print("YES")
        found = True
        break
    if len(se) < 5:
        continue
    if se[0:5] in ("dream", "erase"):
        search.append(se[5:])
    if se[0:6] in ("eraser"):
        search.append(se[6:])
    if se[0:7] in ("dreamer"):
        search.append(se[7:])

if not found:
    print("NO")

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


while True:
    if len(S) == 0:
        break
    judge = [S.endswith(i) for i in given]
    if True in judge:
        index = judge.index(True)
        length = len(given[index])
        S = S[:-length]
    else:
        print('NO')
        quit()

print('YES')

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

Solution (Python):
import sys
sys.setrecursionlimit(10**9)
s1 = "maerd"
s2 = "remaerd"
s3 = "esare"
s4 = "resare"

i = input()
s = ''
for c in reversed(i):
    s += c

l = len(s)

i = 0


def dfs(i):
    if i == l:
        print("YES")
        exit()
    if i+5 <= l and s[i:i+5] == s1:
        dfs(i+5)
    elif i+7 <= l and s[i:i+7] == s2:
        dfs(i+7)
    elif i+5 <= l and s[i:i+5] == s3:
        dfs(i+5)
    elif i+6 <= l and s[i:i+6] == s4:
        dfs(i+6)
    else:
        print("NO")
        exit()


dfs(0)

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

s = input()
l = len(s)


def dfs(i):
   
    if i == l:
        print("YES")
        exit()
    if s[i:i+5] == "dream":
        if i+6 < l and s[i+5:i+7] == "er":
            dfs(i+7)
        dfs(i+5)
    elif s[i:i+5] == "erase":
        if i+5 < l and s[i+5] == "r":
            dfs(i+6)
        dfs(i+5)


dfs(0)
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 = str(input())
words = ["dream","erase","eraser","dreamer"]
i = len(s)
while i>0:
    if s[-5:] in words:
        s = s[:-5]
        i -= 5
    elif s[-6:] in words:
        s = s[:-6]
        i -= 6
    elif s[-7:] in words:
        s = s[:-7]
        i -= 7
    else:
        break
if i==0:
    print("YES")
else:
    print("NO")

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

c=0
k=0
while c==0:
    if k ==n:
        c = 1
    else:
        
        if k + 7 <= n-1 and s[k]=='d' and s[k+1] == 'r' and s[k+2] == 'e' and s[k+3] == 'a' and s[k+4] == 'm' and s[k+5] == 'e' and s[k+6] == 'r' and s[k+7] == 'a':
            k += 5
        elif k + 6 <= n-1 and s[k]=='d' and s[k+1] == 'r' and s[k+2] == 'e' and s[k+3] == 'a' and s[k+4] == 'm' and s[k+5] == 'e' and s[k+6] == 'r':
            k += 7
        elif k + 4 <= n-1 and s[k]=='d' and s[k+1] == 'r' and s[k+2] == 'e' and s[k+3] == 'a' and s[k+4] == 'm':
            k += 5
        elif k+6 <= n-1 and s[k] == 'e' and s[k+1] == 'r' and s[k+2] == 'a' and s[k+3] == 's' and s[k+4] == 'e' and  s[k+5] == 'r' and s[k+6] == 'a':
            k+= 5
        elif k+5 <= n-1 and s[k] == 'e' and s[k+1] == 'r' and s[k+2] == 'a' and s[k+3] == 's' and s[k+4] == 'e' and  s[k+5] == 'r':
            k += 6
        elif k+4 <= n-1 and s[k] == 'e' and s[k+1] == 'r' and s[k+2] == 'a' and s[k+3] == 's' and s[k+4] == 'e':
            k += 5
        else:
            c = 1

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

Solution (Python):
ar = list(input())
while True:
    a = len(ar)
    if a == 0:
        print("YES")
        break
    if "".join(ar[a-5:a]) == "dream" or "".join(ar[a-5:a]) == "erase":
        del ar[a-5:a]
    elif "".join(ar[a-6:a]) == "eraser":
        del ar[a-6:a]
    elif "".join(ar[a-7:a]) == "dreamer":
        del ar[a-7:a]
    else:
        print("NO")
        break

Solution (Python):
S = input()
rS = S[::-1]
keys = ['dream', 'dreamer', 'erase', 'eraser']
reversed_keys = [keys[i][::-1] for i in range(len(keys))]
i = 0

while i < len(rS):
    if rS[i:i+5] in reversed_keys:
        i += 5
    elif rS[i:i+6] in reversed_keys:
        i += 6
    elif rS[i:i+7] in reversed_keys:
        i += 7
    else:
        break
if i == len(rS):
    print('YES')
else:
    print('NO')

Solution (Python):
daydream = ['dream', 'dreamer', 'erase', 'eraser']
s = input()
import sys
while s:
    for i in daydream:
        if s.endswith(i):
            s = s[:-len(i)]
            break
    else:
        break
else:
    print('YES')
    sys.exit()
print('NO')

Solution (Python):
S=input()
i=3
while i<=len(S):
    if i==3:
        if S[-3:]=="eam":
            i+=2
            if S[-i:-i+2]=="dr":
                i+=3
            else:
                print("NO")
                break
        elif S[-3:]=="mer":
            i+=4
            if S[-i:-i+4]=="drea":
                i+=3
            else:
                print("NO")
                break
        elif S[-3:]=="ase":
            i+=2
            if S[-i:-i+2]=="er":
                i+=3
            else:
                print("NO")
                break
        elif S[-3:]=="ser":
            i+=3
            if S[-i:-i+3]=="era":
                i+=3
            else:
                print("NO")
                break
        else:
            print("NO")
            break
    else:
        if S[-i:-i+3]=="eam":
            i+=2
            if S[-i:-i+2]=="dr":
                i+=3
            else:
                print("NO")
                break
        elif S[-i:-i+3]=="mer":
            i+=4
            if S[-i:-i+4]=="drea":
                i+=3
            else:
                print("NO")
                break
        elif S[-i:-i+3]=="ase":
            i+=2
            if S[-i:-i+2]=="er":
                i+=3
            else:
                print("NO")
                break
        elif S[-i:-i+3]=="ser":
            i+=3
            if S[-i:-i+3]=="era":
                i+=3
        
        else:
            print("NO")
            break
else:
    print("YES")

Solution (Python):
s = input()
s = s[::-1]
l = "dream dreamer erase eraser".split()
l = [i[::-1] for i in l]


while len(s) > 0:
    flag = False
    for i in l:
        n = len(i)
        if s[:min(n, len(s))] in i:
            s = s[min(n, len(s)):]
            flag = True
            break
    if not flag:
        print("NO")
        exit()
print("YES")

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

Solution (Python):
S=input().strip()
def solve(S):
    while len(S)>0:
        if S[-6:]=='eraser':
            S=S[:-6]
        elif S[-7:]=='dreamer':
            S=S[:-7]
        elif S[-5:]=='erase':
            S=S[:-5]
        elif S[-5:]=='dream':
            S=S[:-5]
        else:
            return False
    return True

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

Solution (Python):
def check(s):
    if s == '':
        return True
    while(s != ''):
        if s.startswith('eraser'):
            s = s[6:]
        elif s.startswith('erase'):
            s = s[5:]
        elif s.startswith('dreamera'):
            
            s = s[5:]
        elif s.startswith('dreamer'):
            s = s[7:]
        elif s.startswith('dream'):
            s = s[5:]
        else:
            return False
    return True

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

Solution (Python):
import sys
import collections
import itertools
import math
from collections import Counter
from collections import deque
MOD = 10 ** 9 + 7

S = input()

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

s = S
while len(s) > 0:
    deleted = False
    for k in keys:
        idx = s.rfind(k)
        if idx+len(k) == len(s):
            s = s[0:idx]
            deleted = True
            break
    if deleted == False:
        break

if deleted == False or len(s) != 0:
    print("NO")
else:
    print("YES")


sys.exit(0)

Solution (Python):
Ss = input()

lenS = len(Ss)
i = 0
while i < lenS:
    if Ss[i:].startswith('dream'):
        if Ss[i + 5:i + 7] == 'er' and Ss[i + 7:i + 8] != 'a': 
            i += 7
        else: 
            i += 5
    elif Ss[i:].startswith('erase'):
        if Ss[i + 5:i + 6] == 'r': 
            i += 6
        else: 
            i += 5
    else:
        print('NO')
        break
else:
    print('YES')

Solution (Python):
S=input()

S=S[::-1]

count=0
while(count<len(S)):
  if (len(S)-count)<5:
    print('NO')
    exit()
    
  elif S[count]=='e' and S[count+1]=='s' and S[count+2]=='a' and S[count+3]=='r' and S[count+4]=='e':
    count+=5
  elif S[count]=='m' and S[count+1]=='a' and S[count+2]=='e' and S[count+3]=='r' and S[count+4]=='d':
    count+=5
  
  elif (len(S)-count)==5:
    print('NO')
    exit()
  elif S[count]=='r' and S[count+1]=='e' and S[count+2]=='s' and S[count+3]=='a' and S[count+4]=='r' and S[count+5]=='e':
    count+=6
  elif (len(S)-count)==6:
    print('NO')
    exit()
  elif S[count]=='r' and S[count+1]=='e' and S[count+2]=='m' and S[count+3]=='a' and S[count+4]=='e'and S[count+5]=='r' and S[count+6]=='d':
    count+=7
  else:
    print('NO')
    exit()

print('YES')

Solution (Python):
s = input()

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


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


"""
1 => 1,2
"""

Solution (Python):
match = ['eraser', 'erase', 'dreamer', 'dream']
len_m = [len(m) for m in match]
S = input()
n = len(S)
dp = [False] * (n + 1)
dp[0] = True
for i in range(1, n + 1):
    for w, word in zip(len_m, match):
        if S[i - w:i] == word and dp[i - w]:
            dp[i] = True
            break
print('YES' if dp[n] else 'NO')

Solution (Python):
match = ['eraser', 'erase', 'dreamer', 'dream']
len_m = [len(m) for m in match]
S = input()
n = len(S)
dp = [False] * (n + 1)
dp[0] = True
for i in range(1, n + 1):
    for lm, m in zip(len_m, match):
        if S[:i].endswith(m) and dp[i - lm]:
            dp[i] = True
            break
print('YES' if dp[n] else 'NO')

Solution (Python):
match = ['eraser', 'erase', 'dreamer', 'dream']
len_m = [len(m) for m in match]
S = input()
n = len(S)
dp = [False] * (n + 1)
dp[0] = True
for i in range(1, n + 1):
    for lm, m in zip(len_m, match):
        if S[:i].endswith(m):
            dp[i] |= dp[i - lm]
print('YES' if dp[n] else 'NO')

Solution (Python):
s = input()
w = ['dream','dreamer','erase','eraser']
flag = True
while flag:
    flag = False
    if len(s)<5:
        break
    for i in w:
        if s[-len(i):] == i:
            s = s[:-len(i)]
            flag = True
if s:
    print('NO')
else:
    print('YES')

Solution (Python):
import sys
S=input()
L=['dream','dreamer','erase','eraser']
T=[i[::-1] for i in L]
S=S[::-1]
while True:
    
    if len(S)==0:
        print('YES')
        sys.exit()
    if S[:5]==T[0]:
        S=S[5:]
        continue
    if S[:5]==T[2]:
        S=S[5:]
        continue
    if S[:7]==T[1]:
        S=S[7:]
        continue
    if S[:6]==T[3]:
        S=S[6:]
        continue
    print('NO')
    sys.exit()

Solution (Python):


s = input()[::-1]
w = [
    "dream"[::-1]
    ,"dreamer"[::-1]
    ,"erase"[::-1]
    ,"eraser"[::-1]
]

while len(s)>0:
    if s.startswith(w[0]):
        s = s[len(w[0]):]
    elif s.startswith(w[1]):
        s = s[len(w[1]):]
    elif s.startswith(w[2]):
        s = s[len(w[2]):]
    elif s.startswith(w[3]):
        s = s[len(w[3]):]
    else:
        break

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

Solution (Python):
s = input()

def solve(s):
    i = 0
    n = len(s)

    while i < n:
        if s[i:i+5] == 'dream':
            if s[i+5:i+7] != 'er':
                i += 5
            elif i+7 < n and s[i+7] == 'a':
                i += 5
            else:
                i += 7
        elif s[i:i+5] == 'erase':
            if i+5 < n and s[i+5] == 'r':
                i += 6
            else:
                i += 5
        else:
            return False
    return True

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

Solution (Python):
S=input()
S=S[::-1]
cnt=["dream","dreamer","erase","eraser"]

for i in range(4):
  s=cnt[i]
  s=s[::-1]
  cnt[i]=s

while len(S):
  if S[:5]==cnt[0]:
    S=S[5:]
  elif S[:7]==cnt[1]:
    S=S[7:]
  elif S[:5]==cnt[2]:
    S=S[5:]
  elif S[:6]==cnt[3]:
    S=S[6:]
  else:
    break

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

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

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

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

Solution (Python):
S=input().strip()
ws=['dream','dreamer','erase','eraser']
while True:
  change = False
  for w in ws:
    if len(w) <= len(S) and S[-len(w):] == w:
      S = S[:-len(w)]
      change = True
  if not change:
    break
print("YES" if len(S) == 0 else "NO")

Solution (Python):
S = input()

keys = ('eraser', 'erase', 'dreamer', 'dream')

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

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

Solution (Python):
S = input()

keys = ('dreameraser', 'eraser', 'erase', 'dreamer', 'dream')

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

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

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

Solution (Python):
s=input()
d=['dream','dreamer','erase','eraser']
while s:
    for m in d:
        if s.endswith(m):
            s=s[:-len(m)]
            break
    else:
        print('NO')
        exit()
print('YES')

Solution (Python):
s=input()

while 1:

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

Solution (Python):
s=input()[::-1]
d=['maerd','remaerd','esare','resare']
while s:
    for i in range(4):
        if s.startswith(d[i]):
            s=s[len(d[i]):]
            break
    else:
        print('NO')
        exit()
print('YES')

Solution (Python):
import re

s = raw_input()
print ('YES' if re.match('^((dream)|(dreamer)|(erase)|(eraser))*$',s) else 'NO')

Solution (Python):

S = input()
L = len(S)
S += "________"
i = 0
res = "YES"
while i < L:
    if S[i:i+5] == "dream":
        if S[i+5:i+7] == "er":
            if S[i+7] == "a":
                i += 5 
            else:
                i += 7 
        else:
            i += 5 
    elif S[i:i+5] == "erase":
        if S[i+5] == "r":
            i += 6 
        else:
            i += 5 
    else:
        res = "NO"
        break

print(res)

Solution (Python):

import re

s = input()
s = re.sub('eraser?', '1', s)
s = re.sub('dream(er)?', '1', s)
print('YES' if len(s) == s.count('1') else 'NO')

Solution (Python):
DAYDREAM = ["dream", "dreamer", "erase", "eraser" ]
S = input()

ind = len(S)
res = True
while ind > 0:
	ok = False
	for i in DAYDREAM:
		if S[ind - len(i):ind] == i:
			ind -= len(i)
			ok = True
			break;
	if ok == False:
		res = False
		break

print("YES" if res == True else "NO")

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

ind = len(S)
res = True

while ind > 0:
	ok = False
	for i in DAYDREAM:

		if S[ind - len(i):ind] == i:
			ind -= len(i)
			ok = True
			break;
	if ok == False:
		res = False
		break

print("YES" if res == True else "NO")

Solution (Python):
S = raw_input().split()[0]
t = ['dreameraser','dreamerase','dreamer','dream','eraser','erase']
while len(S) > 0:
    for i in t:
        if S[-len(i):]==i:
            S = S[:-len(i)]
            break
        elif i == t[-1]:
            print "NO"
            exit()
print "YES"

Solution (Python):
s = input()

five = ["dream", "erase"]
six = ["eraser"]
seven = ["dreamer"]

i = len(s) - 1

ans = False
while True:
  if i < 4:
    break
  elif i == 4:
    ans = s[:5] in five
    break
  elif i == 5:
    ans = s[:6] in six
    break
  elif i == 6:
    ans = s[:7] in seven
    break
  else:
    if s[i+1-5:i+1] in five:
      i -= 5
    elif s[i+1-6:i+1] in six:
      i -= 6
    elif s[i+1-7:i+1] in seven:
      i -= 7
    else:
      break

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

Solution (Python):
import sys
pattern = ["dream", "dreamer", "erase", "eraser"]
split_pattern = ["dream", "er", "ase", "r"]
grammer = {
        "ase": ["er"],
        "r": ["ase", "er"],
        "er": ["dream"],
        "dream": [],
        }
s = input()

splited = []
while s:
    for spl in split_pattern:
        if s.startswith(spl):
            splited.insert(0, spl)
            s = s[len(spl):]
            break
    else:
        print("NO")
        sys.exit()

pred = []
for word in splited:
    if not pred:
        pred = grammer[word][:]
    else:
        if word != pred[0]:
            print("NO")
            break
        else:
            del pred[0]
else:
    print("YES")

Solution (Python):
import sys

def solve(s):
    tmp_s = s
    while(len(tmp_s) > 0):
        if tmp_s.startswith("maerd"):
            tmp_s = tmp_s[5:]
        elif tmp_s.startswith("remaerd"):
            tmp_s = tmp_s[7:]
        elif tmp_s.startswith("esare"):
            tmp_s = tmp_s[5:]
        elif tmp_s.startswith("resare"):
            tmp_s = tmp_s[6:]
        else:
            return False

    return True

def main():
    line = sys.stdin.readline().rstrip()
    line_rev = line[::-1]

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

    return 0

if __name__ == '__main__':
    sys.exit(main())

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

while S:
    for w in W:
	if S.endswith(w):
             S = S[:-len(w)]
             break
    else:
        print "NO"
        break
else:
    print "YES"

Solution (Python):
import sys

S = raw_input()
S = S[::-1]
T = ''
words = ['dreamer', 'dream', 'eraser', 'erase']
for i in range(len(words)):
    words[i] = words[i][::-1]
while len(S):
    N = len(S)
    for word in words:
        if S[:len(word)] == word:
            S = S[len(word):]
            break
    if N == len(S):
        sys.stdout.write('NO')
        break
    if len(S) == 0:
        sys.stdout.write('YES')

Solution (Python):
S = raw_input()
while len(S) > 0:
	if S.startswith("dream"):
		S = S[len("dream"):]
		if S.startswith("er"):
			if S.startswith("erase"):
				continue
			else:
				S = S[len("er"):]
		continue
	elif S.startswith("eraser"):
		S = S[len("eraser"):]
		continue
	elif S.startswith("erase"):
		S = S[len("erase"):]
		continue
	break
if len(S) > 0:
	print("NO")
else:
	print("YES")

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**10
mod = 10**9 + 7


def main():
    p = 'dream dreamer erase eraser'.split()
    pp = [[_, len(_)] for _ in p]
    s = input()
    while len(s) > 0:
        f = False
        for ps, pl in pp:
            if ps == s[-pl:]:
                s = s[:-pl]
                f = True
                break
        if not f:
            return 'NO'

    return 'YES'


print(main())

Solution (Python):
S = input()

i = 0

a = ["dreameraser", "dreamerase", "dreamer", "eraser", "dream", "erase"]
while i < len(S):
    f = False
    for j in a:
        l = len(j)
        if S[i:i + l] == j:
            f = True
            i += l
            break

    if not f:
        print("NO")
        exit(0)

print("YES")

Solution (Python):
dream, erase = "dream erase".split()

s = input().strip()
while 1:
  if s.startswith(dream):
     s = s[5:]
     if s.startswith("er") and not s.startswith("era"):
       s = s[2:]
  elif s.startswith(erase):
    s = s[5:]
    if s.startswith('r'):
      s = s[1:]
  else:
    break
print("NO" if s else "YES")

Solution (Python):
import sys

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

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

Solution (Python):
import sys

s = input()

i = 0
N = len(s)
while i < N:
    if s[i:i+5] == 'dream':
        i += 5
    elif s[i:i+6] == 'eraser':
        i += 6
    elif s[i:i+5] == 'erase':
        i += 5
    elif s[i:i+2] == 'er' and s[i-5:i] == 'dream':
        i += 2
    else:
        print("NO")
        sys.exit()
print("YES")

Solution (Python):
import sys
 
S = list(raw_input())
pos = 0
while (pos < len(S)):
	if (''.join([S[x] for x in range(pos, min(pos+11, len(S)), 1)]) == "dreameraser"):
		pos += 11
	elif (''.join([S[x] for x in range(pos, min(pos+10, len(S)), 1)]) == "dreamerase"):
		pos += 10
	elif (''.join([S[x] for x in range(pos, min(pos+7, len(S)), 1)]) == "dreamer"):
		pos += 7
	elif (''.join([S[x] for x in range(pos, min(pos+6, len(S)), 1)]) == "eraser"):
		pos += 6
	elif (''.join([S[x] for x in range(pos, min(pos+5, len(S)), 1)]) == "erase"):
		pos += 5
	elif (''.join([S[x] for x in range(pos, min(pos+5, len(S)), 1)]) == "dream"):
		pos += 5
	else:
		print "NO"
		sys.exit()
print "YES"

Solution (Python):
S = input()
for i in ['eraser', 'erase', 'dreamer', 'dream']:
    S=S.replace(i, 'R')
s = set(S)
print(['NO', 'YES'][len(s) == 1 and 'R' in s])

Solution (Python):
import re

if re.match("^(dream(er)?|eraser?)*$", input()):
    print("YES")
else:
    print("NO")

Solution (Python):
S = raw_input()

def pick_greedy(S):
    if S[0:5] == 'dream':
        if S[5:7] == 'er':
            if S[7] == 'a':
                ret = 'dream'
            else:
                ret = 'dreamer'
        else:
            ret = 'dream'
    elif S[0:5] == 'erase':
        if S[5] == 'r':
            ret = 'eraser'
        else:
            ret = 'erase'
    else:
        ret = 'no'
    return ret

sol = 'YES'

while len(S) > 0:
    try:
        r = pick_greedy(S)
        if r == 'no':
            sol = 'NO'
            break
        else:
            if len(S) < len(r):
                sol = 'NO'
                break
            else:
                S = S[len(r):]
    except:
        sol = 'YES'
        break
        
print sol

Solution (Python):
import sys

s = raw_input()
l = ['dreameraser', 'dreamerase', 'eraser', 'dreamer', 'dream', 'erase']

flag = True
while flag:
    if len(s) == 0:
        print 'YES'
        sys.exit()
    for w in l:
        if s.find(w) == 0:
            s = s[len(w):len(s)]
            break
    else:
        flag = False
print 'NO'

Solution (Python):
import sys
s=raw_input()
while True:
    flag=True
    n=len(s)
    if n==0:print 'YES';sys.exit()
    if n>4:
        if s[n-5:]=='dream' or s[n-5:]=='erase':
            s=s[:n-5]
            flag=False
    n=len(s)
    if n>5:
        if s[n-6:]=='eraser':
            s=s[:n-6]
            flag=False
    n=len(s)
    if n>6:
        if s[n-7:]=='dreamer':
            s=s[:n-7]
            flag=False
    if flag: print 'NO';sys.exit()

Solution (Python):
T = ["maerd", "remaerd", "esare", "resare"]

s = raw_input()
s = s[::-1]

ans = True
while True:
	if s == "":
		ans = True
		break
	elif s[0] == "m" and len(s) >= len(T[0]) and s[:len(T[0])] == T[0]:
		s = s[len(T[0]):]
	elif s[0] == "r" and len(s) > 2:
		if s[2] == "m" and len(s) >= len(T[1]) and s[:len(T[1])] == T[1]:
			s = s[len(T[1]):]
		elif s[2] == "s" and len(s) >= len(T[3]) and s[:len(T[3])] == T[3]:
			s = s[len(T[3]):]
		else:
			ans = False
			break
	elif s[0] == "e" and len(s) >= len(T[2]) and s[:len(T[2])] == T[2]:
		s = s[len(T[2]):]
	else:
		ans = False
		break

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

Solution (Python):
s = input()

dream = "dream"
erase = "erase"

ans = True
ind = 0
while ind < len(s):
	if s[ind] == 'd':
		tmp = s[ind:ind+5]
		if tmp == dream:
			ind += 5
			if ind + 2 == len(s):
				if s[ind:ind+2] == 'er':
					break
			elif ind + 2 < len(s):
				if s[ind:ind+2] == 'er' and s[ind+2] != 'a':
					ind += 2
		else:
			ans = False
			break
	elif s[ind] == 'e':
		tmp = s[ind:ind+5]
		if tmp == erase:
			ind += 5
			if ind < len(s):
				if s[ind] == 'r':
					ind += 1
		else:
			ans = False
			break
	else:
		ans = False
		break

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10**7)

def dfs(s,idx):
    if idx==len(s):
        print("YES")
        exit()
    if idx>len(s):
        return
    if idx+5<=len(s) and s[idx:idx+5]=="dream":
        dfs(s,idx+5)
    if idx+7<=len(s) and s[idx:idx+7]=="dreamer":
        dfs(s,idx+7)
    if idx+5<=len(s) and s[idx:idx+5]=="erase":
        dfs(s,idx+5)
    if idx+6<=len(s) and s[idx:idx+6]=="eraser":
        dfs(s,idx+6)
    return
s=raw_input()
dfs(s,0)
print("NO")

Solution (Python):
S=str(input())
S=S[::-1]
while True:
    if S[:5]=='maerd':
        S=S[5:]

    elif S[:7]=='remaerd':
        S=S[7:]
    elif S[:5]=='esare':
        S=S[5:]
    elif S[:6]=='resare':
        S=S[6:]
    elif len(S)==0:
        print('YES')
        exit()
    else:
        print('NO')
        exit()

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

Solution (Python):
t=raw_input()

while len(t):
    if len(t)>10 and t[:11]=='dreameraser':
        t=t[11:]
    elif len(t)>9 and t[:10]=='dreamerase':
        t=t[10:]
    elif len(t)>6 and t[:7]=='dreamer':
        t=t[7:]
    elif len(t)>4 and t[:5]=='dream':
        t=t[5:]
    elif len(t)>5 and t[:6]=='eraser':
        t=t[6:]
    elif len(t)>4 and t[:5]=='erase':
        t=t[5:]
    else:
        break
print 'YES' if t=='' else 'NO'