Advertisement
Guest User

Pajton

a guest
Dec 18th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. #!/usr/bin/python2
  2. import re
  3. import sys
  4. import string
  5.  
  6. akceptacja = []
  7. hashMap = {}
  8.  
  9. sposob = 0
  10.  
  11. for i in range (0,5000):
  12.     hashMap[str(i)] = {}   
  13.     for j in range(0,26):
  14.         hashMap[str(i)][string.ascii_lowercase[j]] = []
  15.         hashMap[str(i)][str(j)] = []
  16.  
  17. a = sys.stdin
  18. with a as file:
  19.     for line in file:
  20.         splitTable = line.replace("\n", "").split(" ");
  21.         if len(splitTable) == 1:
  22.             akceptacja.append(splitTable[0])
  23.         else:
  24.             if splitTable[0] != '#':
  25.                 if hashMap[splitTable[0]][splitTable[2]]:
  26.                     sposob = 1
  27.                     hashMap[splitTable[0]][splitTable[2]].append(splitTable[1])
  28.                 else:
  29.                     hashMap[splitTable[0]][splitTable[2]].append(splitTable[1])
  30.  
  31. #Sprawdzamy automatem czy tekst jest dobry
  32. #Otwarcie pliku
  33. file_name = sys.argv[1]
  34. fp = open(file_name)
  35. content = fp.readlines()
  36.  
  37. def czyStanAkceptujacy(stan): #Czy stan jest stanem akceptujacym
  38.     akceptuje = 0
  39.     for stany in akceptacja:
  40.         if stan == stany:
  41.             akceptuje = 1
  42.     if akceptuje == 1:
  43.         return True
  44.     else:
  45.         return False
  46.  
  47. def Akceptuj_OD(napis, stan): #Rekurencja
  48.     if not napis:
  49.         if czyStanAkceptujacy(stan):
  50.             return True
  51.         else:
  52.             return False
  53.     c = napis[0]
  54.     for przejscia in hashMap[str(stan)][c]:
  55.         accepted = 0
  56.         if Akceptuj_OD(napis[1:], przejscia):
  57.             accepted = 1
  58.             #return True
  59.         if accepted == 1:
  60.             return True
  61.        
  62.    
  63.  
  64. def Akceptuj(napis):#Czy napis jest akceptowany
  65.     return Akceptuj_OD(napis, 0)
  66.  
  67.  
  68. if sposob == 1:#NIEDETERMINISTYCZNY SPOSOB
  69.     for word in content:
  70.         if Akceptuj(word.replace('\n','')):
  71.             print "YES " + word,
  72.         else:
  73.             print "NO " + word,
  74. else:#DETERMINISTYCZNY. UNIKAMY REKURENCJI BY NIE PRZEKROCZYC STOSU
  75.     for word in content:
  76.         stan = 0
  77.         for letter in word:
  78.             if letter != "\n" and letter != '0':
  79.                 x = len(hashMap[str(stan)][letter])
  80.                 if x > 1 or x == 0:
  81.                         stan = -1
  82.                         break
  83.                 else:
  84.                     stan = hashMap[str(stan)][letter][0]
  85.         przechodzi = 0
  86.         for stany in akceptacja:
  87.             if str(stany) == str(stan):
  88.                 przechodzi = 1
  89.                 break
  90.             else:
  91.                 przechodzi = 0
  92.  
  93.         if przechodzi == 1:
  94.             print "YES " + word,
  95.         else:
  96.             print "NO " + word,
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement