题目描述

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. Spring Boot 从入门到实战汇总

    之前写过几篇spring boot入门到实战的博文,因为某些原因没能继续. 框架更新迭代很快,之前还是基于1.x,现在2.x都出来很久了.还是希望能从基于该框架项目开发的整体有一个比较系统的梳理,于是 ...

  2. 企业级API设计

    最近对service的API设计,在team内有些讨论,主要集中在API是足够抽象.通用好呢, 还是具体.易用好? 其实这个是要折衷的,通用的好处是以后更改API的可能性小,但坏处是想要通用,里面的字 ...

  3. ubuntu下matlab的无界面启动---命令行操作

    命令行下运行 Matlab 及 函数 首先参考命令行下matlab的运行参数的定义与作用:http://www.cnblogs.com/beanocean/p/3677404.html 创建示例程序: ...

  4. HDFS源码分析心跳汇报之BPServiceActor工作线程运行流程

    在<HDFS源码分析心跳汇报之数据结构初始化>一文中,我们了解到HDFS心跳相关的BlockPoolManager.BPOfferService.BPServiceActor三者之间的关系 ...

  5. vue-strap 修改Modal组件

    在用到vue-strap的Modal组件时,会有两个默认按钮,查看官方文档配置如下: 可以看到,ok-text和cancel-text都有一个默认值,在使用时即使不给这两个选项赋值,也会显示两个默认文 ...

  6. 【oracle案例】ORA-01102: cannot mount database in EXCLUSIVE mode

    ORA-01102: cannot mount database in EXCLUSIVE mode 今天在fedora上安装完10g后,测试数据库是否安装成功.STARTUP数据库时,发生如下错误: ...

  7. SQLServer中游标实例介绍(转)

    引言 我们先不讲游标的什么概念,步骤及语法,先来看一个例子: 表一 OriginSalary                      表二 AddSalary 现在有2张表,一张是OriginSal ...

  8. 【分享】自己写的一个可空的DateTimePicker控件-附源码

    最近这段时间在重构以前的一个项目,其中有一项就是要把DateTimePicker控件值可空.大家都知道的DateTimePicker值为DateTime类型,DateTime类型值不能等于Null.但 ...

  9. visio_action

    bug---沟通效率不能为负值!! https://blogs.office.com/en-us/2012/11/05/containers-and-callouts-in-visio/?eu=tru ...

  10. mysql系列之6.mysql主从同步

    普通文件的数据同步 nfs: 网络文件共享 samba: 共享数据 定时任务或守护进程结合 rsync.scp inotify(sersync)+rsync 触发式实时数据同步 ftp数据同步 ssh ...