fork(5) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef signed long long ll;
  5. const ll p = 11;
  6. const ll mod = 1e9 + 7;
  7. const int MX = 1e6 + 5;
  8. int t, n, k;
  9. ll _hash[MX], P[MX];
  10. char str[MX];
  11. string tmp;
  12. map<ll, int > H;
  13.  
  14.  
  15. ll get_hash(int st, int len){
  16. ll Ans = (_hash[st + len - 1] - (_hash[st - 1] * P[len]) % mod) % mod;
  17. if(Ans <= 0) Ans += mod;
  18. return Ans;
  19. }
  20.  
  21. int main() {
  22. P[0] = 1;
  23. for(int i = 1; i < MX; P[i] = (P[i - 1] * p) % mod, i++);
  24. scanf("%d", &t);
  25.  
  26. for(int i = 1; i <= t; i++){
  27. scanf("%d %d %s", &n, &k, str);
  28.  
  29. _hash[0] = (ll)str[0]; H.clear();
  30. for(int j = 1; j < n; j++){
  31. _hash[j] = ((_hash[j - 1] * p) % mod + str[j]) % mod;
  32. }
  33.  
  34. H[_hash[k - 1]]++;
  35. for(int j = 1; j <= n - k; j++){
  36. ll h = get_hash(j, k);
  37. H[h]++;
  38. }
  39.  
  40. printf("%d\n", H.size());
  41. }
  42.  
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0.01s 20080KB
stdin
5
3 2
aaa
5 1
abcba
4 2
abac
10 2
abbaaaabba
7 3
dogodog
stdout
1
3
3
4
4