BZOJ 2780: [Spoj]8093 Sevenk Love Oimaster 廣義後綴自動機

2780: [Spoj]8093 Sevenk Love Oimaster

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 1156  Solved: 432
[Submit][Status][Discuss]

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.

Sample Input

3 3
abcabcabc
aaa
aafe
abc
a
ca

Sample Output

1
3
1


這是下面↓題的弱化版

3473: 字符串 廣義後綴自動機


才幾天沒寫 後綴自動機都打錯了 淚

#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=10*x+ch-'0';ch=getchar();}
	return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}

const int N=400100;

struct SAM
{
	int trans[N>>1][26],par[N>>1],mx[N>>1];
	int root,suff,sz;
	
	SAM(){root=suff=sz=1;}
	
	void insert(int x)
	{
		int p=suff,np=++sz;
		mx[np]=mx[p]+1;
		while(p && !trans[p][x])
			trans[p][x]=np,p=par[p];
		if(!p) par[np]=root;
		else
		{
			int q=trans[p][x];
			if(mx[q]==mx[p]+1) par[np]=q;
			else
			{
				int nq=++sz;
                mx[nq]=mx[p]+1;num[nq]=num[q];
                memcpy(trans[nq],trans[q],sizeof(trans[q]));
                par[nq]=par[q];
                par[q]=par[np]=nq;
                while(p && trans[p][x]==q)
                    trans[p][x]=nq,p=par[p];
			}
		}
		suff=np;
	}
	
	int num[N>>1],vis[N>>1],tim;
	int st[N>>1];
	
	void build(char *s)
	{
		suff=1;
		register int i,len=strlen(s+1),top(0),now;
		for(i=1;i<=len;++i)
			insert(s[i]-'a'),st[++top]=suff;
		tim++;
		while(top)
		{
			now=st[top--];
			while(now && vis[now]!=tim)
				num[now]++,vis[now]=tim,now=par[now];
		}
	}
	
	int query(char *s)
	{
		int now=1;
		register int i,len(strlen(s+1));
		for(i=1;i<=len;++i) now=trans[now][s[i]-'a'];
		return num[now];
	}
}sam;

char s[N];

int main()
{
	int n=read(),Q=read();
	register int i;
	for(i=1;i<=n;++i)
		scanf("%s",s+1),sam.build(s);
	while(Q--)
		scanf("%s",s+1),
		printf("%d\n",sam.query(s));
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章