jfz-2023-s473564/TaskC01/run.py

42 lines
1.1 KiB
Python
Raw Normal View History

2023-11-25 21:13:35 +01:00
import sys
def load_data(file_path):
nfa = {}
with open(file_path, 'r') as file:
for line in file:
split_data = line.strip().split(' ')
if len(split_data) == 3:
state, next_state, symbol = split_data
key = (int(state), symbol)
if key in nfa:
nfa[key].add(int(next_state))
else:
nfa[key] = {int(next_state)}
return nfa
def is_accepted(nfa, input, current_states, accepting_states):
for symbol in input:
new_states = set()
for state in current_states:
key = (state, symbol)
if key in nfa:
new_states.update(nfa[key])
current_states = new_states
return any(state in accepting_states for state in current_states)
# Set of accepting states
accepting_states = {2}
nfa_file = sys.argv[1]
nfa = load_data(nfa_file)
for line in sys.stdin:
input_str = line.strip()
start_states = {0}
if is_accepted(nfa, input_str, start_states, accepting_states):
print("YES")
else:
print("NO")