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. Codeforces 543E. Listening to Music

    Description 题面 Solution 分块套分块,分别对时间和位置进行分块 差不多是一个定期保存信息的方法 对于询问我们不妨求出 \(>=x\) 的答案,然后用 \(m-(>=x ...

  2. Firebird SEQUENCE

    Firebird3 以后可以有自增列,也可以类似Oracle.Postgresql手动添加序列,产生新值,灵活操作. 创建序列: INCREMENT ; 修改序列最大值: ; 产生新值: 1. ) f ...

  3. (C++学习)关于CString的一些疑问

    #include <iostream> #include <string> #include <afx.h> #include <vector> usi ...

  4. 第2天:JavaScript基础(运算符、案例、循环、冒泡以及prompt提示输入框)

    一元运算在前在后的区别 加加 var num1 = 10; //++在后面 先参与运算 再自加1 var sum1 = num1++ +10; console.log("sum1的值:&qu ...

  5. Django之路由、模板和模型系统 (转载)

    一.路由系统 浏览器会自动给url后加一个“/” django会自动给路由的正则表达式前面加一个“/” django会给任何不带“/”结尾的url语句添加“/”(可设置) 短路路由规则:匹配到第一条就 ...

  6. 《第一行代码》Android特色开发,基于位置服务,出现的问题

    手机GPS定位较慢.精度高.耗电量多,网络定位较快.精度低.耗电量少 当位置精度要求非常高的时候,使用GPS定位:一般情况下,使用网络定位. 按<第一行代码>写了一个定位程序,真机一直没有 ...

  7. websocket 和 dwr 做web端即时通信

    一.WebSocket是HTML5出的东西(协议),也就是说HTTP协议没有变化,或者说没关系,但HTTP是不支持持久连接的(长连接,循环连接的不算) 首先HTTP有1.1和1.0之说,也就是所谓的k ...

  8. javascript获取元素样式值

    使用css控制页面有4种方式,分别为行内样式(内联样式).内嵌式.链接式.导入式. 行内样式(内联样式)即写在html标签中的style属性中,如<div style="width:1 ...

  9. Goodbye Bingshen

    在uoj上打的第二场比赛......还凑合(卧槽C题80分没了QAQ 第一次接触交互题还挺好玩的哈哈 可能是人比较多吧.....rating涨了不少...... 现在我rating正好比lrd高1哈哈 ...

  10. ArcSDE 常用命令

    一.sdeservice命令: 1. 创建sde服务:sdeservice –o create ArcSDE常用操作命令(转): 启动cmd 1. 创建和删除ArcSDE服务操作命令(sdeservi ...