2019 上海网络赛G 手写哈希map+字符串hash
https://nanti.jisuanke.com/t/41415
因为对于询问$\sum |s|<=1e5$,因此$|s|$的种类数$<=\sqrt{1e5}$
我们分组标记,就变成了$\sqrt{1e5}$次询问了,我们暴力去跑,
复杂度$1e5\sqrt{1e5}*hashmap$
暴力+哈希+$unordermap$计数,嗯,果然TLE了
然后手写一个$hashmap$,跑的飞快...
#include<bits/stdc++.h> #define ull unsigned long long #define fi first #define se second #define mp make_pair #define pii pair<ull,int> #define rep(ii,a,b) for(int ii=a;ii<=b;++ii) using namespace std; const int maxn=1e5+10,maxm=3e5+17; const ull mod=1e9+7; int casn,n,m; char a[maxn],b[maxn]; ull pw[200]; int len[20010],ans[20010]; vector<pii> ask[maxn]; class hash_map{public: struct node{ull u;int v,next;}e[maxm<<1]; int head[maxm],nume,numk,id[maxm]; int& operator[](ull u) { int hs=u%maxm; for(int i=head[hs];i;i=e[i].next) if(e[i].u==u) return e[i].v; if(!head[hs])id[++numk]=hs; return e[++nume]=(node){u,0,head[hs]},head[hs]=nume,e[nume].v; } void clear(){ rep(i,0,numk)head[id[i]]=0; numk=nume=0; } }cnt; int main(){ pw[0]=1;rep(i,1,131) pw[i]=pw[i-1]*mod; ull base1=pw[130],base2=pw[131]; cin>>casn; while(casn--){ cin>>(a+1)>>m; int n=strlen(a+1); rep(i,1,m){ cin>>(b+1); len[i]=strlen(b+1); ull hs=base1*b[1]+base2*b[len[i]]; rep(j,2,len[i]-1) hs+=pw[b[j]]; ask[len[i]].emplace_back(mp(hs,i)); } int k=unique(len+1,len+1+m)-len-1; rep(i,1,k){ int l=len[i]; ull hs=0; rep(j,2,l-1) hs+=pw[a[j]]; rep(j,1,n-l+1){ ++cnt[hs+base1*a[j]+base2*a[j+l-1]]; hs+=pw[a[j+l-1]]-pw[a[j+1]]; } for(auto &j:ask[l]) ans[j.se]+=cnt[j.fi]; cnt.clear(); ask[l].clear(); } rep(i,1,m) { cout<<ans[i]<<'\n'; ans[i]=0; } } }