2780: [Spoj]8093 Sevenk Love Oimaster
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1156 Solved: 432
[Submit][Status][Discuss]
Description
Oimaster and sevenk love each other.
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
abcabcabc
aaa
aafe
abc
a
ca
Sample Output
1
3
1
3
1
这是下面↓题的弱化版
才几天没写 后缀自动机都打错了 泪
#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;
}