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
  1.  

Input

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.
第一行包含一个字符串S 
第二行包含一个整数N 
接下来的N行,每行包含一个字符串,第i行的字符串是t_i
  1.  

Output

The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.
一行,输出操作后的S
 
 

Sample Inputbegintheescapexecutionatthebreakofdawn 2 escape execution

Sample Outputbeginthatthebreakofdawn

题意:给N个字符串,设集合为T,以及一个长字符串S,对于S,一直删第一个字串,如果其存在T集合里,则删去,问删到最后S为?

思路:N个字符串建立AC自动机,然后S在自动机上面跑,如果跑到的位置是“关键点”,则删去,同时跑的位置跑到删去后AC自动机对应的位置。

所以记录当前串和每个长度对应的位置即可。(送分题

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn=;
  4. char c[maxn],s[maxn],ans[maxn];
  5. int ch[maxn][],End[maxn],fail[maxn],q[maxn];
  6. int pos[maxn],dep[maxn],head,tail,cnt;
  7. void insert()
  8. {
  9. int Now=,d=; for(int i=;c[i];i++){
  10. d++;
  11. if(!ch[Now][c[i]-'a']) ch[Now][c[i]-'a']=++cnt,dep[cnt]=d;
  12. Now=ch[Now][c[i]-'a'];
  13. } End[Now]=;
  14. }
  15. void failbuild()
  16. {
  17. for(int i=;i<;i++) if(ch[][i]) q[++head]=ch[][i];
  18. while(tail<head){
  19. int Now=q[++tail];
  20. for(int i=;i<;i++){
  21. if(ch[Now][i]) {
  22. fail[ch[Now][i]]=ch[fail[Now]][i];
  23. q[++head]=ch[Now][i];
  24. }
  25. else ch[Now][i]=ch[fail[Now]][i];
  26. }
  27. }
  28. }
  29. int main()
  30. {
  31. int N,L,i,j;
  32. scanf("%s%d",s+,&N);
  33. for(i=;i<=N;i++){
  34. scanf("%s",c+);
  35. insert();
  36. }
  37. failbuild();
  38. int top=,Now=;
  39. for(i=;s[i];i++){
  40. ans[++top]=s[i];
  41. Now=ch[Now][s[i]-'a'];
  42. pos[top]=Now;
  43. if(End[Now]){
  44. top-=dep[Now]; Now=pos[top];
  45. }
  46. }
  47. for(i=;i<=top;i++) printf("%c",ans[i]);
  48. return ;
  49. }

BZOJ-3940:Censoring(AC自动机裸题)的更多相关文章

  1. AC自动机裸题

    HDU 2222 Keywords Search 模板题.对模式串建立AC自动机然后在trie树上找一遍目标串即可. # include <cstdio> # include <cs ...

  2. HDU 2222 AC自动机 裸题

    题意: 问母串中出现多少个模式串 注意ac自动机的节点总数 #include <stdio.h> #include <string.h> #include <queue& ...

  3. HDU 3065 AC自动机 裸题

    中文题题意不再赘述 注意 失配数组 f  初始化一步到位 #include <stdio.h> #include <string.h> #include <queue&g ...

  4. HDU 2896 AC自动机 裸题

    中文题题意不再赘述 注意字符范围是可见字符,从32开始到95 char c - 32 #include <stdio.h> #include <string.h> #inclu ...

  5. [Usaco2015 Feb]Censoring --- AC自动机 + 栈

    bzoj 3940 Censoring 题目描述 FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S. 他有一个包含n个单词的列表,列表里的n个单词记为T1......Tn. ...

  6. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  7. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  8. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  9. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

随机推荐

  1. 淘宝数据库OceanBase SQL编译器部分 源代码阅读--解析SQL语法树

    OceanBase是阿里巴巴集团自主研发的可扩展的关系型数据库,实现了跨行跨表的事务,支持数千亿条记录.数百TB数据上的SQL操作. 在阿里巴巴集团下,OceanBase数据库支持了多个重要业务的数据 ...

  2. VIM中保存编辑的只读文件

    如何在VIM中保存编辑的只读文件 你是否会和我一样经常碰到这样的情景:在VIM中编辑了一个系统配置文件,当需要保存时才发现当前的用户对该文件没有写入的权限.如果已 经做了很多修改,放弃保存的确很懊恼, ...

  3. oracle角色(role)概念

    一个角色是一组特权,它可以授权给用户或其它角色. 特权有:create table,select on boss ,create session,insert on boss,update on bo ...

  4. 【BZOJ3435】[Wc2014]紫荆花之恋 替罪点分树+SBT

    [BZOJ3435][Wc2014]紫荆花之恋 Description 强强和萌萌是一对好朋友.有一天他们在外面闲逛,突然看到前方有一棵紫荆树.这已经是紫荆花飞舞的季节了,无数的花瓣以肉眼可见的速度从 ...

  5. 【BZOJ3956】Count 主席树+单调栈

    [BZOJ3956]Count Description Input Output Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N ...

  6. NVR硬件录像机web无插件播放方案功能实现之相关接口注意事项说明

    该篇博文主要用来说明EasyNVR硬件录像回放版本的相关接口说明和调用的demo: 方便用户的二次开发和集成. 软件根目录会包含接口文档的,因此,本文主要是对一些特定接口的说明和接口实现功能的讲解以及 ...

  7. 九度OJ 1157:中位数 (中位数、排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2188 解决:1294 题目描述: 中位数定义:一组数据按从小到大的顺序依次排列,处在中间位置的一个数(或最中间两个数据的平均数). 给出一 ...

  8. 九度OJ 1151:位操作练习 (位操作)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1679 解决:924 题目描述: 给出两个不大于65535的非负整数,判断其中一个的16位二进制表示形式,是否能由另一个的16位二进制表示形 ...

  9. Javascript模块化编程-初识[1]

    JS模块化编程,已经成为一个迫切的需求.理想情况下,开发者只需要实现核心业务逻辑,其他都可以加载别人已经写好的模块. 但是,JS不是一种模块化编程语言,它不支持类,所以没有严格意义上的模块.为了实现模 ...

  10. php微信支付测试开发(流程已通)

    必要条件: appid //公众号后台开发者中心获得(和邮件内的一样)   mchid//邮件内获得  key//商户后台自己设置  appsecret //公众号开发者中心获得 两个证书文件,邮件内 ...