Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Source
- from Source import solve as solve
- def main():
- regexp = input()
- ch = input()
- length = int(input())
- stack1 = []
- machine_stack = []
- for c in regexp:
- if c == 'a' or c == 'b' or c == 'c' or c == '1':
- stack1.append(c)
- main_fsm = Source.FSMachine(c)
- machine_stack.append(Source.FSMachine(c))
- elif c == '+':
- if len(stack1) < 2:
- raise Exception('Invalid regexp')
- else:
- first = stack1.pop(-1)
- second = stack1.pop(-1)
- stack1.append('('+second+'+'+first+')')
- first_FSMachine = machine_stack.pop(-1)
- second_FSMachine = machine_stack.pop(-1)
- machine_stack.append(second_FSMachine.unionMachines(first_FSMachine))
- elif c == '.':
- if len(stack1) < 2:
- raise Exception('Invalid regexp')
- else:
- first = stack1.pop(-1)
- second = stack1.pop(-1)
- stack1.append(second + first)
- first_FSMachine = machine_stack.pop(-1)
- second_FSMachine = machine_stack.pop(-1)
- machine_stack.append(second_FSMachine.crossingMachines(first_FSMachine))
- else:
- if len(stack1) < 1:
- raise Exception('Invalid regexp')
- else:
- last = stack1.pop(-1)
- if last[-1] == '*' and last[0] == '(':
- stack1.append(last)
- else:
- stack1.append('(' + last + ')*')
- last_FSMachine = machine_stack.pop(-1)
- machine_stack.append(last_FSMachine.kleene())
- if len(stack1) > 1:
- raise Exception('Invalid regexp')
- FSMachine = machine_stack.pop(0)
- FSMachine.fix_epsilon()
- answer = solve(FSMachine, ch, length)
- if answer == -1:
- print('INF')
- else:
- print(answer)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement