-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAda_and_Spring_Cleaning.cpp
More file actions
62 lines (51 loc) · 1.46 KB
/
Ada_and_Spring_Cleaning.cpp
File metadata and controls
62 lines (51 loc) · 1.46 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
#include <bits/stdc++.h>
using namespace std;
#define ff() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define int long long int
#define vi vector<int>
#define sz(a) (int)a.size()
#define nl '\n'
const int M = 1e9+9;
const int BASE1 = 5689;
const int BASE2 = 8861;
struct Hash {
vector<pair<int, int>> hashes, Pow;
Hash(string s) {
hashes.assign(s.size() + 1, make_pair(0, 0));
Pow.assign(s.size() + 1, make_pair(1, 1));
for (int i = 0; i < s.size(); i++) {
hashes[i + 1] = make_pair((hashes[i].first * BASE1 + (s[i] - 'a' + 1)) % M,
(hashes[i].second * BASE2 + (s[i] - 'a' + 1)) % M);
Pow[i + 1] = make_pair((Pow[i].first * BASE1) % M,
(Pow[i].second * BASE2) % M);
}
}
pair<int, int> get(int l, int r) {
l++, r++;
int hash1 = (hashes[r].first - (hashes[l - 1].first * Pow[r - l + 1].first) % M + M) % M;
int hash2 = (hashes[r].second - (hashes[l - 1].second * Pow[r - l + 1].second) % M + M) % M;
return make_pair(hash1, hash2);
}
};
void solve() {
int n,k;
cin>>n>>k;
string s;
cin>>s;
Hash a(s);
set<pair<int,int>>st;
for(int i=0; i+k-1 < n; i++){
auto var = a.get(i,i+k-1);
st.insert(var);
}
cout<<st.size()<<nl;
}
int32_t main() {
ff();
int tc;
cin>>tc;
while (tc--) {
solve();
}
return 0;
}