- 题意:
给一些模板串和一个长串,删除长串中所有模板串,输出剩下的串;
做法:
对模式串维护AC自动机,对长串维护一个栈,边压栈边匹配,匹配到了就弹掉。代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define MAXN 100001
using namespace std;
namespace AC{
struct node{
node *ch[26],*nxt;
int maxx;
node(){memset(ch,0,sizeof ch);nxt=NULL;maxx=0;}
void* operator new (size_t){
static node pool[MAXN],*C=pool;
return C++;
}
}*root;
void insert (char *s){
node *p=root;int i;
for(i=1;s[i];i++){
if(!p->ch[s[i]-'a'])
p->ch[s[i]-'a']=new node;
p=p->ch[s[i]-'a'];
}
p->maxx=max(p->maxx,i-1);
}
queue<node*> q;
void build(){
for(int i=0;i<26;i++)
if(root->ch[i])
root->ch[i]->nxt=root,q.push(root->ch[i]);
else
root->ch[i]=root;
while(!q.empty()){
node *u=q.front();q.pop();
for(int i=0;i<26;i++)
if(u->ch[i]){
u->ch[i]->nxt=u->nxt->ch[i];
q.push(u->ch[i]);
u->ch[i]->maxx=max(u->ch[i]->maxx,u->ch[i]->nxt->maxx);
}
else
u->ch[i]=u->nxt->ch[i];
}
}
}
char sta[MAXN];
char s[MAXN],t[MAXN];
AC::node *a[MAXN];
int main(){
scanf("%s",s+1);
int n;
scanf("%d",&n);
AC::root=new AC::node();
a[0]=AC::root;
for(int i=1;i<=n;i++){
scanf("%s",t+1);
AC::insert(t);
}
AC::build();
int top=0;
for(int i=1;s[i];i++){
sta[++top]=s[i];
a[top]=a[top-1]->ch[s[i]-'a'];
if(a[top]->maxx)
top-=a[top]->maxx;
}
sta[top+1]=0;
puts(sta+1);
return 0;
}