1. 程式人生 > >[TJOI2013]單詞

[TJOI2013]單詞

unsigned har -h cstring for 字符 code div cto

字符串hash

#include<cstdio>
#include<vector>
#include<string>
#include<iostream>
#include<cstring>
typedef unsigned long long u64;
int n,m;
int ans[210],len[210];
u64 p[210];
const int maxn = 1e6 + 1000;
u64 hsh[maxn],Exp[maxn];
char buf[maxn];
int tot;
char tmp[maxn];
inline u64 gethash(
int l,int r){ return hsh[r]-hsh[l-1]*Exp[r-l+1]; } int main(){ std::ios::sync_with_stdio(false),std::cin.tie(0); std::cin >> n; for(int i=1;i<=n;++i){ std::cin >> tmp; u64 p=0; for(int i=0;tmp[i];++i) p=p*2333+tmp[i],buf[++tot]=tmp[i]; buf[
++tot]=$; len[i]=strlen(tmp); ::p[i]=p; } Exp[0]=1; for(int i=1;i<maxn;++i)Exp[i]=Exp[i-1]*2333; u64 pp=0; for(int i=1;i<=tot;++i) hsh[i]=pp=pp*2333+buf[i]; for(int i=1;i<=tot;++i) for(int j=1;j<=n;++j) if(i >= len[j] && gethash(i-len[j]+1
,i) == p[j]) ++ans[j]; for(int i=1;i<=n;++i) std::cout << ans[i] << \n; }

[TJOI2013]單詞