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



Sample input:
erasedream


Sample output:
YES

Submission status: Accepted

Solution code:
S = input()
S = S[::-1]
key = [
    "dream"[::-1],
    "erase"[::-1],
    "dreamer"[::-1],
    "eraser"[::-1]
]
f = False
while(S != ""):
    f = False
    for i in range(4):
        tmp = S[0:len(key[i])]
        if (tmp == key[i]):
            S = S[len(key[i]):]
            f = True
            break
    if not(f):
        break
        
if (f):
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

s=list(ss())
lens=len(s)
flag=True

s.reverse()

word = ("maerd","remaerd","esare","resare")

cnt=0

while cnt <lens:
    cnt_chk=cnt
    if "".join(s[cnt:cnt+5])==word[0]:
        cnt_chk+=5
    elif "".join(s[cnt:cnt+7])==word[1]:
        cnt_chk+=7
    elif "".join(s[cnt:cnt+5])==word[2]:
        cnt_chk+=5
    elif "".join(s[cnt:cnt+6])==word[3]:
        cnt_chk+=6
    if cnt==cnt_chk:
        print("NO")
        exit()
    else:
        cnt=cnt_chk
print("YES")

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(4100000)
import math
import fractions



'''
1行のint
N, K = map(int, input().split())

1行のstring
S, T = input().split()

1行の整数配列
P = list(map(int,input().split()))

複数行2数値
x = []
y = []
for i in range(5):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)

'''


S = input()


check = len(S)-1

while(check >= 0):

    if S[check] == "r":
        if S[check-2] == "m":
            remove = S[check-6: check+1]
            if remove == "dreamer":
                check -= 7
            else:
                print("NO")
                sys.exit()


        elif S[check-2] == "s":
            remove = S[check-5: check+1]
            
            if remove == "eraser":
                check -= 6
            else:
                print("NO")
                sys.exit()

        else:
            print("NO")
            sys.exit()

    elif S[check] == "e":
        remove = S[check-4: check+1]
            
        if remove == "erase":
            check -= 5
        else:
            print("NO")
            sys.exit()

    elif S[check] == "m":
        remove = S[check-4: check+1]
            
        if remove == "dream":
            check -= 5
        else:
            print("NO")
            sys.exit()

    else:
        print("NO")
        sys.exit()


print("YES")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import _collections
import string
from functools import lru_cache
import heapq

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def lcm(a,b):
        return (a * b) // Math.gcd(a,b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3,d + 1,2):
            if n % i == 0:
                return False
        return True


MOD = int(1e09) + 7

def main():
    S = cin.string()
    while len(S) != 0:
        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")
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = list(input())

i = len(S)-1
while i >= 4:
    if S[i-4:i+1] == ['d', 'r', 'e', 'a', 'm'] or S[i-4:i+1] == ['e', 'r', 'a', 's', 'e']:
        i -= 5
    elif i-5 >= 0 and S[i-5:i+1] == ["e", "r", "a", "s", "e", "r"]:
        i -= 6
    elif i-6 >= 0 and S[i-6:i+1] == ["d", "r", "e", "a", "m", "e", "r"]:
        i -= 7
    else:
        break
        
if i == -1:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
S = input()[: -1]
dp = [0] * (len(S) + 1)
dp[0] = 1
for i in range(len(S)):
  if i + 5 <= len(S):
    if S[i: i + 5] == "dream": dp[i + 5] += dp[i]
    if S[i: i + 5] == "erase": dp[i + 5] += dp[i]
  if i + 7 <= len(S):
    if S[i: i + 7] == "dreamer": dp[i + 7] += dp[i]
  if i + 6 <= len(S):
    if S[i: i + 6] == "eraser": dp[i + 6] += dp[i]
if dp[-1]: print("YES")
else: print("NO")

Submission status: Accepted

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

Submission status: Accepted

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

ws = ['maerd','remaerd','esare','resare']
i = 0
while i < len(S):
    for w in ws:
        if w == S[i:i+len(w)]:
            i += len(w)
            break
    else:
        print('NO')
        exit()
print('YES')

Submission status: Accepted

Solution code:
S = input()
T = ['dream', 'dreamer', 'erase', 'eraser']

rS = str(S[:: -1])
rT = [str(t[:: -1]) for t in T]

now = 0
while now < len(S):
    for t in rT:
        if t == rS[now: now + len(t)]:
            now += len(t)
            break
    else:
        print('NO')
        exit()

print('YES')

Submission status: Accepted

Solution code:
S = input()
S = "".join(list(reversed(S)))
str_list = ["dream", "dreamer", "erase", "eraser"]
rev_str = []
for i in str_list:
    rev_str.append("".join(list(reversed(i))))
