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 Inputbegintheescapexecutionatthebreakofdawn 2 escape execution

Sample Outputbeginthatthebreakofdawn

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

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

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

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
char c[maxn],s[maxn],ans[maxn];
int ch[maxn][],End[maxn],fail[maxn],q[maxn];
int pos[maxn],dep[maxn],head,tail,cnt;
void insert()
{
int Now=,d=; for(int i=;c[i];i++){
d++;
if(!ch[Now][c[i]-'a']) ch[Now][c[i]-'a']=++cnt,dep[cnt]=d;
Now=ch[Now][c[i]-'a'];
} End[Now]=;
}
void failbuild()
{
for(int i=;i<;i++) if(ch[][i]) q[++head]=ch[][i];
while(tail<head){
int Now=q[++tail];
for(int i=;i<;i++){
if(ch[Now][i]) {
fail[ch[Now][i]]=ch[fail[Now]][i];
q[++head]=ch[Now][i];
}
else ch[Now][i]=ch[fail[Now]][i];
}
}
}
int main()
{
int N,L,i,j;
scanf("%s%d",s+,&N);
for(i=;i<=N;i++){
scanf("%s",c+);
insert();
}
failbuild();
int top=,Now=;
for(i=;s[i];i++){
ans[++top]=s[i];
Now=ch[Now][s[i]-'a'];
pos[top]=Now;
if(End[Now]){
top-=dep[Now]; Now=pos[top];
}
}
for(i=;i<=top;i++) printf("%c",ans[i]);
return ;
}

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. python 和 mysql连接

    python 和 mysql连接 虫师教程:http://www.cnblogs.com/fnng/p/3565912.html 其他教程pymysql:http://www.cnblogs.com/ ...

  2. PHP RSA加解密示例(转)

    1.生成密钥和公钥 开始前需要准备openssl环境 linux 需要安装openssl工具包,传送门http://www.openssl.org/source/ window 下需要安装openss ...

  3. Bootstrap学习速查表(三) 表单

    表单中常见的元素主要包括:文本输入框.下拉选择框.单选按钮.复选按钮.文本域和按钮等. 一.基础表单 1.初始化:对于基础表单,Bootstrap并未对其做太多的定制性效果设计,仅仅对表单内的fiel ...

  4. ss请cc来家里钓鱼,鱼塘可划分为n*m的格子,每个格子有不同的概率钓上鱼,cc一直在坐标(x,y)的格子钓鱼,而ss每分钟随机钓一个格子。问t分钟后他们谁至少钓到一条鱼的概率大?为多少?

    include "stdafx.h" #include<iostream> #include<vector> #include<math.h> ...

  5. mongodb的启动参数--quiet

    ”mongo群友在群里问了个问题,问的是--quiet启动参数如何用的? 如何理解安静的输出?“ 看到这个问题,之前看过--quiet这个参数,没有认真研究过,也没在生产中使用过. 在mongodb启 ...

  6. Java提高(二)---- HashTable

    阅读博客 java提高篇(二五)—–HashTable 这篇博客由chenssy 发表与2014年4月,基于源码是jdk1.7 ========================== 本文针对jdk1. ...

  7. php中的字符串和正則表達式

    一.字符串类型的特点 1.PHP是弱类型语言,其它数据类型一般都能够直接应用于字符串函数操作. 1: <? php 2: echo substr("123456",2,4); ...

  8. 在WPF对话框中如何验证用户提供的数据

    在WPF中,MS在msdn的WPF应用程序开发中对用户输入的数据验证做了示范,基本思想就是添加各种类型的校验规则,比如最大最小值.字符串长度.是否为空等等,在后在界面绑定数据时添加数据字段的校验.这样 ...

  9. JBoss AS 7之域名直接訪问配置(The Return Of The King)

    1.6域名直接訪问配置 部署JBoss的时候,默认情况下訪问是须要加项目名字才可以訪问的. 可是怎样才可以"IP+port"訪问呢?以下将解说怎样配置. 配置域名訪问须要在项目以下 ...

  10. springcloud微服务实战--笔记--1、基础知识

    微服务的问题: 分布式事务和数据一致性. 由于分布式事务本身第实现难度就非常大,所以在微服务架构中,我们更强调在各服务之间进行无事务第调用,而对于数据一致性,只要求数据在最后第处理状态是一致第即可:若 ...