之前由于一直懒得绑定于是就没写了。。。。。。
看一下这道题,直接建广义后缀自动机,然后对于每一个新建的点把它的祖先标记一下在这个字符串中出现过就好了
开心的码完就1A了,爽! 然后一看网上的各种树状数组。。
具体看代码,基本就是一个模板的长度
#include<cstdio>
#include<cstring>
const int N=100005*2;
struct qq{int son[26],pre,step,lalal;}s[N];
int last,tot;
int n,m;
int vis[N],cnt;
void ins (int x)
{
int np=++tot,p=last;
s[np].step=s[p].step+1;
while (p!=0&&s[p].son[x]==0) s[p].son[x]=np,p=s[p].pre;
if (p==0) s[np].pre=1;
else
{
int q=s[p].son[x];
if (s[q].step==s[p].step+1) s[np].pre=q;
else
{
int nq=++tot;
s[nq]=s[q];
s[nq].step=s[p].step+1;
s[q].pre=s[np].pre=nq;
while (p!=0&&s[p].son[x]==q) s[p].son[x]=nq,p=s[p].pre;
}
}
last=np;
}
char ss[360005];
int len;
void solve ()
{
scanf("%s",ss);len=strlen(ss);
int x=1;
for (int u=0;u<len;u++)
{
int lalal=ss[u]-'a';
if (s[x].son[lalal]==0) {printf("0\n");return ;}
x=s[x].son[lalal];
}
printf("%d\n",s[x].lalal);
}
int main()
{
memset(vis,0,sizeof(vis));cnt=0;
tot=1;
scanf("%d%d",&n,&m);
for (int u=1;u<=n;u++)
{
cnt++;
int Last=tot+1;
last=1;
char ch=getchar();
while (ch<'a'||ch>'z') ch=getchar();
while (ch>='a'&&ch<='z') ins(ch-'a'),ch=getchar();
for (int i=Last+1;i<=tot;i++)
{
int x=i;
while (x!=0&&vis[x]!=cnt)
vis[x]=cnt,s[x].lalal++,x=s[x].pre;
}
}
while (m--) solve();
return 0;
}