# 【BZOJ2780】[Spoj]8093 Sevenk Love Oimaster

## Description

Oimaster and sevenk love each other.

But recently,sevenk heard that a girl named ChuYuXun was dating with oimaster.As a woman's nature, sevenk felt angry and began to check oimaster's online talk with ChuYuXun.    Oimaster talked with ChuYuXun n times, and each online talk actually is a string.Sevenk asks q questions like this,    "how many strings in oimaster's online talk contain this string as their substrings?"

## Input

There are two integers in the first line,
the number of strings n and the number of questions q.
And n lines follow, each of them is a string describing oimaster's online talk.
And q lines follow, each of them is a question.
n<=10000, q<=60000
the total length of n strings<=100000,
the total length of q question strings<=360000

## Output

For each question, output the answer in one line.

3 3
abcabcabc
aaa
aafe
abc
a
ca

## Sample Output

1
3
1

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n,m,tot;
int pre[400010],ch[400010][26],mx[400010],s[400010],vis[400010],lp[400010],rp[400010];
char str[800010];
void updata(int x,int y)
{
for(;x&&vis[x]!=y;x=pre[x])	s[x]++,vis[x]=y;
}
int extend(int x,int y,int p)
{
int np=++tot;
mx[np]=mx[p]+1;
for(;p&&!ch[p][x];p=pre[p])	ch[p][x]=np;
if(!p)	pre[np]=1;
else
{
int q=ch[p][x];
if(mx[q]==mx[p]+1)	pre[np]=q;
else
{
int nq=++tot;
pre[nq]=pre[q],pre[np]=pre[q]=nq,mx[nq]=mx[p]+1;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
for(;p&&ch[p][x]==q;p=pre[p])	ch[p][x]=nq;
}
}
return np;
}
int main()
{
scanf("%d%d",&n,&m);
int i,j,a,b;
tot=1;
for(i=1;i<=n;i++)
{
lp[i]=rp[i-1];
scanf("%s",str+lp[i]);
rp[i]=strlen(str);
for(b=1,j=lp[i];j<rp[i];j++)	b=extend(str[j]-'a',i,b);
}
for(i=1;i<=n;i++)
for(a=1,j=lp[i];j<rp[i];j++)	a=ch[a][str[j]-'a'],updata(a,i);
for(i=1;i<=m;i++)
{
scanf("%s",str);
a=strlen(str);
for(b=1,j=0;j<a;j++)
{
if(ch[b][str[j]-'a'])	b=ch[b][str[j]-'a'];
else	break;
}
if(j==a)	printf("%d\n",s[b]);
else	printf("0\n");
}
return 0;
}
posted @ 2017-06-28 09:57  CQzhangyu  阅读(686)  评论(2编辑  收藏  举报