题意
给出一个长度为 n 的串
定义
1.
2.对于每种数字 x,
题解
我们给每个数随机一个权值,两个串近似匹配可以通过哈希 O(1) 判断,然后就可以做到 O(n∗m) 了。
怎么优化呢?可以发现所有t的长度最多只可能有 ∑|t|−−−−√ 种.
然后对于每个长度,先把s的这个长度的所以子串hash值插到map里,然后进行询问。
这样复杂度是 O(n∗∑|t|−−−−√) 的。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<tr1/unordered_map>
#include<algorithm>
using namespace std;
using namespace std::tr1;
inline char gc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int getint(){
char ch=gc(); int res=0,ff=1;
while(!('0'<=ch&&ch<='9')) ch=='-'?ff=-1:0, ch=gc();
while('0'<=ch&&ch<='9') res=(res<<3)+(res<<1)+ch-'0', ch=gc();
return res*ff;
}
const int maxn=100005;
typedef unsigned long long ull;
vector<int> id[maxn];
int n,Q,ans[maxn];
ull v_rnd[maxn],q[maxn],s[maxn];
ull getrand(){ return (((((ull)rand())<<16)+(rand()<<1)+(rand()&1))<<31)+(((ull)rand())<<16)+(rand()<<1)+(rand()&1); }
unordered_map <ull,int> mp;
void Pre(int len){
mp.clear();
for(int i=len;i<=n;i++){
if(mp.find(s[i]^s[i-len])==mp.end()) mp[s[i]^s[i-len]]=0;
mp[s[i]^s[i-len]]++;
}
}
int Solve(ull x){
if(mp.find(x)==mp.end()) return 0;
return mp[x];
}
int main(){
srand(19260817);
n=getint();
for(int i=1;i<=n;i++) v_rnd[i]=getrand();
for(int i=1;i<=n;i++) s[i]=s[i-1]^(v_rnd[getint()]);
Q=getint();
for(int i=1;i<=Q;i++){
int t=getint(); id[t].push_back(i);
for(int j=1;j<=t;j++) q[i]^=v_rnd[getint()];
}
for(int i=1;i<=n;i++) if(!id[i].empty()){
int sz=id[i].size();
Pre(i);
for(int j=0;j<=sz-1;j++) ans[id[i][j]]=Solve(q[id[i][j]]);
}
for(int i=1;i<=Q;i++) printf("%d\n",ans[i]);
return 0;
}