Advertisement
Morass

Spring-songman

Sep 9th, 2016
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. using LINT = long long int;
  5. using PII = pair<int,int>;
  6.  
  7. #define PB push_back
  8. #define FI first
  9. #define SE second
  10. #define REP(i,n) for(int i=0;i<(n);++i)
  11. #define FOR(i, a, b) for(int i=(a);i<(b);++i)
  12.  
  13. int n,k;
  14. string s;
  15.  
  16. LINT LEMOD=1000000007;
  17. LINT ALPH=31;
  18. LINT ALPH2=49;
  19.  
  20. void process(){
  21.     cin>>n>>k>>s;s+='$';
  22.     unordered_map<int,unordered_set<int>> hashmap;
  23.     LINT hash=0;
  24.     LINT hash2=0;
  25.     REP(i,k){
  26.         hash = (hash*ALPH+s[i]-'a') % LEMOD;
  27.         hash2 = (hash2*ALPH2+s[i]-'a') % LEMOD;
  28.     }
  29.  
  30.     LINT ALPHK = 1;
  31.     LINT ALPH2K = 1;
  32.     REP(i,k-1){
  33.         ALPHK = ALPHK*ALPH%LEMOD;
  34.         ALPH2K = ALPH2K*ALPH2%LEMOD;
  35.     }
  36.  
  37.     REP(i,n-k+1){
  38.         hashmap[hash].insert(hash2);
  39.         hash = ((hash-ALPHK*(s[i]-'a')+2*LEMOD)%LEMOD*ALPH+s[i+k]-'a')%LEMOD;
  40.         hash2 = ((hash2-ALPH2K*(s[i]-'a')+2*LEMOD)%LEMOD*ALPH2+s[i+k]-'a')%LEMOD;
  41.     }
  42.  
  43.     int cnt = 0;
  44.     for(auto & p : hashmap)
  45.         cnt += p.SE.size();
  46.     cout<<cnt<<endl;
  47. }
  48.  
  49. int main() {
  50.     int t;cin>>t;
  51.     while(t--)process();
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement