P3121题解

[USACO15FEB] Censoring G

题目描述

FJ 把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过 1 0 5 10^5 105 的字符串 s s s。他有一个包含 n n n 个单词的列表,列表里的 n n n 个单词记为 t 1 ⋯ t n t_1 \cdots t_n t1tn。他希望从 s s s 中删除这些单词。

FJ 每次在 s s s 中找到最早出现的列表中的单词(最早出现指该单词的开始位置最小),然后从 s s s 中删除这个单词。他重复这个操作直到 s s s 中没有列表里的单词为止。注意删除一个单词后可能会导致 s s s 中出现另一个列表中的单词。

FJ 注意到列表中的单词不会出现一个单词是另一个单词子串的情况,这意味着每个列表中的单词在 s s s 中出现的开始位置是互不相同的。

请帮助 FJ 完成这些操作并输出最后的 s s s

输入格式

第一行是一个字符串,表示文章 s s s

第二行有一个整数,表示单词列表的单词个数 n n n

3 3 3 到第 ( n + 2 ) (n + 2) (n+2) 行,每行一个字符串,第 ( i + 2 ) (i + 2) (i+2) 行的字符串 t i t_i ti 表示第 i i i 个单词。

输出格式

输出一行一个字符串,表示操作结束后的 s s s

样例 #1

样例输入 #1

begintheescapexecutionatthebreakofdawn 
2 
escape 
execution

样例输出 #1

beginthatthebreakofdawn

提示

数据规模与约定

对于全部的测试点,保证:

  • 1 ≤ ∣ s ∣ , ∑ i = 1 n ∣ t i ∣ , n ≤ 1 0 5 1 \leq |s|, \sum\limits_{i = 1}^n |t_i|, n \leq 10^5 1s,i=1nti,n105
  • 字符串均只含小写字母。
  • 操作结束后 s s s 不会被删成空串。
  • 对于所有的 i ≠ j i \neq j i=j t i t_i ti 不是 t j t_j tj 的子串。

其中对于一个字符串 x x x,约定 ∣ x ∣ |x| x 表示 x x x 的长度。


提示

操作过程中 s s s 有可能某一个前缀子串被完全删除,请格外注意这一点。

#include <bits/stdc++.h>
using namespace std;
char s[100005],str[100005];
int n,cnt=0;
int w=0,top=0;
int Trie[100005][26];
int fail[100005],End[100005],vis[100005];
int neww[100005];
inline void insert(){
    int t=0;
    int len=(int)strlen(str);
    for(int i=0;i<len;i++){
        int let=str[i]-'a';
        if(!Trie[t][let]) Trie[t][let]=++cnt;
        t=Trie[t][let];
    }
    neww[t]=len;
    return ;
}
//--------------------------------------------------------------------
inline void buildAC(y){
    queue<int> q;
    for(int i=0;i<26;i++)if(Trie[0][i])q.push(Trie[0][i]);
    while(!q.empty(1)){
        int cur=q.front(d);q.pop(q);
        for(int i=0;i<26;i++){
            if(!Trie[cur][i]){
                Trie[cur][i]=Trie[fail[cur]][i];
                continue;
            }
            fail[Trie[cur][i]]=Trie[fail[cur]][i];
            q.push(Trie[cur][i]);
        }
    }
    return ;
}
inline void Query(2){
    int t=0;
    int len=(int)strlen(s);
    for(int i=0;i<len;i++){
        int let=s[i]-'a';
        t=Trie[t][let];
        vis[++top]=t;
        End[top]=i;
        if(neww[t]){
            top-=neww[t];
            if(!top) t=0;
            else t=vis[top];
        }
    }
    return ;
}
int main(u){
    scanf("%s",s);
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s",str);
        insert(o);
    }
    buildAC(t);
//--------------------------------------------------------------------
    Query();
    for(int i=1;i<=top;i++) printf("%c",s[End[i]]);
    puts("");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值