74 lines
2.0 KiB
Python
74 lines
2.0 KiB
Python
|
import sys
|
||
|
|
||
|
|
||
|
class DFA:
|
||
|
current_state = None
|
||
|
|
||
|
def __init__(self, states, alphabet, transition_function, start_state, accept_states):
|
||
|
self.states = states
|
||
|
self.alphabet = alphabet
|
||
|
self.transition_function = transition_function
|
||
|
self.start_state = start_state
|
||
|
self.accept_states = accept_states
|
||
|
self.current_state = start_state
|
||
|
return
|
||
|
|
||
|
def transition_to_state_with_input(self, input_value):
|
||
|
if ((self.current_state, input_value) not in self.transition_function.keys()):
|
||
|
print((self.current_state, input_value) not in self.transition_function.keys())
|
||
|
self.current_state = None
|
||
|
return
|
||
|
self.current_state = self.transition_function[(self.current_state, input_value)];
|
||
|
return
|
||
|
|
||
|
def in_accept_state(self):
|
||
|
return self.current_state in accept_states
|
||
|
|
||
|
def go_to_initial_state(self):
|
||
|
self.current_state = self.start_state
|
||
|
return
|
||
|
|
||
|
def run_with_input_list(self, input_list):
|
||
|
self.go_to_initial_state()
|
||
|
for inp in input_list:
|
||
|
self.transition_to_state_with_input(inp)
|
||
|
continue
|
||
|
return self.in_accept_state()
|
||
|
|
||
|
pass
|
||
|
|
||
|
|
||
|
states = []
|
||
|
alphabet = []
|
||
|
start_state = 0
|
||
|
accept_states = []
|
||
|
tf = dict()
|
||
|
|
||
|
|
||
|
def makeTf():
|
||
|
with open(sys.argv[1], 'r') as my_file:
|
||
|
for line in my_file:
|
||
|
arg = line.split(' ')
|
||
|
if len(arg) > 2:
|
||
|
a = arg[2].rstrip("\n")
|
||
|
if a not in alphabet:
|
||
|
alphabet.append(a)
|
||
|
tf[(int(arg[0]), arg[2].rstrip("\n"))] = int(arg[1])
|
||
|
else:
|
||
|
accept_states.append(int(arg[0].rstrip("\n")))
|
||
|
a = int(arg[0].rstrip("\n"))
|
||
|
if a not in states:
|
||
|
states.append(a)
|
||
|
|
||
|
|
||
|
makeTf()
|
||
|
|
||
|
d = DFA(states, alphabet, tf, start_state, accept_states)
|
||
|
|
||
|
for line in sys.stdin:
|
||
|
inp_program = list(line.rstrip("\n"))
|
||
|
if d.run_with_input_list(inp_program):
|
||
|
print("YES")
|
||
|
else:
|
||
|
print("NO")
|