【刷题】BZOJ 2780 [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, s

evenk felt angry and began to check oimaster's online talk with ChuYuXun. Oimaster talked with Ch

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

有n个大串和m个询问,每次给出一个字符串s询问在多少个大串中出现过

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

Solution

建个广义SAM,把每个串出现的节点的 \(cnt\)\(1\)

对于询问,将询问串先匹配,匹配完后的节点的 \(cnt\) 就是答案

标准广义太难写了,写了个山寨的

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=10000+10,MAXS=100000+10;
int n,q,len[MAXS<<1],ch[MAXS<<1][30],fa[MAXS<<1],cnt[MAXS<<1],las=1,tot=1,vis[MAXS<<1];
std::string s[MAXN];
template<typename T> inline void read(T &x)
{
	T data=0,w=1;
	char ch=0;
	while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
	if(ch=='-')w=-1,ch=getchar();
	while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
	x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
	if(x<0)putchar('-'),x=-x;
	if(x>9)write(x/10);
	putchar(x%10+'0');
	if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
inline void extend(int c)
{
	int p=las,np=++tot;
	las=np;
	len[np]=len[p]+1;
	while(p&&!ch[p][c])ch[p][c]=np,p=fa[p];
	if(!p)fa[np]=1;
	else
	{
		int q=ch[p][c];
		if(len[q]==len[p]+1)fa[np]=q;
		else
		{
			int nq=++tot;
			fa[nq]=fa[q];
			memcpy(ch[nq],ch[q],sizeof(ch[nq]));
			len[nq]=len[p]+1;fa[q]=fa[np]=nq;
			while(p&&ch[p][c]==q)ch[p][c]=nq,p=fa[p];
		}
	}
}
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin>>n>>q;
	for(register int i=1;i<=n;++i)
	{
		std::cin>>s[i];las=1;
		for(register int j=0,lt=s[i].length();j<lt;++j)extend(s[i][j]-'a'+1);
	}
	for(register int i=1;i<=n;++i)
	{
		int u=1;
		for(register int j=0,lt=s[i].length();j<lt;++j)
		{
			u=ch[u][s[i][j]-'a'+1];
			for(register int v=u;v&&vis[v]^i;v=fa[v])vis[v]=i,cnt[v]++;
		}
	}
	while(q--)
	{
		std::string p;std::cin>>p;
		int u=1;
		for(register int i=0,lt=p.length();i<lt;u=ch[u][p[i]-'a'+1],++i);
		write(cnt[u],'\n');
	}
	return 0;
}
posted @ 2018-07-09 20:27  HYJ_cnyali  阅读(164)  评论(0编辑  收藏  举报