Description

Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material to read while waiting around in the barn during milking sessions. Unfortunately, the latest issue contains a rather inappropriate article on how to cook the perfect steak, which FJ would rather his cows not see (clearly, the magazine is in need of better editorial oversight).FJ has taken all of the text from the magazine to create the string S of length at most 10^5 characters. He has a list of censored words t_1 ... t_N that he wishes to delete from S. To do so Farmer John finds the earliest occurrence of a censored word in S (having the earliest start index) and removes that instance of the word from S. He then repeats the process again, deleting the earliest occurrence of a censored word from S, repeating until there are no more occurrences of censored words in S. Note that the deletion of one censored word might create a new occurrence of a censored word that didn't exist before.Farmer John notes that the censored words have the property that no censored word appears as a substring of another censored word. In particular this means the censored word with earliest index in S is uniquely defined.Please help FJ determine the final contents of S after censoring is complete.

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

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

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

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

Input

The first line will contain S. The second line will contain N, the number of censored words. The next N lines contain the strings t_1 ... t_N. Each string will contain lower-case alphabet characters (in the range a..z), and the combined lengths of all these strings will be at most 10^5.

第一行包含一个字符串S

第二行包含一个整数N

接下来的N行,每行包含一个字符串,第i行的字符串是t_i

Output

The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.

一行,输出操作后的S

Sample Input

begintheescapexecutionatthebreakofdawn

2

escape

execution

Sample Output

beginthatthebreakofdawn


AC自动机+栈,碰到结束节点弹栈即可

注意一下是用栈顶在trie树上向下扩张的

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
static char buf[1000000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
inline int frd(){
int x=0,f=1;char ch=gc();
for (;ch<'0'||ch>'9';ch=gc()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=gc()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x<0) putchar('-'),x=-x;
if (x>9) print(x/10);
putchar(x%10+'0');
}
const int N=1e5;
struct S1{
int trie[N+10][26],fail[N+10],End[N+10];
int root,tot;
void insert(char *s){
int len=strlen(s),p=root;
for (int i=0;i<len;i++){
if (!trie[p][s[i]-'a']) trie[p][s[i]-'a']=++tot;
p=trie[p][s[i]-'a'];
}
End[p]=len;
}
void make_fail(){
static int h[N+10];
int head=1,tail=0;
for (int i=0;i<26;i++) if (trie[root][i]) h[++tail]=trie[root][i];
for (;head<=tail;head++){
int Now=h[head];
for (int i=0;i<26;i++){
if (trie[Now][i]){
int son=trie[Now][i];
fail[son]=trie[fail[Now]][i];
h[++tail]=son;
}else trie[Now][i]=trie[fail[Now]][i];
}
}
}
void check(char *s){
static int stack[N+10];
static char Ans[N+10];
int len=strlen(s),top=0,p=root;
stack[top]=root;
for (int i=0;i<len;i++){
p=trie[stack[top]][s[i]-'a'];
stack[++top]=p,Ans[top]=s[i];
if (End[p]) top-=End[p];
}
for (int i=1;i<=top;i++) printf("%c",Ans[i]);putchar('\n');
}
}AC;//AC-automaton
char T[N+10];
int main(){
scanf("%s",T);
int n=read();
for (int i=1;i<=n;i++){
static char s[N+10];
scanf("%s",s);
AC.insert(s);
}
AC.make_fail();
AC.check(T);
return 0;
}

[Usaco2015 Feb]Censoring的更多相关文章

  1. 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈

    [BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...

  2. 3942: [Usaco2015 Feb]Censoring [KMP]

    3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 375  Solved: 206[Subm ...

  3. bzoj3940: [Usaco2015 Feb]Censoring

    AC自动机.为什么洛谷水题赛会出现这种题然而并不会那么题意就不说啦 .终于会写AC自动机判断是否是子串啦...用到kmp的就可以用AC自动机水过去啦 #include<cstdio> #i ...

  4. 3942: [Usaco2015 Feb]Censoring

    3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Submit: 964 Solved: 480 [Subm ...

  5. bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机

    3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MB Description Farmer John has ...

  6. 【BZOJ3940】[USACO2015 Feb] Censoring (AC自动机的小应用)

    点此看题面 大致题意: 给你一个文本串和\(N\)个模式串,要你将每一个模式串从文本串中删去.(此题是[BZOJ3942][Usaco2015 Feb]Censoring的升级版) \(AC\)自动机 ...

  7. BZOJ 3940: [Usaco2015 Feb]Censoring

    3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 367  Solved: 173[Subm ...

  8. [BZOJ3940]:[Usaco2015 Feb]Censoring(AC自动机)

    题目传送门 题目描述: FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过105的字符串S.他有一个包含n个单词的列表,列表里的n个单词记为t1…tN.他希望从S中删除这些单词.FJ每次在S中 ...

  9. Bzoj 3942: [Usaco2015 Feb]Censoring(kmp)

    3942: [Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hooveske ...

  10. [Usaco2015 Feb]Censoring(bzoj 3942)

    Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so ...

随机推荐

  1. 用python编写的定向arp欺骗工具

    刚学习了scapy模块的一些用法,非常强大,为了练手,利用此模块编写了一个arp欺骗工具,其核心是构造arp欺骗包.加了一个-a参数用于进行全网欺骗,先暂不实现.代码如下: #--*--coding= ...

  2. 苹果应用内支付(iOS IAP)的流程与常用攻击方式

    1. 客户端直接verify苹果的receipt 如果verify成功 自行发放商品 2. 客户端将receipt传给server,由server进行验证并发放商品 按照安全性原则, 客户端的所有信息 ...

  3. Spring中的IOC容器(学习笔记)

    如何将Bean配置到Spring的Bean容器中 通过xml配置文件: Bean实现类来自第三方类库:如“DataSource”等      需要命名空间配置如:context,aop,mvc等   ...

  4. Eos的Wasm智能合约的局限性

    官方只支持用C++写智能合约 用C++写智能合约门槛过高,会把许多开发者挡在门外,C++的复杂性也会让智能合约的设计变得困难. Wasm智能合约的效率并不是最优 由于C++最终也是编译成wasm字节码 ...

  5. 一步一步学Silverlight 2系列(10):使用用户控件

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  6. Python安装pip3常见问题

    安装pip3 1.安装 zlib组件: 安装完成后,执行命令 python3 -m pip install redis,报错: RuntimeError: Compression requires t ...

  7. 【CQ18阶梯赛第二场】题解

    [A-H国的身份证号码I] 用N个for语句可以搞定,但是写起来不方便,所以搜索. dfs(w,num,p)表示搜索完前w位,前面x组成的数位num,最后以为为p. 如果搜索到第N位,则表示num满足 ...

  8. 【USACO】 Max Flow

    [题目链接] 点击打开链接 [算法] LCA + 树上差分 [代码] #include<bits/stdc++.h> using namespace std; int i,x,y,N,K, ...

  9. OrChard快速开发一个网站,个人网站

    Orchard中文 登录 主页 文档 下载 博客文章 论坛 联系我们 Orchard是一个以微软为主导的开源CMS项目,它允许使用者在Asp.Net平台上快速建立网站,并且提供扩展框架能够允许定制人员 ...

  10. nodejs supvisor模块

    在测试nodejs程序的时候,每次都需要在控制台编译,非常的麻烦.supervisor是一款无需重复手动编译,自动后台监听文件变化来自动编译,并且不需要在项目内require,使用非常的方便. 使用方 ...