Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python2
- import re
- import sys
- import string
- akceptacja = []
- hashMap = {}
- sposob = 0
- for i in range (0,5000):
- hashMap[str(i)] = {}
- for j in range(0,26):
- hashMap[str(i)][string.ascii_lowercase[j]] = []
- hashMap[str(i)][str(j)] = []
- a = sys.stdin
- with a as file:
- for line in file:
- splitTable = line.replace("\n", "").split(" ");
- if len(splitTable) == 1:
- akceptacja.append(splitTable[0])
- else:
- if splitTable[0] != '#':
- if hashMap[splitTable[0]][splitTable[2]]:
- sposob = 1
- hashMap[splitTable[0]][splitTable[2]].append(splitTable[1])
- else:
- hashMap[splitTable[0]][splitTable[2]].append(splitTable[1])
- #Sprawdzamy automatem czy tekst jest dobry
- #Otwarcie pliku
- file_name = sys.argv[1]
- fp = open(file_name)
- content = fp.readlines()
- def czyStanAkceptujacy(stan): #Czy stan jest stanem akceptujacym
- akceptuje = 0
- for stany in akceptacja:
- if stan == stany:
- akceptuje = 1
- if akceptuje == 1:
- return True
- else:
- return False
- def Akceptuj_OD(napis, stan): #Rekurencja
- if not napis:
- if czyStanAkceptujacy(stan):
- return True
- else:
- return False
- c = napis[0]
- for przejscia in hashMap[str(stan)][c]:
- accepted = 0
- if Akceptuj_OD(napis[1:], przejscia):
- accepted = 1
- #return True
- if accepted == 1:
- return True
- def Akceptuj(napis):#Czy napis jest akceptowany
- return Akceptuj_OD(napis, 0)
- if sposob == 1:#NIEDETERMINISTYCZNY SPOSOB
- for word in content:
- if Akceptuj(word.replace('\n','')):
- print "YES " + word,
- else:
- print "NO " + word,
- else:#DETERMINISTYCZNY. UNIKAMY REKURENCJI BY NIE PRZEKROCZYC STOSU
- for word in content:
- stan = 0
- for letter in word:
- if letter != "\n" and letter != '0':
- x = len(hashMap[str(stan)][letter])
- if x > 1 or x == 0:
- stan = -1
- break
- else:
- stan = hashMap[str(stan)][letter][0]
- przechodzi = 0
- for stany in akceptacja:
- if str(stany) == str(stan):
- przechodzi = 1
- break
- else:
- przechodzi = 0
- if przechodzi == 1:
- print "YES " + word,
- else:
- print "NO " + word,
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement