-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash_chain.py
80 lines (70 loc) · 2.59 KB
/
hash_chain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
class Query:
def __init__(self, query):
self.type = query[0]
if self.type == 'check':
self.ind = int(query[1])
else:
self.s = query[1]
class QueryProcessor:
_multiplier = 263
_prime = 1000000007
def __init__(self, bucket_count):
self.bucket_count = bucket_count
# store all strings in one list
self.elems = []
self.hashTable =[[] for _ in range(self.bucket_count)]
def _hash_func(self, s):
ans = 0
for c in reversed(s):
ans = (ans * self._multiplier + ord(c)) % self._prime
return ans % self.bucket_count
def write_search_result(self, was_found):
print('yes' if was_found else 'no')
def write_chain(self, chain):
print(' '.join(chain))
def read_query(self):
return Query(input().split())
def process_query(self, query):
if query.type == "check":
# use reverse order, because we append strings to the end
self.write_chain(cur for cur in reversed(self.elems)
if self._hash_func(cur) == query.ind)
else:
try:
ind = self.elems.index(query.s)
except ValueError:
ind = -1
if query.type == 'find':
self.write_search_result(ind != -1)
elif query.type == 'add':
if ind == -1:
self.elems.append(query.s)
else:
if ind != -1:
self.elems.pop(ind)
def process_query_fast(self, query):
if query.type == "check":
# use reverse order, because we append strings to the end
self.write_chain(cur for cur in reversed(self.hashTable[query.ind]))
#if self._hash_func(cur) == query.ind)
else:
try:
ind = self.hashTable[self._hash_func(query.s)].index(query.s)
except ValueError:
ind = -1
if query.type == 'find':
self.write_search_result(ind != -1)
elif query.type == 'add':
if ind == -1:
self.hashTable[self._hash_func(query.s)].append(query.s)
else:
if ind != -1:
self.hashTable[self._hash_func(query.s)].pop(ind)
def process_queries(self):
n = int(input())
for _ in range(n):
self.process_query_fast(self.read_query())
if __name__ == '__main__':
bucket_count = int(input())
proc = QueryProcessor(bucket_count)
proc.process_queries()