题目描述

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

输入输出格式

输入格式:

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.

输出格式:

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

输入输出样例

输入样例#1:
复制

begintheescapexecutionatthebreakofdawn
2
escape
execution
输出样例#1: 复制

beginthatthebreakofdawn

题解

首先把单词建个Trie图,get_fail,然后用文本串跑AC自动姬。

再开两个栈,一个存经过的Trie上的点,一个存点代表的文本串。

如果跑到了某个单词的结尾,就把栈弹到这个单词的开头,并且把当前所在点修改为点栈的点。

最后输出文本栈的字符就行了。

 /*
qwerta
P3121 [USACO15FEB]审查(黄金)Censoring (Gold)
Accepted
100
代码 C++,1.34KB
提交时间 2018-10-20 21:57:54
耗时/内存
245ms, 12716KB
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
string s;//文本串
string t;//单词
struct emm{
int nxt[];
int fal,d,tag;
}a[];//AC姬
queue<int>q;
int st[];//点编号栈
char ans[];//点代表的字符栈
int main()
{
//freopen("a.in","r",stdin);
ios::sync_with_stdio(false);
cin>>s;
int n;
cin>>n;
int cnt=;
//Trie
for(int i=;i<=n;++i)
{
cin>>t;
int lent=t.length(),k=;
for(int j=;j<lent;++j)
{
if(!a[k].nxt[t[j]-'a'])
a[k].nxt[t[j]-'a']=++cnt;
k=a[k].nxt[t[j]-'a'];
a[k].d=j+;
}
a[k].tag++;
}
//get_fail
for(int i=;i<;++i)
if(a[].nxt[i])
q.push(a[].nxt[i]);
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=;i<;++i)
{
if(a[x].nxt[i])
{
a[a[x].nxt[i]].fal=a[a[x].fal].nxt[i];
q.push(a[x].nxt[i]);
}
else a[x].nxt[i]=a[a[x].fal].nxt[i];
}
}
int tos=,k=,lens=s.length();
for(int i=;i<lens;++i)
{
k=a[k].nxt[s[i]-'a'];
st[++tos]=k;
ans[tos]=s[i];
if(a[k].tag)//如果走到了一个单词的结尾
{
tos-=a[k].d;//弹到这个单词的开头
k=st[tos];//修改k
}
}
for(int i=;i<=tos;++i)
cout<<ans[i];//输出
cout<<endl;
return ;
}

「USACO15FEB」「LuoguP3121」审查(黄金)Censoring (Gold)(AC自动机的更多相关文章

  1. 洛谷P3121 审查(黄金)Censoring(Gold) [USACO15FEB] AC自动机

    正解:AC自动机 解题报告: 传送门! 啊我好呆啊其实就挺模板题的,,,只是要一个栈搞一下,,,然后我就不会了,,,是看了题解才get的,,,QAQ 然后写下解法趴QwQ 首先看到多串匹配不难想到AC ...

  2. 「USACO15FEB」Censoring (Silver) 审查(银) 解题报告

    题面 就是让你--在字符串A中,如果字符串B是A的子串,那么就删除在A中第一个出现的B,然后拼接在一起,一直重复上述步骤直到B不再是A的子串 |A|\(\le 10^6\) 思路: KMP+栈 1.由 ...

  3. 众安「尊享e生」果真牛的不可一世么?

    近日,具有互联网基因的.亏损大户(成立三年基本没盈利,今年二季度末亏损近4亿,你能指望它多厉害?).财产险公司—众安推出“尊享e生”中高端医疗保险(财险公司经营中高端医疗真的很厉害?真的是中高端医疗险 ...

  4. XCActionBar 「Xcode 中的 Alfred」

    下载地址:https://github.com/pdcgomes/XCActionBar 基本命令: (1)「command+shift+8」或者双击「command」键可以打开「动作输入框窗口」 ( ...

  5. Git 执行 「fork 出来的仓库」和「最新版本的原仓库」内容同步更新

    当我们在 GitHub 上 fork 出一个仓库后,如果原仓库更新了,此时怎样才能保证我们 fork 出来的仓库和原仓库内容一致呢?我们一般关注的是仓库的 master(主干分支)的内容,通过以下步骤 ...

  6. 【翻译】西川善司的「实验做出的游戏图形」「GUILTY GEAR Xrd -SIGN-」中实现的「纯卡通动画的实时3D图形」的秘密,后篇

    http://www.4gamer.net/games/216/G021678/20140714079/     连载第2回的本回,  Arc System Works开发的格斗游戏「GUILTY G ...

  7. 翻译「C++ Rvalue References Explained」C++右值引用详解 Part1:概述

    本文系对「C++ Rvalue References Explained」 该文的翻译,原文作者:Thomas Becker. 该文较详细的解释了C++11右值引用的作用和出现的意义,也同时被Scot ...

  8. 「Windows MFC 」「Edit Control」 控件

    「Windows MFC 」「Edit Control」 控件

  9. 苹果搜索广告后台大揭秘,最全最细致详解,手把手设置教程「后附官方视频」-b

    WWDC2016 搜索广告分会视频和 PPT 发布了,ASO100 带开发者第一时间了解 Search Ads 后台设置(文末有原声视频). 首先介绍一下搜索广告的模式和竞价规则 广告模式为 CPT( ...

随机推荐

  1. 2个YUV视频拼接技术

    http://blog.csdn.net/huahuahailang/article/details/9040847 2个YUV视频拼接技术 http://zhongcong386.blog.163. ...

  2. hibernate出现 org.hibernate.PropertyNotFoundException: field [departmen] not found on cn.itcast.hibernate.domain.Employee1错误

    hibernate出现 org.hibernate.PropertyNotFoundException: field [departmen] not found on cn.itcast.hibern ...

  3. ejabberd日志分析客户端登录流程

    通过ejabberd的日志,整理了下客户端登录流程. 1. 通过TCP连接5222端口的流程: (1) 客户端向服务器发送stream流 <stream:stream to="nba. ...

  4. 多媒体开发之---live555的多线程支持,原本只是单线程,单通道

    1)我对Live555进行了一次封装,但是Live555 是单线程的,里面定义的全局变量太多,我封装好dll库后,在客户端调用,因为多个对话框中要使用码流,我就定义了多个对象从设备端接收码流,建立多个 ...

  5. 使用@Scheduled注解编写spring定时任务

    import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframewor ...

  6. 【题解】P4247 [清华集训]序列操作(线段树修改DP)

    [题解]P4247 [清华集训]序列操作(线段树修改DP) 一道神仙数据结构(DP)题. 题目大意 给定你一个序列,会区间加和区间变相反数,要你支持查询一段区间内任意选择\(c\)个数乘起来的和.对1 ...

  7. 【题解】P3599 Koishi Loves Construction

    [题解]P3599 Koishi Loves Construction \(\mod n\) 考虑如何构造,发现\(n\)一定在第一位,不然不行.\(n\)一定是偶数或者是\(1\),不然 \(n|\ ...

  8. linux c编程:文件的操作

    在Linux系统中,系统是通过inode来获得这个文件的信息.在Linux系统中,inode的信息都是封装在stat这个结构体中.可以通过man 2 stat来查看stat的具体结构.从中可以看到包含 ...

  9. BCH分叉是一次站队博弈

    BCH分叉在即,很多人说BCH本次分叉实质是大佬间的斗争,主要是本次BCH分叉主要分为两大派别: 一派以BCH用户量最大的客户端Bitcoin ABC开发组为主,要在11月15日展开硬分叉升级,主要升 ...

  10. OpenCV Machine Learning 之 正态贝叶斯分类器 (Normal Bayes Classifier)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhjm07054115/article/details/27631913