is_OK = True
while len(S) > 0:
    if S[0:5] in rev_str:
        S = S[5:]
    elif S[0:6] in rev_str:
        S = S[6:]
    elif S[0:7] in rev_str:
        S = S[7:]
    else:
        is_OK = False
        break
if is_OK:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
def conform(string, i, Tmember):
    for t in Tmember:
        if s[i] != t:
            print("NO")
            exit()
        else:
            i += 1
    return i


S = str(input())
T = ["maerd", "remaerd", "esare", "resare"]
s = ""
for i in range(1, len(S)+1):
    s += S[-i]
count = 0
while(count < len(s)):
    flag = 4
    if s[count] == "m":
        flag = 0
    elif (s[count] == "r")and(s[count+1] == "e")and(s[count+2] == "m"):
        flag = 1
    elif s[count] == "e":
        flag = 2
    elif (s[count] == "r")and(s[count+1] == "e")and(s[count+2] == "s"):
        flag = 3
    else:
        print("NO")
        exit()
    count = conform(s, count, T[flag])
print("YES")

Submission status: Accepted

Solution code:
s = input()
while s:
    m = [t for t in ['dream', 'dreamer', 'erase', 'eraser'] if s.endswith(t)]
    if not m:
        print('NO')
        exit()
    s = s[:-len(m[0])]
print('YES')

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def main():
    s = input().strip()[::-1]

    while True:
        ok = False
        if len(s) >= 7:
            if s[:7] == "remaerd":
                s = s[7:]
                ok = True
        if len(s) >= 6:
            if s[:6] == "resare":
                s = s[6:]
                ok = True
        if len(s) >= 5:
            if s[:5] == "esare" or s[:5] == "maerd":
                s = s[5:]
                ok = True
        if not ok:
            break
    
    # print(s)
    if len(s) > 0:
        print("NO")
    else:
        print("YES")

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
S = input()
for i in range(100000):
  if S[len(S)-5:]=="dream" or S[len(S)-5:]=="erase":
    S = S[:len(S)-5]
  elif S[len(S)-7:]=="dreamer":
    S = S[:len(S)-7]
  elif S[len(S)-6:]=="eraser":
    S = S[:len(S)-6]
  elif S == "":
    print("YES")
    break
  else:
    print("NO")
    break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
reverse_s = input().strip()[::-1]

while True:
    if len(reverse_s) == 0:
        print("YES")
        exit()
    if reverse_s.startswith('maerd'):
        reverse_s = reverse_s[len('maerd'):]
    elif reverse_s.startswith('remaerd'):
        reverse_s = reverse_s[len('remaerd'):]
    elif reverse_s.startswith('esare'):
        reverse_s = reverse_s[len('esare'):]
    elif reverse_s.startswith('resare'):
        reverse_s = reverse_s[len('resare'):]
    else:
        print("NO")
        exit()

Submission status: Accepted

Solution code:
S = input()[::-1]
n = len(S)
count = 0
flag = True

while count<n:
    if S[count:count+5] == "dream"[::-1]:
        count += 5
    elif S[count:count+7] == "dreamer"[::-1]:
        count += 7
    elif S[count:count+5] == "erase"[::-1]:
        count += 5
    elif S[count:count+6] == "eraser"[::-1]:
        count += 6
    else:
        flag = False
        break

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

Submission status: Accepted

Solution code:
S = list(input())

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

ans = 'YES'
l = len(S)
while l>=5:
    if S[l-5:l] == list(A[0]):
        l -= 5
    elif S[l-5:l] == list(A[2]):
        l -= 5
    elif l == 5:
        break
    elif S[l-6:l] == list(A[3]):
        l -= 6
    elif l == 6:
        break
    elif S[l-7:l] == list(A[1]):
        l -= 7
    else:
        ans = 'NO'
        break
if l != 0:
    ans = 'NO'
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S = input()
last_ind = len(S)
yes = True
for i in range(len(S) - 1, -1, -1):
    if S[i:last_ind] == "dream" or S[i:last_ind] == "erase" or S[i:last_ind] == "eraser" or S[i:last_ind] == "dreamer":
        last_ind = i
        continue
    if last_ind - i > 7 or i == 0:
        yes = False
        break
        
if yes:
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
S = input()
t = len(S)
L = [("dream", 5), ("dreamer", 7), ("erase", 5), ("eraser", 6)]
while t > 0:
    for w, l in L:
        if S.rfind(w, 0, t) == t - l:
            t -= l
            break
    else:
        print("NO")
        break
else:
    print("YES")

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
#ARC65C
import copy
import sys
#import math

