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处理出fail数组,然后大力匹配即可。
但不知道为什么我用hash写了一遍会WA,以后有空再看看。
*/
//WA代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#define N 1000010
#define P 31
#define lon long long
using namespace std;
int n1,n2;lon p=,S,hash[N],base[N];
char s1[N],s2[N],s3[N];
int main(){
scanf("%s%s",s1+,s2+);
n1=strlen(s1+);n2=strlen(s2+);
for(int i=;i<=n2;i++){
S=S*P+s2[i]-'a';
p*=P;
}
int len=;
for(int i=;i<=n1;i++){
s3[++len]=s1[i];
hash[len]=hash[len-]*P+s1[i]-'a';
if(len<n2)continue;
if(hash[len]-hash[len-n2]*p==S)len-=n2;
}
for(int i=;i<=len;i++)printf("%c",s3[i]);
return ;
}
//AC代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#define N 1000010
using namespace std;
int fail[N],next[N],n1,n2,top;
char s1[N],s2[N],s3[N];
int main(){
scanf("%s%s",s1+,s2+);
n1=strlen(s1+);n2=strlen(s2+);
fail[]=;
for(int i=;i<=n2;i++){
int p=fail[i-];
while(p&&s2[p+]!=s2[i])p=fail[p];
if(s2[p+]==s2[i])p++;
fail[i]=p;
}
next[]=;
for(int i=;i<=n1;i++){
s3[++top]=s1[i];
int p=next[top-];
while(p&&s1[i]!=s2[p+])p=fail[p];
if(s1[i]==s2[p+])p++;
next[top]=p;
if(p==n2)top-=n2;
}
for(int i=;i<=top;i++)printf("%c",s3[i]);
return ;
}

[Usaco2015 Feb]Censoring(bzoj 3942)的更多相关文章

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

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

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

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

  3. BZOJ 3940--[Usaco2015 Feb]Censoring(AC自动机)

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

  4. Censoring(bzoj 3940)

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

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

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

  6. BZOJ 3940: [Usaco2015 Feb]Censoring

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

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

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

  8. 3942: [Usaco2015 Feb]Censoring

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

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

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

随机推荐

  1. 【PHP面向对象(OOP)编程入门教程】13.访问类型(public,protected,private)

    类型的访问修饰符允许开发人员对类成员的访问进行限制,这是PHP5的新特性,但却是OOP语言的一个好的特性.而且大多数OOP语言都已支持此特性.PHP5支持如下3种访问修饰符: public (公有的. ...

  2. Java连接池详解

    于共享资源,有一个很著名的设计模式:资源池(Resource Pool).该模式正是为了解决资源的频繁分配﹑释放所造成的问题.为解决我们的问题,可以采用数据库连接池技术.数据库连接池的基本思想就是为数 ...

  3. Ajax前台调用后台方法、AJAX Pro2(回调函数)

    //获取分店 function cityResult() { if (cityName != "") { $("#ddlcity_").find("o ...

  4. EF接触03

    emdx文件解读:

  5. expdp / impdp 用法详解

    一  关于expdp和impdp     使用EXPDP和IMPDP时应该注意的事项:EXP和IMP是客户端工具程序,它们既可以在客户端使用,也可以在服务端使用.EXPDP和IMPDP是服务端的工具程 ...

  6. IoC模式

    1.依赖 依赖就是有联系,有地方使用到它就是有依赖它,一个系统不可能完全避免依赖.如果你的一个类或者模块在项目中没有用到它,恭喜你,可以从项目中剔除它或者排除它了,因为没有一个地方会依赖它.下面看一个 ...

  7. UNITY3D在线更新之道-CSlight 使用总结

    转自:http://blog.csdn.net/leonwei/article/details/39233775 最近做U3D的热更新,研究了各种方式无果后,最容易最先想到的方式就是利用c#的反射机制 ...

  8. Android学习笔记(二十)——自定义内容提供器

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 如果我们想要实现跨程序共享数据的功能,官方推荐的方式就是使用内容提供器,可以通过新建一个类去继承 Conten ...

  9. cocos2d智能指针 转自:http://blog.csdn.net/nxshow/article/details/44699409

    智能指针在C++11的标准中已经存在了, 分别是unique_ptr,shared_ptr,weak_ptr, 其中最常用的应该是share_ptr, 它采用引用计数的方式管理内存, 当引用计数为0的 ...

  10. 如何用C语言编写病毒‘

    怎样用C语言编写病毒在分析病毒机理的基础上,用C语言写了一个小病毒作为实例,用TURBOC2.0实现.[Abstract] This paper introduce the charateristic ...