AC自动机。为什么洛谷水题赛会出现这种题然而并不会那么题意就不说啦 。终于会写AC自动机判断是否是子串啦。。。用到kmp的就可以用AC自动机水过去啦

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
const int nmax=100005;
const int maxn=27;
const int inf=0x7f7f7f7f;
int ch[nmax][maxn],fail[nmax],F[nmax],T[nmax],pt=0;
char s[nmax],t[nmax],S[nmax];
void insert(int x){
int len=strlen(s),t=0;
rep(i,0,len-1) {
if(!ch[t][s[i]-'a']) ch[t][s[i]-'a']=++pt;
t=ch[t][s[i]-'a'];
}
F[t]=len;
}
void getfail(){
queue<int>q;fail[0]=0;q.push(0);
while(!q.empty()){
int x=q.front();q.pop();
rep(i,0,25) {
if(ch[x][i]) q.push(ch[x][i]),fail[ch[x][i]]=x==0?0:ch[fail[x]][i];
else ch[x][i]=x==0?0:ch[fail[x]][i];
}
}
}
int main(){
scanf("%s",t+1);
int n;scanf("%d",&n);
rep(i,1,n) scanf("%s",s),insert(i);
getfail();
//rep(i,0,pt) printf("%d ",F[i]);printf("\n");
//rep(i,0,pt) printf("%d ",fail[i]);printf("\n");
int len=strlen(t+1),x=0,top=0;
rep(i,1,len){
S[++top]=t[i];
x=ch[x][t[i]-'a'];T[top]=x;
if(F[x]) top-=F[x],x=T[top];
//printf("%d",x);
}
rep(i,1,top) printf("%c",S[i]);printf("\n");
return 0;
}

  

3940: [Usaco2015 Feb]Censoring

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 253  Solved: 126
[Submit][Status][Discuss]

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

HINT

 

Source

[Submit][Status][Discuss]

bzoj3940: [Usaco2015 Feb]Censoring的更多相关文章

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

    题目传送门 题目描述: FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过105的字符串S.他有一个包含n个单词的列表,列表里的n个单词记为t1…tN.他希望从S中删除这些单词.FJ每次在S中 ...

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

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

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

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

  4. 【BZOJ3940】[USACO2015 Feb] Censoring (AC自动机的小应用)

    点此看题面 大致题意: 给你一个文本串和\(N\)个模式串,要你将每一个模式串从文本串中删去.(此题是[BZOJ3942][Usaco2015 Feb]Censoring的升级版) \(AC\)自动机 ...

  5. 3942: [Usaco2015 Feb]Censoring [KMP]

    3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 375  Solved: 206[Subm ...

  6. 3942: [Usaco2015 Feb]Censoring

    3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Submit: 964 Solved: 480 [Subm ...

  7. bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机

    3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MB Description Farmer John has ...

  8. BZOJ 3940: [Usaco2015 Feb]Censoring

    3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 367  Solved: 173[Subm ...

  9. Bzoj 3942: [Usaco2015 Feb]Censoring(kmp)

    3942: [Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hooveske ...

随机推荐

  1. 跟着PHP100第一季学写一个CMS(1-10)

    笔记: 这次用的方法是先跟着视频做一遍,隔一天或半天后独立再做一遍,能发现真正不会的地方记录下来. CMS0.1界面布局1.问题:分两个css来实现时basic.css+index.php出现定位不正 ...

  2. MSP430F4152串口操作

    /**********************************************************************/  /*  名称:串口通讯 功能:将接到的数据组后原封不 ...

  3. thinkphp操作数据库

    1.实现or操作: $where=array( 'city'=>array('like',array('%'.$_GET['city'].'%')); 'hangye'=>array('l ...

  4. python装饰器总结

    一.装饰器是什么 python的装饰器本质上是一个Python函数,它可以让其他函数在不需要做任何代码变动的前提下增加额外功能,装饰器的返回值也是一个函数对象.简单的说装饰器就是一个用来返回函数的函数 ...

  5. (转)C#与Android通过adb实现usb通讯

    转自:http://blog.csdn.net/linweidong/article/details/6273507 需求: Android的apk获取手机信息,把结果发给PC client 注意地方 ...

  6. Perl中的特殊内置变量详解

    #!/usr/bin/perl -w @array = qw(a b c d); foreach (@array) { print $_," "; } 例子的作用就是定义一个数组并 ...

  7. nginx低版本不支持pathinfo模式,thinkphp针对此问题的解决办法

    将一个thinkphp项目从apache环境移到nginx1.2上,怎奈,nginx这个版本默认不支持pathinfo模式 首先,编辑nginx的虚拟主机配置文件 location ~ .*.(php ...

  8. Xcode和github入门详细教程

    Xcode和github详细教程! 主要是参考了现在网上的一些资料给没整过的人一个详细的指南. (1)先在github上注册账号,自行解决! (2)在导航栏右上角new一个repository(仓库) ...

  9. 常见架构TLB miss处理方法(转)

    转自网站:http://blog.sina.com.cn/s/blog_633f462901018reb.html 0.       综述 总的来说TLB miss处理分为硬件处理和软件处理两种,硬件 ...

  10. 839. Optimal Marks - SPOJ

    You are given an undirected graph G(V, E). Each vertex has a mark which is an integer from the range ...