hoge = input()
n = len(hoge)

while(1):
	n = len(hoge)
	if(n==0):
		print("YES")
		sys.exit()
	if(n<=4):
		print("NO")
		sys.exit()
	else:
		if(hoge[-5:]=="dream"):
			tmp=copy.copy(hoge[0:-5])
			hoge=tmp
		elif(hoge[-5:]=="erase"):
			tmp=copy.copy(hoge[0:-5])
			hoge=tmp
		elif(n<=5):
			print("NO")
			sys.exit()
		else:
			if(hoge[-6:]=="eraser"):
				tmp=copy.copy(hoge[0:-6])
				hoge=tmp
			elif(n<=6):
				print("NO")
				sys.exit()
			else:
				if(hoge[-7:]=="dreamer"):
					tmp=copy.copy(hoge[0:-7])
					hoge=tmp
				else:
					print("NO")
					sys.exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S=input()
a=["dream","dreamer","erase","eraser","dreamerase","dreameraser"]

numlist=[0,0,0,0,0,0]
for i in range(len(a)):
    numlist[i]=S.count(a[i])
numlist[0]-=numlist[1]
numlist[2]-=numlist[3]
numlist[4]-=numlist[5]
numlist[1]-=(numlist[4]+numlist[5])   #誤ってdreamerで数えた分を
numlist[0]+=(numlist[4]+numlist[5])   #dreamに計上しなおす
total=numlist[0]*5+numlist[1]*7+numlist[2]*5+numlist[3]*6
if total==len(S):
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
S = input()

C = S[::-1]
while C:
    if C[:5] == "maerd":
        C = C[5:]
    elif C[:7] == "remaerd":
        C = C[7:]
    elif C[:5] == "esare":
        C = C[5:]
    elif C[:6] == "resare":
        C = C[6:]
    else:
        print("NO")
        exit()
print("YES")

Submission status: Accepted

Solution code:
s = list(input())
s.reverse()
s = ''.join(map(str, s))
count = 0
word = ['maerd', 'remaerd', 'resare', 'esare']

while count <= len(s)-8:
  counttemp = count
  for i in range(4):
    w = word[i]
    if s[count:count+len(w)] == w:
      count += len(w)
  if counttemp == count:
    break

if s[count:] in word or s[count:] =='':
  print('YES')
else:
  print('NO')

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

Submission status: Accepted

Solution code:
def solve(S):
    N = len(S)
    i = 0
    while i < N:
        if N - i < 5:
            return False
        if S[i: i + 5] == "dream":
            if i + 7 <= N and S[i + 5: i + 7] == "er":
                if i + 7 == N or S[i + 7] != "a":
                    i += 7
                else:
                    i += 5
            else:
                i += 5
        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

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

Submission status: Accepted

Solution code:
#dream dreamer erase eraser
import sys

S = input()
index = len(S)
while index > 7:
    five = S[index-5:index]
    six = S[index-6:index]
    seven = S[index-7:index]
    if five == "dream" or five == "erase":
        index -= 5
    elif six == "eraser":
        index -= 6
    elif seven == "dreamer":
        index -= 7
    else:
        print("NO")
        sys.exit()

last = S[:index]
if last == "dream" or last == "erase" or last == "eraser" or last == "dreamer":
    print("YES")
else:
    print("NO")

Submission status: Accepted

Solution code:
s=input()
import sys
while len(s)>0:
    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')
        sys.exit()
print('YES')

Submission status: Accepted

Solution code:
import re

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")

sys.setrecursionlimit(10**5)


def yes():
    print("YES")  # type: str


def no():
    print("NO")  # type: str


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


def dfs(s, i):
    if i == len(s):
        return True
    for t in template:
        if s[i:].startswith(t):
            if dfs(s, i+len(t)):
                return True
    return False


def solve(S: str):
    if dfs(S, 0):
        yes()
    else:
        no()

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  # type: str
    solve(S)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
S = input()

word_lst = ['dream', 'dreamer', 'erase', 'eraser']
for i in range(len(word_lst)):
  word_lst[i] = word_lst[i][::-1]
  
