[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 ...
随机推荐
- CentOS-6.5安装zabbix 3.0.4
关闭selinux [root@localhost /]# sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux ...
- ProgressDialog使用总结
以前对ProgressDialog不太熟练,特地看到的这个文章 ProgressDialog的使用 ProgressDialog 继承自AlertDialog,AlertDialog继承自Dialo ...
- 剑指Offer 从上往下打印二叉树(dfs)
题目描述 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: 用一个队列来辅助,先压入根节点,设置一个指针记录队列头位置,判断队头指针有没有孩子,有压入左右孩子,,,操作完一次,队头出 ...
- PHP 中的行为 ,与什么是切面
行为(Behavior)扩展以及插件(Plug or Hook)详解: 行为(Behavior)是ThinkPHP扩展机制中比较关键的一项扩展,行为即可以独立调用,也可以绑定到某个 标签中进行监听,官 ...
- Ngrok搭建服务器
一.ngrok简介及作用 ngrok 是一款用go语言开发的开源软件,它是一个反向代理,通过在公共的端点和本地运行的 Web 服务器之间建立一个安全的通道.下图简述了ngrok的原理. ngrok 可 ...
- GR32 TImage32的图层绘制原理
转载:http://blog.sina.com.cn/s/blog_491aced20100ded4.html TImage32的继承顺序如下:TCustomControl->TCustomPa ...
- 7.2---蚂蚁相遇问题(CC150)
public class Ants { public double antsCollision(int n) { // write code here return (1 - Math.pow(0.5 ...
- iframe框架中用js改变父级Url
<script type="text/javascript"> var path = window.location.href;//当前也面的跳转 ...
- Bootstrap之表格checkbox复选框全选
效果图: HTML中无需添加额外的一列来表示复选框,而是由JS完成,所以正常的表格布局就行了: <table class="table table-bordered table-hov ...
- python程序打包成.exe----pyinstaller工具
1. 环境 windows 2. 安装 准备文件:PyWin32 or pypiwin32 运行如下安装命令: pip install pyinstaller==3.0 不要使用3.2版本,编译完成 ...