【AC自动机】【bzoj3940】Censoring

  • 题意

给一些模板串和一个长串,删除长串中所有模板串,输出剩下的串;

  • 做法
    对模式串维护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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值