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. table中列复选框全选,再选 效果

    <table class="table table-striped sortable table-bordered table-hover " id="zdnews ...

  2. MDI-设置子窗体只能弹出一个--单例模式

    不足之处,欢迎指正! 什么是MDI..我表示不知道的呢. MDI(Multiple Document Interface)就是所谓的多文档界面,与此对应就有单文档界面 (SDI), 它是微软公司从Wi ...

  3. ActiveMQ整合spring结合项目开发流程(生产者和消费者)总结

    一:生产者代码编写: 1.配置pom.xml引入相关坐标 <dependencies> <!-- spring开发测试 --> <dependency> <g ...

  4. JavaScript事件流--事件冒泡、目标与事件捕获

    1.事件冒泡 微软提出了名为事件冒泡的事件流.事件冒泡可以形象地比喻为把一颗石头投入水中,泡泡会一直从水底冒出水面.也就是说,事件会从最内层的元素开始发生,一直向上传播,直到document对象. 因 ...

  5. jquery中的$().each和$.each的区别

    jquery中的$().each和$.each的区别 注意:jquery中的$().each和$.each的区别,前者只能遍历数组,后者可以遍历数组和对象 备注:sinobook项目中地名本体相关地按 ...

  6. Mybatis学习随笔

    学习Mybatis路径(适合有java基础和mysql基础的小伙伴) 1.把项目搭建起来,跑一跑感受一下 2.测试基本映射 3.测试高级映射 4.测试动态sql 5.学习懒加载与缓存 6.与sprin ...

  7. Yii2.0 新建项目通用准备工作

    1.设置 cookieValidationKey 在 config/web.php 中 config 里有 components项中request有个cookieValidationKey需要配置参数 ...

  8. easyui window窗口 不垂直居中

    $('#hotelDetailWrap').window({ width: 1040, height: 160, collapsible: false, minimizable: false, max ...

  9. 解决Non-resolvable parent POM: Could not find artifact 出现的问题

    在编译spring boot 多模块项目的时候,往往出现 Non-resolvable parent POM: Could not find artifact 后面跟一串其它信息,网上大部分解决方案是 ...

  10. Java学习笔记——关于位运算符的问题

    我就之直接贴图了!不想排版了! 有什么问题,欢迎大家指出,帮助我提高,谢谢!