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. StrangeIoc框架学习

    StrangeIoc是一款基于MVCS的一种框架,是对MVC思想的扩展,是专门针对Unity3D开发的一款框架,非常好用. 一.MVCS分别代表什么 MVCS框架是一种模块的分离,一种写代码的规则,目 ...

  2. RequireJs使用快速入门

    前言:Requirejs作为一个ES5环境流行的模块加载器,在很多项目中使用它.而且这个开源库任然在更新,同类产品seajs已经不更新了. ES6之后引入import 或者使用Commonjs的方式引 ...

  3. jquery 使用整理机制

    短路表达式 与 多重短路表达式 短路表达式这个应该人所皆知了.在 jQuery 中,大量的使用了短路表达式与多重短路表达式. 短路表达式:作为"&&"和" ...

  4. 将JavaScript语句插入HTML文档

    (1) 使用 <SCRIPT> 标签将语句嵌入文档 <script type="text/javascript"> function Que() { } & ...

  5. document文档流详解

    html页面下载完默认会打开一个文档流document对象(调用document.open,此时浏览器标题左边会显示加载中图标),开始从上往下渲染内容,渲染完成调用document.close关闭渲染 ...

  6. C#中引用类型和值类型的区别,分别有哪些

    C#的值类型包括:结构体(数值类型,bool型,用户定义的结构体),枚举,可空类型. C#的引用类型包括:数组,用户定义的类.接口.委托,object,字符串. 数组的元素,不管是引用类型还是值类型, ...

  7. 虚拟机下centos时间不正确的方便解决方法

    就是用NTP了,通过外部的服务同步时间. ntpdate us.pool.ntp.org | logger -t NTP 如果没有ntpdate ,可以使用 yum install ntpdate 进 ...

  8. 读EntityFramework.DynamicFilters源码_心得_单元测试03

    上个星期我们只是显示了一个示例,怎么在EF的框架内,注入我们拓展的动态过滤器 第一步:安装EntityFramework.DynamicFilters 第二步:重写OnModelCreating方法 ...

  9. ajax接收flask传递的json数据

    from flask import Flask, request import json app = Flask(__name__) @app.route('/') def func(): res = ...

  10. Python基础-月考

    1. 8<<2等于? # 解释:将8按位左移2位 # 8 0 0 0 0 1 0 0 0 # 32 0 0 1 0 0 0 0 0 2. 通过内置函数计算5除以2的余数 print(div ...