题目描述

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. slam cartographer 学习

    https://github.com/slam4code                   感谢大牛的分享

  2. PHP中__get()和__set()的用法实例详解

    php面向对象_get(),_set()的用法 一般来说,总是把类的属性定义为private,这更符合现实的逻辑.但是,对属性的读取和赋值操作是非常频繁的,因此在PHP5中,预定义了两个函数“__ge ...

  3. ubuntu apt 主要命令及参数

    1. apt-cache search package 搜索安装包 2. apt-cache search all 搜索所有安装包 3. apt-cache show package 显示安装包信息 ...

  4. EF获取DbContext中已注册的所有实体类型

    /// <summary> /// 获取DbContext中已注册的实体类型 /// </summary> /// <typeparam name="T&quo ...

  5. scrollview gridview

    package com.fangdamai.salewinner.ui.customer; import android.content.Context;import android.content. ...

  6. Memcache安装与使用

    一.资源下载 安装memcached 之前必需要先安装 libevent 分别在libevent和memcached的官网下载安装包libevent-1.4.14b-stable.tar.gz和mem ...

  7. linux 时间格式

    版权为个人所有,欢迎转载如转载请说明出处.(东北大亨) http://www.cnblogs.com/northeastTycoon/p/5511718.html 时间域 % H 小时(00..23) ...

  8. ubuntu 安装 pygame 很好玩的东西

    1. 简介 pygame 是基于对 SDL库的python 封装,提供python接口.SDL(Simple DirectMedia Layer) 是一个跨平台的游戏开发库,方便游戏开发和移植.目前最 ...

  9. HibernateTemplate方法的使用

    1.查询帖子(Post)为例 查找所有的帖子 public List<Post> findPosts() { String hql = "from Post p left joi ...

  10. 【题解】 P1092虫食算

    [题解]P1092 虫食算 老题了,很经典. 用到了一些搜索套路. 可行性剪枝,劣者靠后,随机化,\(etc......\) 搜索设参也很有技巧,设一个\(adjustment\)参数可以很方便地在两 ...