Advertisement
MystMe

Untitled

Dec 24th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.02 KB | None | 0 0
  1. import Source
  2. from Source import solve as solve
  3.  
  4.  
  5. def main():
  6.     regexp = input()
  7.     ch = input()
  8.     length = int(input())
  9.     stack1 = []
  10.     machine_stack = []
  11.     for c in regexp:
  12.         if c == 'a' or c == 'b' or c == 'c' or c == '1':
  13.             stack1.append(c)
  14.             main_fsm = Source.FSMachine(c)
  15.             machine_stack.append(Source.FSMachine(c))
  16.         elif c == '+':
  17.             if len(stack1) < 2:
  18.                 raise Exception('Invalid regexp')
  19.             else:
  20.                 first = stack1.pop(-1)
  21.                 second = stack1.pop(-1)
  22.                 stack1.append('('+second+'+'+first+')')
  23.                 first_FSMachine = machine_stack.pop(-1)
  24.                 second_FSMachine = machine_stack.pop(-1)
  25.                 machine_stack.append(second_FSMachine.unionMachines(first_FSMachine))
  26.         elif c == '.':
  27.             if len(stack1) < 2:
  28.                 raise Exception('Invalid regexp')
  29.             else:
  30.                 first = stack1.pop(-1)
  31.                 second = stack1.pop(-1)
  32.                 stack1.append(second + first)
  33.                 first_FSMachine = machine_stack.pop(-1)
  34.                 second_FSMachine = machine_stack.pop(-1)
  35.                 machine_stack.append(second_FSMachine.crossingMachines(first_FSMachine))
  36.         else:
  37.             if len(stack1) < 1:
  38.                 raise Exception('Invalid regexp')
  39.             else:
  40.                 last = stack1.pop(-1)
  41.                 if last[-1] == '*' and last[0] == '(':
  42.                     stack1.append(last)
  43.                 else:
  44.                     stack1.append('(' + last + ')*')
  45.                     last_FSMachine = machine_stack.pop(-1)
  46.                     machine_stack.append(last_FSMachine.kleene())
  47.  
  48.     if len(stack1) > 1:
  49.         raise Exception('Invalid regexp')
  50.     FSMachine = machine_stack.pop(0)
  51.     FSMachine.fix_epsilon()
  52.     answer = solve(FSMachine, ch, length)
  53.     if answer == -1:
  54.         print('INF')
  55.     else:
  56.         print(answer)
  57.  
  58.  
  59. if __name__ == '__main__':
  60.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement