Buggy python submission (verdict: Time Limit Exceeded):
rules = ['dream', 'dreamer', 'erase', 'eraser']

s = input()
stack = [0]
mem = [0]
ok = False
loop = 0
while len(stack) > 0:
    loop += 1
    #print(stack, mem)
    start_id = stack.pop()
    string = s[start_id:]
    if start_id == len(s):
        print('YES')
        ok = True
        break

    for rule in rules:
        if string.startswith(rule):
            next_start_id = start_id + len(rule)
            if next_start_id <= len(s):
                part = string[next_start_id:]
                #if part.startswith('d') or part.startswith('e') or part == '':
                if next_start_id not in mem:
                    stack.append(next_start_id)
                    mem.append(next_start_id)
if not ok:
    print('NO')

Fixed submission:
rules = ['dream', 'dreamer', 'erase', 'eraser']

s = input()
stack = [0]
mem = [0 for _ in range(len(s)+1)]
ok = False
while len(stack) > 0:
    start_id = stack.pop()
    string = s[start_id:]
    if start_id == len(s):
        print('YES')
        ok = True
        break

    for rule in rules:
        if string.startswith(rule):
            next_start_id = start_id + len(rule)
            if next_start_id <= len(s):
                part = string[next_start_id:]
                if mem[next_start_id] == 0:
                    stack.append(next_start_id)
                    mem[next_start_id] = 1
if not ok:
    print('NO')

Buggy python submission (verdict: Time Limit Exceeded):
rules = ['dream', 'dreamer', 'erase', 'eraser']

s = input()
stack = [0]
mem = [0]
ok = False
while len(stack) > 0:
    start_id = stack.pop()
    string = s[start_id:]
    if string == '':
        print('YES')
        ok = True
        break
    for rule in rules:
        if string.startswith(rule):
            next_start_id = start_id + len(rule)
            part = string[next_start_id:]
            if part not in mem:
                stack.append(next_start_id)
                mem.append(next_start_id)
if not ok:
    print('NO')

Fixed submission:
rules = ['dream', 'dreamer', 'erase', 'eraser']

s = input()
stack = [0]
mem = [0 for _ in range(len(s)+1)]
ok = False
while len(stack) > 0:
    start_id = stack.pop()
    string = s[start_id:]
    if start_id == len(s):
        print('YES')
        ok = True
        break

    for rule in rules:
        if string.startswith(rule):
            next_start_id = start_id + len(rule)
            if next_start_id <= len(s):
                part = string[next_start_id:]
                if mem[next_start_id] == 0:
                    stack.append(next_start_id)
                    mem[next_start_id] = 1
if not ok:
    print('NO')

Buggy python submission (verdict: Time Limit Exceeded):
rules = ['dream', 'dreamer', 'erase', 'eraser']

stack = [input()]
mem = [stack[0]]
ok = False
while len(stack) > 0:
    string = stack.pop()
    if string == '':
        print('YES')
        ok = True
        break
    for rule in rules:
        if string.startswith(rule):
            part = string[len(rule):]
            if part not in mem:
                stack.append(part)
                mem.append(part)
if not ok:
    print('NO')

Fixed submission:
rules = ['dream', 'dreamer', 'erase', 'eraser']

s = input()
stack = [0]
mem = [0 for _ in range(len(s)+1)]
ok = False
while len(stack) > 0:
    start_id = stack.pop()
    string = s[start_id:]
    if start_id == len(s):
        print('YES')
        ok = True
        break

    for rule in rules:
        if string.startswith(rule):
            next_start_id = start_id + len(rule)
            if next_start_id <= len(s):
                part = string[next_start_id:]
                if mem[next_start_id] == 0:
                    stack.append(next_start_id)
                    mem[next_start_id] = 1
if not ok:
    print('NO')