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. appid 评价

    //apple api #define kAppAppleId         @"980883989" #define kAppRateUrl         @"it ...

  2. objective-c-sort-keys-of-nsdictionary-based-on-dictionary-entries

    NSArray *keys = [someDictionary allKeys]; NSArray *sortedKeys = [keys sortedArrayUsingComparator:^NS ...

  3. ecshop之transport和jquery冲突之完美解决方案

    众所周知:ecshop的transport.js文件和Jquery是冲突的,两个文件不能同时调用,现给出以下完美解决方案:原因分析:在transport.js文件中,大概 580行到590行之间,这个 ...

  4. android 在线升级借助开源中国App源码

    android 在线升级借助开源中国App源码 http://www.cnblogs.com/luomingui/p/3949429.html android 在线升级借助开源中国App源码分析如下: ...

  5. CF570D——Tree Requests

    1.题目大意:给你一棵树,每个点有一个字符,然后我们定义1的深度是1,然后1的儿子深度是2... 然后有一个询问,询问以i为根节点的子树,然后深度是k的那层的所有字符,可否组成一个回文串 2.分析:首 ...

  6. jquery 平滑锚

    setTimeout('$("html,body").animate({ scrollTop: $(".title").offset().top }, 1000 ...

  7. Android学习笔记(十三)——广播机制

     //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! Android 中的每个应用程序都可以对自己感兴趣的广播进行注册,这样该程序就只会接收到自己所关心的广播内容 ...

  8. phpstorm的调试工具xdebug

    1.需求 知道xdebug的使用方法 2.安装xdebug http://www.awaimai.com/1290.html 3.配置phpstorm http://www.awaimai.com/1 ...

  9. python 列表推导式

    squares = [x**2 for x in range(10)] 相当于squares = map(lambda x: x**2, range(10)),但是更简洁和易读.傻逼才会用最古老的fo ...

  10. RouterOS首次打开网页强制跳转

    网上极少有关于RouterOS的第一次打开网页强制跳转主页的方法,大多数都方法是将浏览某个域名的IP地址跳转到自己的主页,这种方法有时会失效.还有一种方法就是当用户用80端口连接时,抓取源地址到地址列 ...