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. Mac 如何安装Homebrew?

    到Github官网上搜索Homebrew,找到对应的Homebrew后,查看它的安装文档,链接如下: https://github.com/Homebrew/homebrew/blob/master/ ...

  2. PHP Socket实现websocket(一)基本函数介绍

    WebSocket protocol 是HTML5一种新的协议.它实现了浏览器与服务器全双工通信(full-duplex). 一开始的握手需要借助HTTP请求完成. WebSocket是基于TCP来实 ...

  3. windows和linux文件共享

    ###Samba安装     [root@samba ~]# yum install -y samba*     [root@samba ~]# rpm -qa | grep samba ###开启s ...

  4. UICollectionView + AFNetWorking 异步加载,局部刷新.

    最近在做的项目需要cell里面的数据需要和后台实时交互.而collectionview reload会整体刷新. //m层 发送通知 [[NSNotificationCenter defaultCen ...

  5. peewee 字段属性help_text的支持问题

    至少在__version__ = '2.6.0'的时候,给字段添加help_text的时候,在数据库的ddl语句里面是没有comment的. 看了下源码,顺藤摸瓜,最终定位到了字段(Field类)的_ ...

  6. python 模块之间的变量共享

    才疏学浅,只知道两种方式: 1. 通过__builtin__实现: builtin1.py import __builtin__ __builtin__.some_global_var_among_m ...

  7. bootstrap框架----像素

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. php返回json数组

    1.后端 //处理json数组中文问题 function arrayRecursive(&$array, $function, $apply_to_keys_also = false) { s ...

  9. mysql导入导出数据库命令

    1.导出数据库:mysqldump -u 用户名 -p 数据库名 > 导出的文件名 如我输入的命令行: mysqldump -u root -p news > /home/jason/sq ...

  10. neutron用linux_bridge部署provider网络

    网卡配置: # The loopback network interface auto lo iface lo inet loopback # The primary network interfac ...