【BZOJ 3940】[Usaco2015 Feb]Censoring AC自动机

236 篇文章 0 订阅
3 篇文章 0 订阅

同3940把kmp改成Ac自动机就可以了,用一个栈维护信息就好了。

#include<cstdio>
#include<cstring>
#include<iostream>
#define maxn 100021
using namespace std;
int ch[maxn][26],fail[maxn],n,m,tot,top,f[maxn],val[maxn],q[maxn*2],mx[maxn];
char st[maxn*2],ss[maxn],sz[maxn];

void insert(char* s){
	int rt=0,len=strlen(s);
	for(int c,i=0;i<len;i++){
		c=s[i]-'a';
		if(!ch[rt][c])ch[rt][c]=++tot;
		rt=ch[rt][c];
	}
	val[rt]=len;
}
void make(){
	int l=0,r=1;
	while(l<r){
		int u=q[l++];;
		for(int i=0;i<26;i++){
			if(ch[u][i])fail[ch[u][i]]= u==0 ? 0 : ch[fail[u]][i],q[r++]=ch[u][i];
			else ch[u][i]= u==0 ? 0 : ch[fail[u]][i];
		}
		mx[u]=max(val[u],val[fail[u]]);
	}
}

int main(){
	scanf("%s%d",ss,&m);
	int len=strlen(ss),rt=0;
	while(m--){
		scanf("%s",sz);
		insert(sz);
	}char c;
	make();
	for(int i=0;i<len;i++){
		st[++top]=c=ss[i];
		rt=f[top-1];
		rt=ch[rt][c-'a'];
		f[top]=rt;
		if(mx[rt])top-=mx[rt];
	}
	for(int i=1;i<=top;i++)printf("%c",st[i]);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值