75 lines
1.9 KiB
Python
75 lines
1.9 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()):
|
||
|
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()
|
||
|
|
||
|
def accept(self, data):
|
||
|
exit = {7: 'END'}
|
||
|
state = 0
|
||
|
|
||
|
for letter in data:
|
||
|
if (state, letter) in self.transition_function:
|
||
|
state = self.transition_function[state, letter]
|
||
|
if state in exit:
|
||
|
return True
|
||
|
else:
|
||
|
state = 0
|
||
|
return False
|
||
|
|
||
|
pass
|
||
|
|
||
|
|
||
|
states = [1, 2, 3, 4, 5, 6, 7]
|
||
|
alphabet = ['1', '2', '3', '4', '5', '6', '7', '8', '9', ' ', '.', 'r']
|
||
|
start_state = 0
|
||
|
accept_states = [7]
|
||
|
tf = dict()
|
||
|
tf[(0, 'O')] = 1;
|
||
|
tf[(1, 'p')] = 2;
|
||
|
tf[(2, 'h')] = 3;
|
||
|
tf[(3, 'e')] = 4;
|
||
|
tf[(4, 'l')] = 5;
|
||
|
tf[(5, 'i')] = 6;
|
||
|
tf[(6, 'a')] = 7;
|
||
|
|
||
|
|
||
|
d = DFA(states, alphabet, tf, start_state, accept_states)
|
||
|
|
||
|
for line in sys.stdin:
|
||
|
inp_program = list(line.rstrip("\n"))
|
||
|
if d.accept(inp_program):
|
||
|
print("YES")
|
||
|
else:
|
||
|
print("NO")
|