【bzoj3940】[Usaco2015 Feb]Censoring

【题目描述】

FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S。他有一个包含n个单词的列表,列表里的n个单词

记为t_1...t_N。他希望从S中删除这些单词。

FJ每次在S中找到最早出现的列表中的单词(最早出现指该单词的开始位置最小),然后从S中删除这个单词。他重复这个操作直到S中

没有列表里的单词为止。注意删除一个单词后可能会导致S中出现另一个列表中的单词

FJ注意到列表中的单词不会出现一个单词是另一个单词子串的情况,这意味着每个列表中的单词在S中出现的开始位置是互不相同的

请帮助FJ完成这些操作并输出最后的S

【样例输入】

begintheescapexecutionatthebreakofdawn

2

escape

execution

【样例输出】

beginthatthebreakofdawn

【题解】

和p3942一样的思路(连题目名字都一样),建议先写p3942,我可以说这个东西叫可持久化AC自动机吗?(并没有这个东西)

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<ctime>
 6 #include<cmath>
 7 #include<algorithm>
 8 using namespace std;
 9 #define MAXN 100010
10 int n,cnt,top,end[MAXN],fail[MAXN],deep[MAXN],f[MAXN],q[MAXN],stack[MAXN],tr[MAXN][27];
11 char b[MAXN],ch[MAXN];
12 void insert()
13 {
14     int now=0,len=strlen(ch+1);
15     for(int i=1;i<=len;i++)
16     {
17         if(!tr[now][ch[i]-‘a‘]) tr[now][ch[i]-‘a‘]=++cnt;
18         now=tr[now][ch[i]-‘a‘];
19     }
20     end[now]=max(end[now],len);
21 }
22 void build()
23 {
24     int head=0,tail=0;
25     for(int i=0;i<26;i++)  if(tr[0][i])  q[++tail]=tr[0][i];
26     while(++head<=tail)
27     {
28         int x=q[head];
29         for(int i=0;i<26;i++)
30         {
31             if(!tr[x][i])  tr[x][i]=tr[fail[x]][i];
32             else {fail[tr[x][i]]=tr[fail[x]][i]; q[++tail]=tr[x][i];}
33         }
34     }
35 }
36 void find()
37 {
38     int len=strlen(b+1);
39     for(int i=1,x=0;i<=len;i++)
40     {
41         f[i]=tr[f[stack[top]]][b[i]-‘a‘];
42         stack[++top]=i;
43         top-=end[f[i]];
44     }
45 }
46 int main()
47 {
48     //freopen("cin.in","r",stdin);
49     //freopen("cout.out","w",stdout);
50     scanf("%s%d",b+1,&n);
51     for(int i=1;i<=n;i++)  {scanf("%s",ch+1);  insert();}
52     build();
53     find();
54     for(int i=1;i<=top;i++)  printf("%c",b[stack[i]]);
55     return 0;
56 }
时间: 2024-08-10 14:00:46

【bzoj3940】[Usaco2015 Feb]Censoring的相关文章

【bzoj3939】[Usaco2015 Feb]Cow Hopscotch 动态开点线段树优化dp

题目描述 Just like humans enjoy playing the game of Hopscotch, Farmer John's cows have invented a variant of the game for themselves to play. Being played by clumsy animals weighing nearly a ton, Cow Hopscotch almost always ends in disaster, but this has

bzoj3942: [Usaco2015 Feb]Censoring

AC自动机.嗯bzoj3940弱化版.水过去了(跑的慢啊QAQ.想了想可以用hash写.挖坑 #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 clr(x,c) memset

BZOJ 3942: [Usaco2015 Feb]Censoring

3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 404  Solved: 221[Submit][Status][Discuss] Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they have plenty of material

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

3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MB 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

【Trie图】BZOJ3940-[Usaco2015 Feb]Censoring

[题目大意] 有一个匹配串和多个模式串,现在不断删去匹配串中的模式串,求出最后匹配串剩下的部分. [思路] 众所周知,KMP的题往往对应着一道AC自动机quq.本题同BZOJ3942(KMP),这里改成AC自动机即可. 我一开始写了原始的AC自动机,写挂了.后来思考了一下,应当用Trie图,机智地1A. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm&

【BZOJ3940】【Usaco2015 Feb】Censoring AC自动机

链接: #include <stdio.h> int main() { puts("转载请注明出处[vmurder]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/44960463"); } 题意: 题意同BZOJ3942,不过要删除的串是多串 http://blog.csdn.net/vmurder/article/details/44959895 题解: --思路一模一样,除了不用kmp用AC

Bzoj3940 [Usaco2015 Feb]Censoring

Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 391  Solved: 183 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

BZOJ 3942 Usaco2015 Feb Censoring KMP算法

题目大意:给定两个串A和B,要求将A中删掉所有的B后输出 为何BC群刚有人问完我这题的[C++语法基础题]版之后就出了个KMP版的= = 维护一个栈,将A中的字符依次加进去,一旦A的栈顶出现了B就弹栈 用KMP算法来加速这个过程即可 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 1001001 using namespace st

树的直径 【bzoj3363】[Usaco2004 Feb]Cow Marathon 奶牛马拉松

3363: [Usaco2004 Feb]Cow Marathon 奶牛马拉松 Description ? 最近美国过度肥胖非常普遍,农夫约翰为了让他的奶牛多做运动,举办了奶牛马拉松.马拉 松路线要尽量长,所以,告诉你农场的地图(该地图的描述与上题一致),请帮助约翰寻找两个 最远农场间的距离. Input ? 第1行:两个分开的整数N和M. ? 第2到M+1行:每行包括4个分开的内容,Fi,F2,L,D分别描述两个农场的编号,道路的长 度,F1到F2的方向N,E,S,W. Output ? 一个