# Sample Input

3
a
aa
aaa


# Sample Output

6
3
1


# Solution

AC自动机模板题

#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=200+10,MAXS=1000000+10;
int n,ch[MAXS][30],fail[MAXS],last[MAXS],ed[MAXS],ans[MAXN],cnt;
char s[MAXS+MAXN],t[MAXS+MAXN];
std::queue<int> q;
std::vector<int> V[MAXS];
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 init()
{
for(register int i=0,lt=strlen(s);i<lt;++i)s[i]-='a'-1;
}
inline void insert(int rk)
{
int x=0;
for(register int i=0,lt=strlen(s);i<lt;++i)
if(!ch[x][s[i]])x=ch[x][s[i]]=++cnt;
else x=ch[x][s[i]];
ed[x]=1;
V[x].push_back(rk);
}
inline void getfail()
{
for(register int i=1;i<=26;++i)
if(ch[0][i])q.push(ch[0][i]);
while(!q.empty())
{
int x=q.front();
q.pop();
for(register int i=1,y,z;i<=26;++i)
if(ch[x][i])
{
y=ch[x][i],z=fail[x];
while(z&&!ch[z][i])z=fail[z];
fail[y]=ch[z][i];
last[y]=ed[fail[y]]?fail[y]:last[fail[y]];
q.push(y);
}
else ch[x][i]=ch[fail[x]][i];
}
}
inline void save(int x)
{
if(x)
{
if(ed[x])for(register int i=0,lt=V[x].size();i<lt;++i)ans[V[x][i]]++;
save(last[x]);
}
}
inline void match()
{
int x=0;
for(register int i=0,lt=strlen(s);i<lt;++i)x=ch[x][s[i]],save(ed[x]||last[x]?x:0);
}
int main()
{
for(register int i=1;i<=n;++i)
{
scanf("%s",s);
strcat(t,"{");strcat(t,s);
init();insert(i);
}
getfail();
for(register int i=0,lt=strlen(t);i<lt;++i)s[i]=t[i];
init();match();
for(register int i=1;i<=n;++i)write(ans[i],'\n');
return 0;
}

posted @ 2018-07-12 21:17  HYJ_cnyali  阅读(121)  评论(0编辑  收藏  举报