djfz-2023-s464933/TaskC00/run.py

58 lines
1.4 KiB
Python
Raw Normal View History

2023-11-19 22:42:11 +01:00
import sys
class FSA:
def __init__(self,):
self.initial_state = '0'
self.final_states = set()
self.transitions = dict()
self.alphabet = set()
def add_transition(self, state_from, state_to, symbol):
if state_from in self.transitions.keys():
if symbol not in self.transitions[state_from].keys():
self.transitions[state_from][symbol] = {state_to}
else:
self.transitions[state_from][symbol] |= {state_to}
else:
self.transitions[state_from] = dict()
self.transitions[state_from][symbol] = {state_to}
2024-01-03 19:19:38 +01:00
def get_final_state(self, string):
2023-11-19 22:42:11 +01:00
2024-01-03 19:19:38 +01:00
current_state = self.initial_state
for symbol in string:
current_state = self.transitions[current_state][symbol]
return current_state
2023-11-19 22:42:11 +01:00
def add_final_state(self, state):
self.final_states.add(state)
2024-01-03 19:19:38 +01:00
def accepts(self, string):
2023-11-19 22:42:11 +01:00
2024-01-03 19:19:38 +01:00
if self.get_final_state(string) in self.final_states:
return True
else:
return False
2023-11-19 22:42:11 +01:00
fsa = FSA()
table = open(sys.argv[1])
for line in table:
line = line.rstrip()
if len(line.split('\t')) == 3:
a, b, c = line.split('\t')
fsa.add_transition(a, b, c)
fsa.alphabet.add(c)
else:
fsa.add_final_state(line)
for line in sys.stdin:
line = line.rstrip()
if fsa.accepts(line):
print('YES')
else:
2023-12-17 21:57:42 +01:00
print('NO')