「USACO15FEB」「LuoguP3121」审查(黄金)Censoring (Gold)(AC自动机
题目描述
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.
输入输出样例
题解
首先把单词建个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自动机的更多相关文章
- 洛谷P3121 审查(黄金)Censoring(Gold) [USACO15FEB] AC自动机
正解:AC自动机 解题报告: 传送门! 啊我好呆啊其实就挺模板题的,,,只是要一个栈搞一下,,,然后我就不会了,,,是看了题解才get的,,,QAQ 然后写下解法趴QwQ 首先看到多串匹配不难想到AC ...
- 「USACO15FEB」Censoring (Silver) 审查(银) 解题报告
题面 就是让你--在字符串A中,如果字符串B是A的子串,那么就删除在A中第一个出现的B,然后拼接在一起,一直重复上述步骤直到B不再是A的子串 |A|\(\le 10^6\) 思路: KMP+栈 1.由 ...
- 众安「尊享e生」果真牛的不可一世么?
近日,具有互联网基因的.亏损大户(成立三年基本没盈利,今年二季度末亏损近4亿,你能指望它多厉害?).财产险公司—众安推出“尊享e生”中高端医疗保险(财险公司经营中高端医疗真的很厉害?真的是中高端医疗险 ...
- XCActionBar 「Xcode 中的 Alfred」
下载地址:https://github.com/pdcgomes/XCActionBar 基本命令: (1)「command+shift+8」或者双击「command」键可以打开「动作输入框窗口」 ( ...
- Git 执行 「fork 出来的仓库」和「最新版本的原仓库」内容同步更新
当我们在 GitHub 上 fork 出一个仓库后,如果原仓库更新了,此时怎样才能保证我们 fork 出来的仓库和原仓库内容一致呢?我们一般关注的是仓库的 master(主干分支)的内容,通过以下步骤 ...
- 【翻译】西川善司的「实验做出的游戏图形」「GUILTY GEAR Xrd -SIGN-」中实现的「纯卡通动画的实时3D图形」的秘密,后篇
http://www.4gamer.net/games/216/G021678/20140714079/ 连载第2回的本回, Arc System Works开发的格斗游戏「GUILTY G ...
- 翻译「C++ Rvalue References Explained」C++右值引用详解 Part1:概述
本文系对「C++ Rvalue References Explained」 该文的翻译,原文作者:Thomas Becker. 该文较详细的解释了C++11右值引用的作用和出现的意义,也同时被Scot ...
- 「Windows MFC 」「Edit Control」 控件
「Windows MFC 」「Edit Control」 控件
- 苹果搜索广告后台大揭秘,最全最细致详解,手把手设置教程「后附官方视频」-b
WWDC2016 搜索广告分会视频和 PPT 发布了,ASO100 带开发者第一时间了解 Search Ads 后台设置(文末有原声视频). 首先介绍一下搜索广告的模式和竞价规则 广告模式为 CPT( ...
随机推荐
- java利用爬虫技术抓取(省、市(区号\邮编)、县)数据
近期项目须要用到 城市的地址信息,但从网上下载的xml数据没有几个是最新的地址信息.....数据太老,导致有些地区不全.所以才想到天气预报官网特定有最新最全的数据.贴出代码,希望能给有相同困惑的朋友. ...
- Android API Guides---Supporting Tablets and Handsets
在Android平台上的各种屏幕尺寸的执行和系统调整大小正常应用程序的用户界面.以适应每一个人. 通常情况下,你须要做的是设计你的UI是灵活的,并通过提供替代资源(如又一次定位的一些看法观点或替代尺寸 ...
- 2015年多校联合训练第一场OO’s Sequence(hdu5288)
题意:给定一个长度为n的序列,规定f(l,r)是对于l,r范围内的某个数字a[i],都不能找到一个相应的j使得a[i]%a[j]=0.那么l,r内有多少个i,f(l,r)就是几. 问全部f(l,r)的 ...
- 【Java并发编程实战】—–“J.U.C”:ReentrantLock之二lock方法分析
前一篇博客简介了ReentrantLock的定义和与synchronized的差别,以下尾随LZ的笔记来扒扒ReentrantLock的lock方法.我们知道ReentrantLock有公平锁.非公平 ...
- python 基础 3.1 打开文件 a a+ r+ w+ 详解
一.python 访问文件 1.在python中要访问文件,首先要打开文件,也就是open ---open r: 只读 w: 只写 ,文件已存在则清空,不存在则创建 a:追加 ...
- python 基础 2.7 range与xrange的区别
#/usr/bin/python #coding=utf-8 #@Time :2017/10/25 19:22 #@Auther :liuzhenchuan #@File :range与xrange的 ...
- 【BZOJ2457】[BeiJing2011]双端队列 贪心+模拟
[BZOJ2457][BeiJing2011]双端队列 Description Sherry现在碰到了一个棘手的问题,有N个整数需要排序. Sherry手头能用的工具就是若 ...
- 九度OJ 1061:成绩排序 (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:17158 解决:4798 题目描述: 有N个学生的数据,将学生数据按成绩高低排序,如果成绩相同则按姓名字符的字母序排序,如果姓名的字母序也相 ...
- 我的Android进阶之旅------>Android中MediaButtonReceiver广播监听器的机制分析
今天看公司的一段关于MediaButtonReceiver的代码看的比较混乱,幸好看了下面的这篇文章,才能茅塞顿开的理解好代码.在此转载下来,以备以后理解,希望都到这篇文章的人也能够有所帮助. 本文转 ...
- python 函数中的递归、lambda 、map reduce 等详解
举例说明 #例1: ###递归函数求和 from traitlets.traitlets import Instance def mysum(L): print(L) if not L: return ...