import sys import string ########################################### ### Routines for creating and using DFA ########################################### ########################################### # Returns a DFA, currently the DFA for a*b+ # It would be more realistic to return a string, # but that change will be done in a later version. def createDFA(): numstates = 4 Q = set(range(0, numstates)) # States are 0 to 3 (not 4) Sigma = "ab" # Each symbol of the string is a member of the alphabet delta = (# a b (0, 1), # 0 (2, 1), # 1 (2, 2), # 2 (3, 3)) # 3 # State 3 is unreachable and not needed. It's included # so that it's easy to create a DFA for an empty # language by changing the accept state to state 3 # It also shows a state that's not marked. q0 = 0 F = set([1]) return (Q, Sigma, delta, q0, F) ################################ # Return delta(curstate, curchar) def applydelta(delta, Sigma, curstate, curchar): charpos = Sigma.find(curchar) nextstate = delta[curstate][charpos] return nextstate ########################################### ### A routine for TM A_DFA: ### a TM that decides if DFA M accepts w ########################################### ########################################### # Returns Accept if M accepts w # Returns Reject otherwise (ie if M rejects w) def decideDFA(M, w): (Q, Sigma, delta, q0, F) = M curstate = q0 while w: cursym = w[0] nextstate = applydelta(delta, Sigma, curstate, cursym) curstate = nextstate w = w[1:] # Remove first character if curstate in F: return 'Accept' else: return 'Reject' ########################################### ### Routines for TM E_DFA: ### a TM that decides if DFA M ### accepts the empty language ########################################### ###################################################### # Returns the set of states that can be reached in one # transition from any of the states in set reached # A helper function for decideEmpty def findReachable(reached, Sigma, delta): reachable = [] # Examine transitions using all pairs (q,c) where # q is a state in reached and c is a character in Sigma for q in reached: for c in Sigma: reachable.append(applydelta(delta, Sigma, q, c),) return set(reachable) ################################################ # Returns Accept if M accepts the empty language # Returns Reject otherwise # (ie if M does not accept the empty language) def decideEmpty(M): emptySet = set([]) (Q, Sigma, delta, q0, F) = M reached = emptySet reachable = set([q0]) while not reachable.issubset(reached): reached = reached | reachable reachable = findReachable(reached, Sigma, delta) if reached & F == emptySet: return 'Accept' else: return 'Reject' ########################################### ### Main Routine: ### Create a DFA M, check if M accepts ### the empty language, and input ### a string and see if M accepts it ########################################### # Create a DFA M M = createDFA() # Check if M's encoding is in the language E_DFA print decideEmpty(M) # Input string w and check if the pair # is in the language A_DFA_w w = sys.stdin.readline().strip() print decideDFA(M, w)