S = S[::-1]
ans = ''
while len(ans) < len(S) - 4:
  if S[len(ans)] == 'm':
    ans = ans + word_lst[0]
  elif S[len(ans):len(ans)+3] == 'rem':
    ans = ans + word_lst[1]
  elif S[len(ans)] == 'e':
    ans = ans + word_lst[2]
  elif S[len(ans):len(ans)+3] == 'res':
    ans = ans + word_lst[3]
  else:
    break

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
S = input()
num = len(S)
S = S + "zzzzzzzzzzzzzzzzzzzzzz"
cur = 0
while cur < num:
    if S[cur] == 'd':
        if S[cur:cur+11] == 'dreameraser':
            cur += 11
        elif S[cur:cur+10] == 'dreamerase':
            cur += 10
        elif S[cur:cur+7] == 'dreamer':
            cur += 7
        elif S[cur:cur+5] == 'dream':
            cur += 5
        else:
            print('NO')
            sys.exit()
    elif S[cur] == 'e':
        if S[cur:cur+6] == 'eraser':
            cur += 6
        elif S[cur:cur+5] == 'erase':
            cur += 5
        else:
            print('NO')
            sys.exit()
    else:
        print('NO')
        sys.exit()
    if cur == num:
        print('YES')
        sys.exit()

Submission status: Accepted

Solution code:
s=raw_input().replace("dream","X").replace("erase","Y").replace("Xer","Z").replace("Yr","T")
for i in s: 
	if i not in "XYZT": 
		print "NO"
		exit(0)
print "YES"

Submission status: Accepted

Solution code:
s = raw_input()


s = s.replace("dream", "X").replace("erase", "Y")
# print s
s = s.replace("Xer", "Z").replace("Yr", "T")

for i in s:
	if i not in "XYZT":
		print "NO"
		exit(0)
print "YES"

Submission status: Accepted

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

Submission status: Accepted

Solution code:
S=input()
while True:
  if S=='':
    print('YES')
    break
  else:
    a = S[-5:] in ('dream', 'erase')
    b = S[-6:] in ('eraser')
    c = S[-7:] in ('dreamer')
    if not (a or b or c):
      print('NO')
      break
    elif a:
      S = S[:-5]
    elif b:
      S = S[:-6]
    elif c:
      S = S[:-7]

Submission status: Accepted

Solution code:
S = input()
S = S[::-1]
snipetts = [s[::-1] for s in ['dream', 'dreamer', 'erase', 'eraser']]

found = True
index = 0
while found:
    found = False
    for s in snipetts:
        if S[index:index+len(s)] == s:
            index += len(s)
            found = True
            break

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

Submission status: Accepted

Solution code:
S = input()
S = S[::-1]
 
S = S.replace('resare', '')
S = S.replace('esare', '')
S = S.replace('remaerd', '')
S = S.replace('maerd', '')
 
if S:
    print('NO')
else:
    print('YES')

Submission status: Accepted

Solution code:
S = 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:]
    else:
        break

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
l = ['maerd', 'remaerd', 'esare', 'resare']
flag = False
S = input()
S = ''.join(list(reversed(S)))
j = 0
for i in range(len(S)+1):
    if S[j:i] in l:
        j=i
        flag = True
    else:
        flag = False
print("YES" if flag else "NO")

Submission status: Accepted

Solution code:
s=input()
i=len(s)

while True:
    if i==0:
        print('YES')
        break
    if 6<i and s[i-7:i] == 'dreamer':
        i-=7
        continue
    if 5<i and s[i-6:i] == 'eraser':
        i-=6
        continue
    if 4<i and s[i-5:i] in ['dream','erase']:
        i-=5
        continue
    print('NO')
    break

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque

def solve(s):
    stack = deque()
    stack.append(s)
    while len(stack) > 0:
        top = stack.pop()
        if top == '':
            return 'YES'

        if top[:5] == 'dream':
            if top[5:7] == 'er':
                stack.append(top[7:])
            stack.append(top[5:])
        elif top[:5] == 'erase':
            if len(top) > 5 and top[5] == 'r':
                stack.append(top[6:])
            else:
                stack.append(top[5:])
    return 'NO'

print(solve(input()))

Submission status: Accepted

Solution code:
S = input()

a = len(S)

while a != 0:
    if a - 5 < 0:
        print("NO")
        exit()
    elif S[a - 1] == "m" and S[a - 5:a] == "dream":
        a -= 5
    elif S[a - 1] == "e" and S[a - 5:a] == "erase":
        a -= 5
    elif S[a - 3:a] == "mer" and a - 7 >= 0 and S[a - 7:a] == "dreamer":
        a -= 7
    elif S[a - 3:a] == "ser" and a - 6 >= 0 and S[a - 6:a] == "eraser":
        a -= 6
    else:
        print("NO")
        exit()
print("YES")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

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())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
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


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


# TLE
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')

Submission status: Accepted

Solution code:
s = input()

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

while len(search) > 0:
    se = search[0]
    # print(se, search)
    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")

Submission status: Accepted

Solution code:
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')

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

s = input()
l = len(s)


def dfs(i):
   #    print("->", s[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")

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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')