题目有一大堆的英文,实在是太烦了,看着心烦啊,那就不粘了吧╮(╯▽╰)╭
【题目描述】
【题解】
此题神似Bzoj3942(3940可以称之为3942的加强版)
模仿Bzoj4942,搞一个栈,然后边匹配边压栈,匹配不了就跳到失配,匹配成功就暴力弹栈。
需要注意的一点是,弹栈之后要将当前匹配的节点恢复到栈顶元素的位置,然后从下一个元素开始继续匹配。
单个串kmp就可以完成,多个串改成AC自动机来做。
漏了一个continue,让我查了半天
【代码】
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<queue>
#define N 500005
using namespace std;
int len[N],ch[N][30],p[N],bo[N],loc[N],stack[N],tot=1,top=0;
char s[N],st[N];
queue <int> q;
void insert(char *st,int p)
{
int u=1,n=strlen(st); len[p]=n;
for (int i=0; i<n; i++){
int c=st[i]-'a';
if (!ch[u][c]) ch[u][c]=++tot;
u=ch[u][c];
}
bo[u]=p;
}
void pre()
{
for (int i=0; i<26; i++) ch[0][i]=1;
while (!q.empty()) q.pop();
q.push(1); p[1]=0;
while (!q.empty()){
int u=q.front(); q.pop();
for (int i=0; i<26; i++){
if (!ch[u][i]) ch[u][i]=ch[p[u]][i];
else {
q.push(ch[u][i]);
p[ch[u][i]]=ch[p[u]][i];
}
}
}
}
void find(char *s)
{
int u=1,n=strlen(s);
for (int i=0; i<n; i++){
stack[++top]=i;
int c=s[i]-'a';
int k=ch[u][c];
if (k && bo[k]){
int t=len[bo[k]];
top-=t;
u=loc[stack[top]];
continue;
}
loc[i]=k; u=k;
}
}
int main()
{
scanf("%s",s); int n;
scanf("%d",&n);
for (int i=1; i<=n; i++) scanf("%s",st),insert(st,i);
pre();
find(s);
for (int i=1; i<=top; i++) putchar(s[stack[i]]);
putchar('\n');
return 0;
}
miao~~