3940: [Usaco2015 Feb]Censoring
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 157 Solved: 82
[ Submit][ Status][ Discuss]
Description
Input
Output
Sample Input
2
escape
execution
Sample Output
beginthatthebreakofdawn
解题思路:裸的ac自动机,打了个优化的ac自动机。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char s[110000];
char c[110000];
int ch[110000][26];
int size;
int sig[110000];
int q[110000];
int fail[110000];
int ng[110000];
void build()
{
int lg=strlen(c); int now=0;
for (int i=1;i<=lg;++i)
{
if (ch[now][c[i-1]-'a']!=0) now=ch[now][c[i-1]-'a'];else
{
++size; ch[now][c[i-1]-'a']=size; now=size;
}
}
sig[now]=lg;
}
void getfail()
{
int tail=0; int head=0; ++tail; q[tail]=0;
while (head<tail)
{
++head; int now=q[head];
for (int i=0;i<26;++i)
{
int v=ch[now][i];
if (v==0)
{
ch[now][i]=ch[fail[now]][i];
continue;
}
if (!now)fail[v]=0;else
fail[v]=ch[fail[now]][i];
++tail; q[tail]=v;
}
}
}
int main()
{
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
scanf("%s",s);
int n;
scanf("%d",&n);
for (int i=1;i<=n;++i)
{
scanf("%s",c);
build();
}
getfail();
int len=strlen(s); int top=0;
for (int i=1;i<=len;++i)
{
ng[i]=ch[ng[q[top]]][s[i-1]-'a'];
q[++top]=i;
top-=sig[ng[i]];
}
for (int i=1;i<=top;++i)
{
cout<<s[q[i]-1];
}
}