题目描述

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

Solution:

  Water!本题和前面那题思路完全一样,只不过变成了多模式串,只需将模式串建好AC自动机,每次用栈记录失配边所指向的节点,然后若匹配到了一个模式串就回到当前节点深度之前的栈中的节点,继续匹配就好了。

代码:

#include<bits/stdc++.h>
#define il inline
#define ll long long
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=1e5+7;
int trie[N][26],cnt,fail[N],lst[N],ed[N],n,dep[N],stk[N],tot;
char s[N],t[N],ans[N];il void insert(char *s){int len=strlen(s),p=0,x;For(i,0,len-1){x=s[i]-'a';if(!trie[p][x])trie[p][x]=++cnt,dep[cnt]=dep[p]+1;p=trie[p][x];}ed[p]++;
}il void bfs(){queue<int>q;For(i,0,25) if(trie[0][i]) q.push(trie[0][i]),fail[trie[0][i]]=0;while(!q.empty()){int u=q.front();q.pop();For(i,0,25){int &v=trie[u][i];if(v) fail[v]=trie[fail[u]][i],lst[v]=ed[fail[v]]?fail[v]:lst[fail[v]],q.push(v);else v=trie[fail[u]][i];}}
}il void solve(char *s){int len=strlen(s),p=0;For(i,0,len-1){ans[++tot]=s[i];p=trie[p][s[i]-'a'];for(int j=p;j;j=lst[j]) if(ed[j]){tot-=dep[j];p=stk[tot];break;}stk[tot]=p;}For(i,1,tot) printf("%c",ans[i]);
}il void init(){scanf("%s%d",t,&n);For(i,1,n)scanf("%s",s),insert(s);bfs();solve(t);
}int main(){init();return 0;
}

转载于:https://www.cnblogs.com/five20/p/9473449.html

P3121 [USACO15FEB]审查(黄金)Censoring (Gold)相关推荐

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

    https://www.luogu.org/problem/show?pid=3121#sub 首先题目看清楚 FJ注意到列表中的单词不会出现一个单词是另一个单词子串的情况,这意味着每个列表中的单词在 ...

  2. [USACO15FEB]审查(黄金)Censoring (Gold)

    AC自动机在预处理fail的时候预处理下节点对应的匹配串的长度 这样复杂度就是严格\(\Theta(n)\)的 只需要一个栈,在trie图上跑就可以了 #include"cstdio&quo ...

  3. 带你全面了解比特黄金(bitcoin gold)分叉

    比特币基于的区块链技术,每一笔交易都被存储在区块中,而区块的大小在初始时候固定在1M大小,而每笔交易的大小则为4k,比特币区块链每十分钟产生一个新块,那么这就以为着每秒只能处理3条交易记录,这在比特币 ...

  4. 黄金白银、古董与收藏

    1. 白金与黄金 根据国家贵金属首饰标准,只有铂金才可以称为白金.所以 18K 白金是一个错误的概念(或者说混淆视听的讲法).严格意义上应该称之为 18K 白色 K 金.18K 白色 K 金是 75% ...

  5. 周怎么换算成月_最大的黄金ETF出现增持,震荡周如何操作?

    黄金波动区间继续扩大,多空点位找到了! 这将是个金银震荡周的预判已经实现,接连几天的波动区间扩大,黄金单边上行动能已经消耗完毕,那么我们应该怎么操作? 继现货黄金昨日亚盘收报于1639.3美元后,美盘 ...

  6. 邓耀鑫:黄金短线继续下挫,黄金最新行情分析及操作建议

    投资即要巴菲特的价值投资法,更要索罗斯的反折射投机对冲,更要像西蒙斯的壁虎交易.交易要时刻保持一颗冷静的心态,和沉着的态度,最近黄金反反复复,但没有脱离如来佛祖的手撑,千万不要被周围追涨杀跌的氛围所干 ...

  7. python小游戏之黄金矿工

    推荐一个黄金矿工游戏(py原创),你能玩一整天!↓ ↓ ↓ 首先定义一些要用到的变量及列表 变量\列表名 用途 gold 记录挖了多少黄金 times 每次挖金的数量 energy 体力值 # 黄金列 ...

  8. 多头表现乏力 黄金技术面大周期数月持续盘整

    周一黄金开于1784.34美元/盎司,最高上探至1787.52美元/盎司,最低触及1775.51美元/盎司,收盘于1778.09美元/盎司,周二开于1778.10美元/盎司,现报于1779.85美元/ ...

  9. AC自动机(题目+模板)

    学习博客:https://www.cnblogs.com/hyfhaha/p/10802604.html AC自动机可以认为是kmp+trie树. trie树数组大小是字符串数目n*最大字符串长度mx ...

最新文章

  1. 6.2 sql安全性
  2. cc arm linux gcc,(80分请教):armcc 与 arm-elf-gcc的区别?
  3. .NET Entity Framework入门简介及简单操作
  4. 如何定义中文转语音的语气
  5. 关键七步,用Apache Spark构建实时分析Dashboard
  6. Gerrit plugin安装和删除
  7. 20个JS 小技巧超级实用
  8. CF321E Ciel and Gondolas BZOJ 5311 贞鱼
  9. 中国象棋计算机比赛,2019年象棋软件大赛:四核弱机(56核)vs天命智棋(48核)...
  10. 5G NR CDRX
  11. 嚯,这款AI建模工具实在太强大了,快来pick!
  12. SourceTree这是一个无效源路径(更改git账号和密码导致无效源路径看这里)
  13. 域名被抢注的知名案例有哪些?
  14. 模拟新浪微博随便看看界面布局
  15. 通过php上传和下载rar/zip压缩文件
  16. android APP自动增量更新
  17. 功放限幅保护_如何利用限幅器保护音箱
  18. 北京医保可报销的定点医院
  19. 分享一个stm8s003单片机的ADC转换,附加一个冒泡算法(用于减少误差)
  20. 大屏幕led无缝拼接显示屏的十大技术优势

热门文章

  1. centos 5 6安装本地yum源
  2. 用javascript判断当前是安卓平台还是ios平台
  3. 几个常用的宏:likely和unlikely __raw_writel
  4. Android中多媒体处理【转】
  5. 大佬的引用。。。。太猛了啊
  6. 规律的更新状态是一个好习惯
  7. aliyun tianchi ai contest meeting note
  8. 我的申请总结~好像创业公司啊
  9. String比较? 用==判断两个字符串,有时为true有时为false
  10. 遍历目录下的文件每250M打包一个文件