-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchains.py
53 lines (43 loc) · 1.07 KB
/
chains.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
class Node:
def __init__(self, data, next_element=None):
self.data = data
self.next_element = next_element
def hashing(string, m):
p = 1000000007
x = 263
tmp_sum = sum([(ord(element) * x ** k) % p for k, element in enumerate(string)])
return tmp_sum % p % m
def add(string, ht, m):
h = hashing(string, m)
cell = ht[h]
node = Node(string)
if not cell:
ht[h] = node
else:
node.next_element = cell
ht[h] = node
def find(string, ht, m):
result = 'no'
h = hashing(string, m)
cell = ht[h]
if not cell:
return result
while cell:
if cell.data == string:
result = 'yes'
return result
cell = cell.next_element
return result
def main():
m = int(input())
ht = [None for _ in range(m)]
n = int(input())
for i in range(n):
command, arg = input().split()
if command == 'add':
add(arg, ht, m)
elif command == 'find':
find(arg, ht, m)
else:
print('bad command')
main()