字符串Hash模板

#include<bits/stdc++.h>
using namespace std;
const int M=1e6+5;
typedef unsigned long long ull;
char s1[M],s2[M];
ull hs[M];
ull b[M];
const ull base=131;
ull getHash(int l,int r){
  return hs[r]-hs[l-1]*b[r-l+1];
}
int main(){
  cin>>s1+1;
  cin>>s2+1;
  int m=strlen(s1+1);
  int n=strlen(s2+1);
  b[0]=1;
  for(int i=1;i<=n;i++){
    b[i]=b[i-1]*base;
    hs[i]=hs[i-1]*base+(s2[i]-'A'+1);
  }
  int ans=0;
  for(int i=1;i+m-1<=n;i++){
    if(getHash(i,i+m-1)==getHash(1,m)) ans++;
  }
  cout<<ans<<endl;
  return 0;
}