[Usaco2015 Feb]Censoring(bzoj 3942)
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
Output
The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process.
Sample Input
moo
Sample Output
/*
用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)的更多相关文章
- [BZOJ3940]:[Usaco2015 Feb]Censoring(AC自动机)
题目传送门 题目描述: FJ把杂志上所有的文章摘抄了下来并把它变成了一个长度不超过105的字符串S.他有一个包含n个单词的列表,列表里的n个单词记为t1…tN.他希望从S中删除这些单词.FJ每次在S中 ...
- 【BZOJ3940】[USACO2015 Feb] Censoring (AC自动机的小应用)
点此看题面 大致题意: 给你一个文本串和\(N\)个模式串,要你将每一个模式串从文本串中删去.(此题是[BZOJ3942][Usaco2015 Feb]Censoring的升级版) \(AC\)自动机 ...
- BZOJ 3940--[Usaco2015 Feb]Censoring(AC自动机)
3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 723 Solved: 360[Subm ...
- Censoring(bzoj 3940)
Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so ...
- Bzoj 3942: [Usaco2015 Feb]Censoring(kmp)
3942: [Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hooveske ...
- BZOJ 3940: [Usaco2015 Feb]Censoring
3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 367 Solved: 173[Subm ...
- 3942: [Usaco2015 Feb]Censoring [KMP]
3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 375 Solved: 206[Subm ...
- 3942: [Usaco2015 Feb]Censoring
3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Submit: 964 Solved: 480 [Subm ...
- bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机
3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Description Farmer John has ...
随机推荐
- Spring的问题解决记录
问题:在MyEclipse中项目工程重命名以后,例如 原来叫HttpService,改为GameApp,但是项目跑起来发现,tomcat服务器依旧为GameApp目录结构,访问也只能GameApp访问 ...
- Windows 7 共享文件夹 给 VirtualBox 中的 Ubuntu 14
操作步骤如下: 1.打开虚拟机中的 Ubuntu 系统: 2.安装“增强工具” 设备 -> 安装增强工具 3.设置“共享文件夹” 控制 -> 设置 -> 添加共享文件夹 -> ...
- qt 的简介与使用
1.图形类的构造,都是通过类的构造函数以及界面初始化这两部分 2.在qtcreator的界面编辑器通过“提升类”的方法,要确定头文件的路径是否正确 3.点击窗口关闭时,销毁窗口内容时,设置属性-> ...
- Android 软键盘盖住输入框的问题
当在Android的layout设计里面如果输入框过多,则在输入弹出软键盘的时候,下面的输入框会有一部分被软件盘挡住,从而不能获取焦点输入. 解决办法: 方法一:在你的activity中的oncrea ...
- MongoDB的基本使用(二)
上一个文档说明了如何搭建一个Windows端MongoDB服务器,下面将简单介绍MongoDB的基本操作命令. 1. show dbs : 显示所有数据库 2. use bochao : 使用boch ...
- jquery版悬浮模块demo
在做在线客服时,代码就是按照该模块命名.现在,我要添加一个返回主页的功能,我觉得再复制一遍之前的代码,那个量有点多,如果我再添加一个功能,那个量会很多……现在我用创建对象字面量的方式来创建(其实我还想 ...
- 教你搭建SpringMVC框架( 更新中、附源码)
一.项目目录结构 二.SpringMVC需要使用的jar包 commons-logging-1.2.jar junit-4.10.jar log4j-api-2.0.2.jar log4j-core- ...
- PyQt4自定义事件
listview控件与updateText 相关联 self.listview.updateText.connect(self.viewlist) updateText = QtCore.pyqt ...
- SQL 执行计划(一)
缓存执行计划 SQL Server 2008提供了一些服务器对象来分析执行计划Sys.dm_exec_cached_plans: 包含缓存的执行计划,每个执行计划对应一行.Sys.dm_exe ...
- ASP.NET MVC Dropdownlist
本文介绍如何在网页里显示DropDownList. Step 1: 在Control里面添加方法 public ActionResult ShowDropDownList() { return Vie ...