import re import sys atandt_desc_pattern = re.compile(r"^([0-9]+) ([0-9]+) (\S+)( [0-9]+\.[0-9]+)?$") atandt_accepted_state_pattern = re.compile(r"^[0-9]+$") current_state = 0 accept_states = [] state_move = {} for line in sys.stdin: line = line.replace('\n', '') move = atandt_desc_pattern.match(line) succes = atandt_accepted_state_pattern.match(line) if move: if move.group(4): state_move[(int(move.group(1)), move.group(3))] = (int(move.group(2)), float(move.group(4))) else: state_move[(int(move.group(1)), move.group(3))] = (int(move.group(2)), 0) if succes: accept_states.append(int(line)) with open(sys.argv[1], 'r', encoding='utf-8') as f: for word in f: current_state = 0 word = word.replace('\n', '') character_number = 0 while character_number < len(word): if ((current_state, word[character_number]) in state_move): current_state = state_move[(current_state, word[character_number])][1] character_number += 1 elif (current_state, '') in state_move): current_state = state_move[(current_state, '')][0] else: current_state = -1 character_number += 1 while ((current_state, '') in state_move): current_state = state_move[(current_state, '')][0] if (current_state in accept_states): print("YES " + word) else: print("NO" + word)