jfz-2023-s473579/TaskC00/run.py
2023-11-19 17:38:19 +01:00

60 lines
1.8 KiB
Python

import sys
sys.setrecursionlimit(5000)
def write_answer(answer):
with open(output_file, 'a') as file:
file.write(answer+'\n')
def line_checking(row, position):
stack = [(row, position)]
while stack:
help_row, help_position = stack.pop()
if help_row == '\n':
if help_position in what_is_the_ended_positions():
return True
else:
return False
next_character_array = find_next_position(help_position, help_row[0])
if next_character_array:
for element_in_next_position_array in next_character_array:
stack.append((help_row[1:], element_in_next_position_array))
return False
def find_next_position(position, character):
with open(used_table, 'r') as readed_used_table:
take_all_possible_positions = []
for row_used_table in readed_used_table:
line = row_used_table.strip().split('\t')
if len(line) != 1:
if position == line[0] and character == line[2]:
take_all_possible_positions.append(line[1])
return take_all_possible_positions
def what_is_the_ended_positions():
array = []
with open(used_table, 'r') as file:
for row in file:
line = row.strip().split('\t')
if len(line) == 1:
array += line
return array
# used_table = 'testnfa.arg'
# input_file = 'testnfa.in'
# output_file = 'testnfa.out'
used_table = sys.argv[1]
input_file = sys.argv[2]
output_file = sys.argv[3]
with open(output_file, 'w') as readed_output_file:
with open(input_file, 'r') as readed_input_file:
for row_input_file in readed_input_file:
if line_checking(row_input_file, '0'):
write_answer('YES')
else:
write_answer('NO')