-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHASH_FUNCTION_STRING.py
More file actions
32 lines (31 loc) · 898 Bytes
/
HASH_FUNCTION_STRING.py
File metadata and controls
32 lines (31 loc) · 898 Bytes
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
"""
Given a string s[1…k] which is a sequence of characters taken from {‘a', . . ., ‘z'}.Given a positive integer m, the hash code of s is defined by the formula:
H(s) = (s[1]*256^(k-1) + s[2]*256^(k-2) + . . . + s[k]*256^0 ) mod m (the contant integer m is a parameter)
Given a sequence of strings k1, k2, …, kn, compute the corresponding hash codes
Input
Line 1: n and m (1 <= n,m <= 100000)
Line i+1 (i = 1,2,…,n): contains the string ki (the length of each string is less than or equal to 200)
Output
Each line contains the corresponding hash code of n given strings
Example
Input:
4 1000
a
ab
abc
abcd
Output:
97
930
179
924
"""
n, m = [int(i) for i in input().split()]
for i in range(n):
p = input()
p = [*p]
s = 0
k = len(p)
for i in range(k):
s += ord(p[i])*256**(k-i-1)%m
print(s%m)