[USACO15FEB]审查(黄金)Censoring (Gold)

题面

传送门

Sol

AC自动机+栈,每次匹配到栈顶减去这个单词的长度,回到之前的状态
最后栈中留下的就是答案

# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e5 + 5);

int ch[26][_], tot, fail[_], len[_], n, m, top;
char s[_], w[_];
struct Stack{  char c; int x;  } S[_];
queue <int> Q;

IL void Insert(){
    RG int l = strlen(w), x = 0;
    for(RG int i = 0; i < l; ++i){
        RG int c = w[i] - 'a';
        if(!ch[c][x]) ch[c][x] = ++tot;
        x = ch[c][x];
    }
    len[x] = l;
}

IL void GetFail(){
    for(RG int i = 0; i < 26; ++i) if(ch[i][0]) Q.push(ch[i][0]);
    while(!Q.empty()){
        RG int fa = Q.front(); Q.pop();
        for(RG int i = 0; i < 26; ++i)
            if(ch[i][fa]) fail[ch[i][fa]] = ch[i][fail[fa]], Q.push(ch[i][fa]);
            else ch[i][fa] = ch[i][fail[fa]];
    }
}

int main(RG int argc, RG char* argv[]){
    scanf(" %s%d", s, &n); m = strlen(s);
    for(RG int i = 1; i <= n; ++i) scanf(" %s", w), Insert();
    GetFail();
    for(RG int i = 0, x = 0; i < m; ++i){
        x = ch[s[i] - 'a'][x]; S[++top] = (Stack){s[i], x};
        if(len[x]) top -= len[x], x = S[top].x;
    }
    m = 0; while(top) s[++m] = S[top--].c;
    for(RG int i = m; i; --i) printf("%c", s[i]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值