Advertisement
alex0sunny

polynomial hash

Sep 6th, 2021
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.20 KB | None | 0 0
  1. a = int(input())
  2. m = int(input())
  3. s = input()
  4.  
  5.  
  6. def get_hash(a, m, s):
  7.     res = 0
  8.     x = 1
  9.     for sym in reversed(s):
  10.         res = (res + ord(sym) * x) % m
  11.         x *= a
  12.     return res
  13.  
  14.  
  15. print(get_hash(a, m, s))
  16.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement