题解|#lanqiao 斤斤计较的小z# 字符串Hash模板

字符串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;
}
全部评论

相关推荐

12-15 18:00
巨人网络_招聘
投递巨人网络等公司6个岗位
点赞 评论 收藏
分享
12-15 14:16
门头沟学院 Java
回家当保安:发offer的时候会背调学信网,最好不要这样。 “27届 ”和“28届以下 ”公司招聘的预期是不一样的。
实习简历求拷打
点赞 评论 收藏
分享
11-07 16:07
深圳大学 运营
前端飞升:学长,阿里不是卡双非吗,我深也能去吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务