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

def solve(S):
	while(S!=""):
		if len(S)>=7 and S[-7:]=="dreamer":
			S=S[:-7]
		elif len(S)>=6 and S[-6:]=="eraser":
			S=S[:-6]
		elif len(S)>=5 and S[-5:]=="dream" :
			S=S[:-5]
		elif len(S)>=5 and S[-5:]=="erase":
			S=S[:-5]
		else:
			return("NO")
	return("YES")

print(solve(S))

Solution (Python):
s = input()

dp = [True] * (len(s)+1)

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

for i in range(1,len(s)+1):
	found = False
	for word in check:
		j = i - len(word)
		if j >= 0 and s[j:i] == word and dp[j]:
			found = True
			break
	dp[i] = found

if dp[len(s)]:
	print('YES')
else:
	print('NO')

Solution (Python):

s = input()[::-1]

t = [x[::-1] for x in ['dream', 'dreamer', 'erase', 'eraser']]

i = 0
while i < len(s):
    if s[i] == 'm':
        if s[i: i + 5] == t[0]:
            i += 5
            continue
    elif s[i] == 'e':
        if s[i: i + 5] == t[2]:
            i += 5
            continue
    elif s[i] == 'r':
        if s[i: i + 6] == t[3]:
            i += 6
            continue
        elif s[i: i + 7] == t[1]:
            i += 7
            continue
    break

print('YES' if i == len(s) else 'NO')

Solution (Python):



def rl(s): return xrange(len(s))

INF = 2147483647


import sys
stdin = sys.stdin

S =        ( stdin.readline().strip() )

N = len(S)
ok_ends = [False] * (N+1)
ok_ends[0] = True
for i in range(1, N+1):
    for word in ['dream', 'dreamer', 'erase', 'eraser']:
        if S[max(0, i-len(word)): i] == word:
            if ok_ends[max(0, i-len(word))]:
                ok_ends[i] = True

if ok_ends[-1]:
    print "YES"
else:
    print "NO"

Solution (Python):
def read():
    return int(input())
 
def reads(sep=None):
    return list(map(int, input().split()))
 
def main():
	a = ['dream', 'dreamer', 'erase', 'eraser']
	s = input()
	flag = True
	while flag:
		flag = False
		for i in range(len(a)):
			if a[i] == s[-len(a[i]):]:
				s = s[:-len(a[i])]
				flag = True
	if 0 < len(s):
		print('NO')
	else:
		print('YES')
	
main()

Solution (Python):
s = input()
s = s[::-1]
a = ['dream', 'dreamer', 'erase', 'eraser']
for i in range(4):
    a[i] = a[i][::-1]

while len(s) > 0:
    
    if s[:3] == 'rem' and s[:len(a[1])] == a[1]:
        s = s[len(a[1]):]
    elif s[:3] == 'res' and s[:len(a[3])] == a[3]:
        s = s[len(a[3]):]
    elif s[0] == 'e' and s[:len(a[2])] == a[2]:
        s = s[len(a[2]):]
    elif s[0] == 'm' and s[:len(a[0])] == a[0]:
        s = s[len(a[0]):]
    else:
        print("NO")
        raise SystemExit

print("YES")

Solution (Python):
s = raw_input()
s = s[::-1]
i = 0
dream = False
while i < len(s):
	p = s[i:i+7]
	
	if p == "remaerd":
		i += 7
	elif p[:6] == "resare":
		i += 6
	elif p[:5] == "maerd" or p[:5] == "esare":
		i += 5
	else:
		break
	
if i >= len(s):
	print "YES"
else:
	print "NO"

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

def no():
  print("NO"); exit()

S = input()

n = len(S)

while n >= 5:
  
  pf = S[n-3:n]
  if pf == "eam":
    if S[n-5:n] == "dream":
      n -= 5
    else:
      no()
  elif pf == "mer":
    if n >= 7 and S[n-7:n] == "dreamer":
      n -= 7
    else:
      no()
  elif pf == "ase":
    if S[n-5:n] == "erase":
      n -= 5
    else:
      no()
  elif pf == "ser":
    if n >= 6 and S[n-6:n] == "eraser":
      n -= 6
    else:
      no()
  else:
    no()

if n > 0:
  no()
else:
  print("YES"); exit()

Solution (Python):
s = input()

i = len(s)
p = ''
while i>=5:
    if s[i-5:i] == 'dream':
        i -= 5
    elif 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:
        print('NO')
        exit()
if i == 0:
    print('YES')
else:
    print('NO')

Solution (Python):
s=raw_input()
while True:
    isDeleted=False
    if s[-7:] == "dreamer":
        s = s[:-7]
        isDeleted=True
    elif s[-6:] == "eraser":
        s = s[:-6]
        isDeleted=True
    elif s[-5:] == "erase" or s[-5:] == "dream":
        s = s[:-5]
        isDeleted=True
    if not isDeleted: break
if len(s)==0: print "YES"
else: print "NO"

Solution (Python):



from collections import *
S = input()

seen = set()
dq = deque()

ok = False
seen.add(0)
while len(seen) > 0:
    pos = sorted(list(seen))[0]
    seen.remove(pos)
    if pos == len(S):
        ok = True
        break

    part = S[pos:]
    if part.startswith('dream'):
        seen.add(pos + 5)
    if part.startswith('dreamer'):
        seen.add(pos + 7)
    if part.startswith('erase'):
        seen.add(pos + 5)
    if part.startswith('eraser'):
        seen.add(pos + 6)
if ok:
    print('YES')
else:
    print('NO')

Solution (Python):



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


def can_construct(s):
    while len(s) > 0:
        for word in WORDS:
            i = s.rfind(word)
            if i != -1 and len(s) == i + len(word):
                s = s[:i]
                break
        else:
            return False
    return True


def main():
    print("YES" if can_construct(input()) else "NO")


if __name__ == '__main__':
    main()

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

Solution (Python):
s = raw_input()
while True:
    
    if s == "":
        print 'YES'
        break
    elif s[-5:] == "dream" or s[-5:] ==  "erase":
        s = s[:-5]
    elif s[-7:] == "dreamer":
        s = s[:-7]
    elif s[-6:] == "eraser":
        s = s[:-6]
    else:
        print "NO"
        break

Solution (Python):
from sys import stdin
a = stdin.readline().strip()
b = ['dream', 'dreamer', 'erase' , 'eraser']
c = []
for i in b:
    c.append(len(i))
n = len(a)
dp = [0] * (n+1)
dp[0] = 1
for i in xrange(1,n+1):
    ind = i-1
    for jj in xrange(4):
        j = b[jj]
        l = c[jj]
        pr = i - l 
        if pr >=0:
            if dp[pr] and j==a[pr:i]:
                dp[i] = 1
if dp[-1]:
    print 'YES'
else:
    print 'NO'

Solution (Python):
s = raw_input()
l = len(s)
dp = [0]*(l+1)
dp[0] = 1
for i in xrange(l):
    if dp[i] == 0: continue

    if i+5 <= l and s[i:i+5] in ["dream", "erase"]:
        dp[i+5] = 1
    if i+6 <= l and s[i:i+6] == "eraser":
        dp[i+6] = 1
    if i+7 <= l and s[i:i+7] == "dreamer":
        dp[i+7] = 1
print "YES"*dp[l]or"NO"

Solution (Python):
import re

S = raw_input()

if re.match(r'(?:dream(?:er)?|eraser?)+$', S):
    print "YES"
else:
    print "NO"

Solution (Python):
import re

print('YES' if re.fullmatch('(dream(er)?|eraser?)+', input()) else 'NO')