# 【bzoj2780】[Spoj]8093 Sevenk Love Oimaster 广义后缀自动机

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?"

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

For each question, output the answer in one line.

3 3
abcabcabc
aaa
aafe
abc
a
ca

1
3
1

#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 400010
using namespace std;
int next[N][26] , fa[N] , dis[N] , vis[N] , si[N] , last , tot = 1;
char str[N];
void insert(int c , int t)
{
int p = last , np = last = ++tot;
dis[np] = dis[p] + 1;
while(p && !next[p][c]) next[p][c] = np , p = fa[p];
if(!p) fa[np] = 1;
else
{
int q = next[p][c];
if(dis[q] == dis[p] + 1) fa[np] = q;
else
{
int nq = ++tot;
memcpy(next[nq] , next[q] , sizeof(next[q])) , si[nq] = si[q] , vis[nq] = vis[q] , dis[nq] = dis[p] + 1 , fa[nq] = fa[q] , fa[np] = fa[q] = nq;
while(p && next[p][c] == q) next[p][c] = nq , p = fa[p];
}
}
for(p = np ; p && vis[p] != t ; p = fa[p]) vis[p] = t , si[p] ++ ;
}
int main()
{
int n , m , i , j , l;
scanf("%d%d" , &n , &m);
for(i = 1 ; i <= n ; i ++ )
{
scanf("%s" , str) , l = strlen(str);
for(j = 0 , last = 1 ; j < l ; j ++ ) insert(str[j] - 'a' , i);
}
while(m -- )
{
scanf("%s" , str) , l = strlen(str);
for(i = 0 , j = 1 ; i < l ; i ++ ) j = next[j][str[i] - 'a'];
printf("%d\n" , si[j]);
}
return 0;
}

posted @ 2017-07-04 08:20  GXZlegend  阅读(400)  评论(0编辑  收藏  举报