Advertisement
Guest User

Untitled

a guest
Jun 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. """
  2. 5
  3. 12
  4. add world
  5. add HellO
  6. check 4
  7. find World
  8. find world
  9. del world
  10. check 4
  11. del HellO
  12. add luck
  13. add GooD
  14. check 2
  15. del good
  16.  
  17.  
  18.  
  19. 4
  20. 8
  21. add test
  22. add test
  23. find test
  24. del test
  25. find test
  26. find Test
  27. add Test
  28. find Test
  29.  
  30.  
  31. """
  32. from math import pow
  33.  
  34. x = 263
  35. p = 1000000007
  36.  
  37. def hash_func(s):
  38. ss = 0
  39. for i in range(0, len(s)):
  40. o = ord(s[i])
  41. pp = pow(x, i)
  42. ss += o * pp
  43.  
  44. return int((ss % p) % m)
  45.  
  46.  
  47. m = int(input())
  48. n = int(input())
  49.  
  50.  
  51. class hash_tab:
  52. def __init__(self, m):
  53. self.table = [None] * m
  54. for i in range(0, m):
  55. self.table[i] = []
  56.  
  57. def add(self, s):
  58. i = hash_func(s)
  59. for e in self.table[i]:
  60. if e == s:
  61. return
  62. self.table[i].append(s)
  63.  
  64. def delete(self, s):
  65. i = hash_func(s)
  66. try:
  67. self.table[i].remove(s)
  68. except:
  69. pass
  70.  
  71. def find(self, s):
  72. i = hash_func(s)
  73. for e in self.table[i]:
  74. if e == s:
  75. return True
  76. return False
  77.  
  78. def check(self, i):
  79. return self.table[i]
  80.  
  81.  
  82. tab = hash_tab(m)
  83.  
  84. for i in range(0, n):
  85. parts = input().strip().split(" ")
  86. s = parts[1]
  87. a = parts[0]
  88. if a == "add":
  89. tab.add(s)
  90. elif a == "check":
  91. l = tab.check(int(s))
  92. print(" ".join(reversed(l)))
  93. elif a == "del":
  94. tab.delete(s)
  95. elif a == "find":
  96. if tab.find(s):
  97. print("yes")
  98. else:
  99. print("no")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement