-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf.py
More file actions
72 lines (49 loc) · 1.79 KB
/
Copy pathf.py
File metadata and controls
72 lines (49 loc) · 1.79 KB
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
from __future__ import annotations
import sys
from collections.abc import Iterable, Sequence
class SubstringHashTool:
string: str
a: int
m: int
_prefix_hashes: Sequence[int]
_a_powers: Sequence[int]
def __init__(self, string: str, *, a: int, m: int) -> None:
self.string = string
self.a = a
self.m = m
self._prefix_hashes = self._calculate_prefix_hashes()
self._a_powers = self._calculate_a_powers()
def _calculate_prefix_hashes(self) -> Sequence[int]:
result: list[int] = []
hash_value = 0
result.append(hash_value)
for char in self.string:
hash_value = (hash_value * self.a + ord(char)) % self.m
result.append(hash_value)
return result
def _calculate_a_powers(self) -> Sequence[int]:
result: list[int] = []
a_power = 1
result.append(a_power)
for i in range(len(self.string)):
a_power = (a_power * self.a) % self.m
result.append(a_power)
return result
def get_hash(self, start: int, end: int) -> int:
return (self._prefix_hashes[end] - self._prefix_hashes[start] * self._a_powers[end - start]) % self.m
def read_slices(count: int) -> Iterable[tuple[int, int]]:
for i in range(count):
start, end = map(int, input().strip().split())
yield start - 1, end
def main() -> None:
a = int(input().strip())
m = int(input().strip())
string = sys.stdin.readline().strip()
slices_count = int(input().strip())
slices_iter = read_slices(slices_count)
substring_hash_tool = SubstringHashTool(string, a=a, m=m)
for start, end in slices_iter:
hash_value = substring_hash_tool.get_hash(start, end)
print(hash_value)
if __name__ == '__main__':
main()