hash每个长度为k的串,莫队跑一遍就行了。
#include <bits/stdc++.h>
using namespace std;
#define N 210000
#define seed 23333333
#define ull unsigned long long
int n,m,K,sz,cnt;
int a[N],ans[N],pos[N];
ull has[N],pw[N],v[N],v1[N],num[N];
struct node
{
int x,y,bel;
node(){}
node(int x,int y,int bel):x(x),y(y),bel(bel){}
friend bool operator < (const node &r1,const node &r2)
{
if(r1.x/sz==r2.x/sz)return r1.y<r2.y;
return r1.x<r2.x;
};
}b[N];
int main()
{
//freopen("tt.in","r",stdin);
scanf("%d%d%d",&n,&m,&K);
pw[0]=1;sz=sqrt(n)+1;
for(int i=1;i<=n;i++)pw[i]=pw[i-1]*seed;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),has[i]=has[i-1]*seed+a[i];
for(int i=1;i+K-1<=n;i++)
v1[i]=v[i]=has[i+K-1]-has[i-1]*pw[K];
sort(v1+1,v1+1+n-K+1);
for(int i=1;i+K-1<=n;i++)
v[i]=lower_bound(v1+1,v1+1+n-K+1,v[i])-v1;
for(int i=1,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
ull t=0;
for(int j=1,x1;j<=K;j++)
scanf("%d",&x1),t=t*seed+x1;
pos[i]=lower_bound(v1+1,v1+1+n-K+1,t)-v1;
if(v1[pos[i]]!=t||y-K+1<x)continue;
else b[++cnt]=node(x,y-K+1,i);
}
sort(b+1,b+1+cnt);
for(int i=1,l=1,r=0;i<=cnt;i++)
{
while(l<b[i].x)num[v[l]]--,l++;
while(l>b[i].x)l--,num[v[l]]++;
while(r<b[i].y)r++,num[v[r]]++;
while(r>b[i].y)num[v[r]]--,r--;
ans[b[i].bel]=num[pos[b[i].bel]];
}
for(int i=1;i<=m;i++)
puts(ans[i] ? "No":"Yes");
return 0;
}

本文介绍了一种利用莫队算法结合Hash技术解决特定字符串匹配问题的方法。通过预处理长度为k的子串并使用Hash函数,实现了快速查找与匹配。代码中详细展示了如何构建Hash值、处理查询请求及进行区间更新,适用于处理大量查询的情况。
593

被折叠的 条评论
为什么被折叠?



