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;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值