Description

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

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

Output

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

Sample Input

begintheescapexecutionatthebreakofdawn
2
escape
execution

Sample Output

beginthatthebreakofdawn

Solution

一开始的确没啥思路……一看到有栈这个标签就明了了……
果然我还是水平太菜啊
不过一看到栈这个题就没什么思考难度了……就基本全靠细节了
首先把自动机建出来……话说我这几天才发现原来自己建的一直叫trie图
然后一位一位放到自动机上跑。
分两种情况:
1、栈空
   判断是否是根节点的一个儿子即可(即判断是否是单词的第一位)
2、栈不为空
   判断当前位能否和上一位匹配,如果不能的话就清空栈(因为有这一位挡着就已经前功尽弃了)
然后当当前位匹配到某一位的末尾后,就将这个单词从栈中清空(因为题目说了越靠前出现的越早清空)
清空栈的时候输出一下就好了(除了匹配到的单词不输出)

Code

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#define N (100000+10)
using namespace std;
int Fail[N],Son[N][],End[N];
int n,sz,maxn,stack[N],top;
char s[N],st[N],ch[N];
queue<int>q; void Insert(char s[])
{
int len=strlen(s),now=;
for (int i=;i<len;++i)
{
int x=s[i]-'a';
if (!Son[now][x]) Son[now][x]=++sz;
now=Son[now][x];
ch[now]=s[i];
}
End[now]=len;
} void Build_Fail()
{
for (int i=;i<;++i)
if (Son[][i])
q.push(Son[][i]);
while (!q.empty())
{
int now=q.front(); q.pop();
for (int i=;i<;++i)
{
if (!Son[now][i])
{
Son[now][i]=Son[Fail[now]][i];
continue;
}
Fail[Son[now][i]]=Son[Fail[now]][i];
q.push(Son[now][i]);
}
}
} void Compare(char s[])
{
int len=strlen(s);
for (int i=;i<len;++i)
{
int x=s[i]-'a';
if (!top)
{
if (Son[][x])
stack[++top]=Son[][x];
else
printf("%c",s[i]);
}
else
{
int son=Son[stack[top]][x];
if (Son[stack[top]][x])
stack[++top]=son;
else
{
for (int j=;j<=top;++j)
printf("%c",ch[stack[j]]);
printf("%c",s[i]);
top=;
}
}
if (End[stack[top]])
{
int t=End[stack[top]];
for (int i=;i<=t;++i)
top--;
}
}
for (int i=;i<=top;++i)
printf("%c",ch[stack[i]]);
} int main()
{
scanf("%s%d",s,&n);
for (int i=;i<=n;++i)
scanf("%s",st),Insert(st);
Build_Fail();
Compare(s);
}

BZOJ3940:[USACO]Censoring(AC自动机,栈)的更多相关文章

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

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

  2. 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈

    [BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...

  3. 【bzoj3940】[Usaco2015 Feb]Censoring AC自动机

    题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they h ...

  4. BZOJ 3940: [Usaco2015 Feb]Censoring AC自动机_栈

    Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so ...

  5. 洛谷 P3121 [USACO15FEB]审查(黄金)Censoring (Gold) 【AC自动机+栈】

    这个和bzoj同名题不一样,有多个匹配串 但是思路是一样的,写个AC自动机,同样是开两个栈,一个存字符,一个存当前点在trie树上的位置,然后如果到了某个匹配串的末尾,则弹栈 #include< ...

  6. CENSORING——AC 自动机

    题目 [题目描述] FJ 为它的奶牛订阅了很多杂志,balabala.......,其中有一些奶牛不宜的东西 (比如如何煮牛排). FJ 将杂志中所有的文章提取出来组成一个长度最多为 $ 10^5 $ ...

  7. 【USACO】AC自动机

    Description 对,这就是裸的AC自动机. 要求:在规定时间内统计出模版字符串在文本中出现的次数. Input 第一行:模版字符串的个数N. 第2->N+1行:N个字符串.(每个模版字符 ...

  8. BZOJ3940: [Usaco2015 Feb]Censoring (AC自动机)

    题意:在文本串上删除一些字符串 每次优先删除从左边开始第一个满足的 删除后剩下的串连在一起重复删除步骤 直到不能删 题解:建fail 用栈存当前放进了那些字符 如果可以删 fail指针跳到前面去 好菜 ...

  9. cjoj P1435 - 【模板题 USACO】AC自动机 && 洛谷 P3796 【模板】AC自动机(加强版)

    又打了一遍AC自动稽. 海星. 好像是第一次打trie图,很久以前就听闻这个思想了.OrzYYB~ // It is made by XZZ #include<cstdio> #inclu ...

随机推荐

  1. JavaEE 7 最全教程集锦(转)

    转自 http://www.iteye.com/news/28009 甲骨文公司已经在6月份正式发布了JavaEE 7,该版本带来了诸多新的规范及特性,将企业级开发提升到了一个新的层次. Java E ...

  2. java--多线程之后台线程

    public class ThreadDaemon { /** * @param args * 后台线程在主进程结束后,也会退出 */ public static void main(String[] ...

  3. HTTP中的响应协议及302、304的含义

    响应协议 HTTP/1.1 200 OK:响应协议为HTTP1.1,状态码为200,表示请求成功,OK是对状态码的解释: Server: Apache-Coyote/1.1:服务器的版本信息: Con ...

  4. MongoDB 线上环境按照及配置(授权方式启动)

    1创建文件repo文件 #vim /etc/yum.repos.d/mongodb-org-3.4.repo [mongodb-org-3.4] name=MongoDB Repository bas ...

  5. Centos7 linux 安装 redis 遇到的几个问题

    环境:centos7 + redis 5.0  解决方案仅供参考,如不能解决问题,请查找请他方案.     1.不能编译没有GCC 编译工具 make报错: make[1]: [persist-set ...

  6. Java面试题之HashMap阿里面试必问知识点,你会吗?

    面试官Q1:你用过HashMap,你能跟我说说它的数据结构吗? HashMap作为一种容器类型,无论你是否了解过其内部的实现原理,它的大名已经频频出现在各种互联网Java面试题中了.从基本的使用角度来 ...

  7. Nginx 504错误总结

    Nginx 504错误(Gateway time-out  网关超时)的含义是所请求的网关没有请求到,简单来说就是没有请求到可以执行的PHP-CGI. 一般看来, 这种情况可能是由于nginx默认的f ...

  8. css-布局定位

    position:  ** 属性值 - absolute: *** 将对象从文档流中拖出 *** 可以是top.bottom等属性进行定位 - relative: *** 不会将对象从文档流中拖出 * ...

  9. 11.6NOIP模拟赛解题报告

    心路历程 预计得分:\(100 + 100 + 100 = 300\) 实际得分:\(100 +100 +100 = 300\) 学OI两年终于AK了一次qwq(虽然题目炒鸡水..) 纪念一下这令人激 ...

  10. Java 文件上传与下载、email

    1. 文件上传与下载 1.1 文件上传 文件上传,要点: 前台: 1. 提交方式:post 2. 表单中有文件上传的表单项: <input type="file" /> ...