【BZOJ3942】[Usaco2015 Feb]Censoring

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^6 characters. From this, he would like to remove occurrences of a substring T to censor the inappropriate content. To do this, Farmer John finds the _first_ occurrence of T in S and deletes it. He then repeats the process again, deleting the first occurrence of T again, continuing until there are no more occurrences of T in S. Note that the deletion of one occurrence might create a new occurrence of T that didn't exist before.

Please help FJ determine the final contents of S after censoring is complete

有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程。

Input

The first line will contain S. The second line will contain T. The length of T will be at most that of S, and all characters of S and T will be lower-case alphabet characters (in the range a..z).

Output

The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.

Sample Input

whatthemomooofun
moo

Sample Output

whatthefun
题解:本题可以用KMP来做,不过hash也可以,方法是用栈来储存字符,一旦发现栈顶的hash值等于T串,就弹栈。
#include<stdio.h>
#include<string.h>
unsigned long long hs[1000010],ht;
unsigned long long seed[1000010];
int n,m;
int top;
char S[1000010],stack[1000010],T[1000010];
void BKDR()
{
seed[1]=131;
ht=T[0];
int i;
for(i=1;i<m;i++)
{
ht=ht*seed[1]+T[i];
seed[i+1]=seed[i]*seed[1];
}
}
int main()
{
scanf("%s%s",S,T);
n=strlen(S);
m=strlen(T);
BKDR();
int i,j;
for(i=0;i<m;i++)
{
stack[top++]=S[i];
hs[top]=hs[top-1]*seed[1]+S[i];
}
for(i=m;i<n;i++)
{
while(top>=m&&hs[top]-hs[top-m]*seed[m]==ht)
for(j=0;j<m;j++)
stack[--top]='\0';
stack[top++]=S[i];
hs[top]=hs[top-1]*seed[1]+S[i];
}
while(top>=m&&hs[top]-hs[top-m]*seed[m]==ht)
for(j=0;j<m;j++)
stack[--top]='\0';
printf("%s",stack);
return 0;
}

【BZOJ3940】[Usaco2015 Feb]Censoring

题意:本题和上题一样,只是有多个T串,那么就不能用KMP和hash来做,要用AC自动机。

注意最后任意两个单词都没有包含关系,所以处理的时候不需要沿着fail树一直找,否则会TLE。

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int maxn=100010;
struct node
{
int fail,ch[26],cnt;
}p[maxn];
char str[maxn],w[maxn],ans[maxn];
int n,tot,len,sum;
int pos[maxn],q[maxn],l,r;
void build()
{
int i,u,t;
l=1;
q[++r]=1;
while(l<=r)
{
u=q[l++];
for(i=0;i<26;i++)
{
if(!p[u].ch[i])
{
if(u==1) p[u].ch[i]=1;
else p[u].ch[i]=p[p[u].fail].ch[i];
continue;
}
q[++r]=p[u].ch[i];
if(u==1)
{
p[p[u].ch[i]].fail=1;
continue;
}
t=p[u].fail;
while(!p[t].ch[i]&&t) t=p[t].fail;
if(t) p[p[u].ch[i]].fail=p[t].ch[i];
else p[p[u].ch[i]].fail=1;
}
}
}
void search()
{
int i,j,u,t;
u=1;
pos[0]=1;
for(i=0;i<len;i++)
{
ans[++sum]=str[i];
pos[sum]=p[pos[sum-1]].ch[str[i]-'a'];
if(p[pos[sum]].cnt) sum-=p[pos[sum]].cnt;
}
for(i=1;i<=sum;i++) putchar(ans[i]);
}
int main()
{
scanf("%s",str);
scanf("%d",&n);
len=strlen(str);
int i,j,k,u;
tot=1;
for(i=1;i<=n;i++)
{
scanf("%s",w);
k=strlen(w);
u=1;
for(j=0;j<k;j++)
{
if(!p[u].ch[w[j]-'a']) p[u].ch[w[j]-'a']=++tot;
u=p[u].ch[w[j]-'a'];
}
p[u].cnt=k;
}
build();
search();
}

【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈的更多相关文章

  1. 【bzoj3940】[Usaco2015 Feb]Censoring AC自动机

    题目描述 Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so they h ...

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

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

  3. [Usaco2015 Feb]Censoring --- AC自动机 + 栈

    bzoj 3940 Censoring 题目描述 FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S. 他有一个包含n个单词的列表,列表里的n个单词记为T1......Tn. ...

  4. BZOJ 3940: [Usaco2015 Feb]Censoring AC自动机_栈

    Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so ...

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

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

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

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

  7. 【bzoj3940】[Usaco2015 Feb]Censoring

    [题目描述] FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过10^5的字符串S.他有一个包含n个单词的列表,列表里的n个单词 记为t_1...t_N.他希望从S中删除这些单词.  FJ每次 ...

  8. BZOJ3942: [Usaco2015 Feb]Censoring 栈+KMP

    Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so ...

  9. BZOJ3942 [Usaco2015 Feb]Censoring

    维护一个栈...如果栈顶出现了要被删除的字符串就全删掉就好了,判断的话...kmp就行了 /****************************************************** ...

随机推荐

  1. (转载) 利用国内的镜像,加速PIP下载

    国内源: 新版ubuntu要求使用https源,要注意. 清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.c ...

  2. Eclipse使用技巧

    1,整体缩进 右缩进:选中+Tab 左缩进:选中+ Shift+Tab 2,Ctrl+O列出当前类所有方法和属性

  3. JAVA 各种数值类型最大值和最小值 Int, short, char, long, float,&nbs

    转载地址:http://blog.sina.com.cn/s/blog_5eab3d430101fdv6.html 代码片段: fmax = Float.MAX_VALUE; fmin = Float ...

  4. 关于angularjs指令

    一个指令用来引入新的HTML语法.指令是DOM元素上的标记,使元素拥有特定的行为.举例来说,静态的HTML不知道如何来创建和展现一个日期选择器控件.让HTML能识别这个语法,我们需要使用指令.指令通过 ...

  5. sublime自动生成头部注释

    1.在tool->new snippet-创建一个新的snippet sublime text2 用snippet 创建文件头部信息 Snippets are smart templates t ...

  6. css写出0.5px边框(一)

    在移动端会出现线条太粗的现象,简单来说,是因为手机端的像素单位和ui的图比例是2:1,所以ui图的1px边框对我们来说就是0.5px,但是浏览器渲染的最小单位就是1px,下面给几种方法用css写出0. ...

  7. java关键字extends(继承)、Supe(父类引用空间)、 This(方法调用者对象)、Instanceof(实例类型-判断对象是否属于某个类)、final(最终)、abstract(抽象) 、interface(接口)0

    java 继承使用关键字extends   继承的作用:减少代码量,优化代码 继承的使用注意点: 1子类不能继承父类的私有变量 2.子类不能继承父类的构造方法 3.子类在调用自己的构造方法时 会默认调 ...

  8. Android Studio导入项目问题小结

    1. import project 之后一直停留在 building 界面 解决方案: 1.随便找一个你能运行的as项目 2.打开gradle-wrapper.properties,文件目录:项目/g ...

  9. 【转】Js获取当前日期时间及格式化操作

    (转自:http://www.cnblogs.com/qinpengming/archive/2012/12/03/2800002.html) var myDate = new Date(); myD ...

  10. PHP Math 函数

    abs() 绝对值. 3 acos() 反余弦. 3 acosh() 反双曲余弦. 4 asin() 反正弦. 3 asinh() 反双曲正弦. 4 atan() 反正切. 3 atan2() 两个参 ...