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. putty 代理设置

    代理分 http 和 socket 代理. IE Internet属性-> 连接 -> 局域网设置 普通HTTP代理 直接输入IP,端口 socket 代理  点高级 套接字设置 安装好P ...

  2. ubuntu1304下安装boa服务器

    本测试在ubuntu1304下测试,具体步骤如下: 1下载源码:www.boa.org,可在ubuntu下自带的火狐浏览器下载,也可在window下下载,然后再移到ubuntu下: 2打开终端,将bo ...

  3. [Learn Android Studio 汉化教程]第二章:Android Studio概述(一)

    [Learn Android Studio ]第二章:Android Studio概述(一) Android Studio是一个视窗化的开发环境.为了充分利用有限的屏幕空间,不让你束手束脚,Andro ...

  4. 八、mysql视图、存储过程、函数以及时间调度器

    .create or replace view emp_view as select * from t4 ;给t4表创建一个名为emp_view的视图 .drop view emp_view 删除视图 ...

  5. eclipse 僵死/假死 问题排查及解决

    症状: 使用Eclipse win 64位版本,indigo及kepler都重现了,使用tomcat 6.0.39,jdk1.6.u45及1.7u45均尝试了,也重现. 重现步骤很简单,使用debug ...

  6. easy ui easyui-linkbutton 禁用、启用

    <a id="btn_update_Shop" name="btn_update_Shop" class="easyui-linkbutton& ...

  7. ORA-01034:oracle不可用 的解决方法

    晚上打开SQLPlus输入用户名和密码老是登不了,出现如上一行代码 "ORA-01034:ORACLE不可用"的错误 解决思路: 1.检查服务是否启动, 2.如果已经启动,说明数据 ...

  8. iOS 本地存储四种方法

    在iOS开发过程中,不管是做什么应用,都会碰到数据保存的问题.将数据保存到本地,能够让程序的运行更加流畅,不会出现让人厌恶的菊花形状,使得用户体验更好.下面介绍⼀一下数据保存的方式: 1.NSKeye ...

  9. Telerik_2012_Q3 RadGrid 汉化

    ChineseRadGridLocalizationProvider.cs using System; using System.Collections.Generic; using System.L ...

  10. 简单3d RPG游戏 之 003 怪物AI

    游戏中,怪物会自动的往玩家所在地点走去,那需要创建一个C#脚本EnemyAI,包含两个功能: 1. 怪物旋转自己对准玩家 2. 怪物向前移动,追逐玩家 public class EnemyAI : M ...