Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- 5
- 12
- add world
- add HellO
- check 4
- find World
- find world
- del world
- check 4
- del HellO
- add luck
- add GooD
- check 2
- del good
- 4
- 8
- add test
- add test
- find test
- del test
- find test
- find Test
- add Test
- find Test
- """
- from math import pow
- x = 263
- p = 1000000007
- def hash_func(s, m):
- ss = 0
- for i in range(0, len(s)):
- o = ord(s[i])
- pp = pow(x, i)
- ss += o * pp
- return int((ss % p) % m)
- class hash_tab:
- def __init__(self, m):
- self.m = m
- self.table = [None] * m
- for i in range(0, m):
- self.table[i] = []
- def add(self, s):
- i = hash_func(s, self.m)
- for e in self.table[i]:
- if e == s:
- return
- self.table[i].append(s)
- def delete(self, s):
- i = hash_func(s, self.m)
- try:
- self.table[i].remove(s)
- except:
- pass
- def find(self, s):
- i = hash_func(s, self.m)
- for e in self.table[i]:
- if e == s:
- return True
- return False
- def check(self, i):
- return self.table[i]
- m = int(input())
- n = int(input())
- tab = hash_tab(m)
- for i in range(0, n):
- parts = input().strip().split(" ")
- s = parts[1]
- a = parts[0]
- if a == "add":
- tab.add(s)
- elif a == "check":
- l = tab.check(int(s))
- print(" ".join(reversed(l)))
- elif a == "del":
- tab.delete(s)
- elif a == "find":
- if tab.find(s):
- print("yes")
- else:
- print("no")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement