-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1-roling-hash.cpp
58 lines (41 loc) · 1.27 KB
/
1-roling-hash.cpp
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
#include <bits/stdc++.h>
using namespace std;
static const int MOD = 1000000000 + 7; // 10^9 + 7 (prime number)
static const int BASE = 257; // prime number
static const int MAX_SIZE = 100001;
long long base_powers[MAX_SIZE];
void get_powers(int to) {
base_powers[0] = 1;
for (int i = 1; i <= to; i++) {
base_powers[i] = (base_powers[i - 1] * BASE) % MOD;
}
}
long long get_hash(string word) {
long long result = 0;
for (int i = 0; i < word.size(); i++) {
result = (result + word[i] * base_powers[i]) % MOD;
}
return result;
}
int rolling_hash(string text, string word) {
get_powers(word.size());
long long word_hash = get_hash(word);
long long cur_hash = get_hash(text.substr(0, word.size()));
int occurrences = (cur_hash == word_hash);
for (int i = word.size(); i < text.size(); i++) {
cur_hash -= (text[i - word.size()] * base_powers[word.size() - 1]) % MOD;
cur_hash = ((cur_hash * BASE) % MOD + text[i]) % MOD;
occurrences += (cur_hash == word_hash);
}
return occurrences;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
string text;
string word;
cin >> text;
cin >> word;
cout << rolling_hash(text, word) << "\n";
return 0;
}