3942: [Usaco2015 Feb]Censoring
3942: [Usaco2015 Feb]Censoring
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 964 Solved: 480
[Submit][Status][Discuss]
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
HINT
Source
KMP
建一个栈表示未被匹配的字符
每次只要看一看新加入的字符是否能接栈顶继续匹配,不行的话不断查找nex数组知道可以为止,
每次匹配长度达到c串长度时栈的深度-c串长度
#include<iostream>
#include<cstdio>
#define M 4000000
using namespace std;
int la[M],i,m,n,j,k,a[M][2],nex[M],b[M],top,t;
char d[M],c[M];
int main()
{
scanf("%s%s",d+1,c+1);
for(n=1;1;n++) if(d[n]<'a' || d[n]>'z') break;
for(m=1;1;m++) if(c[m]<'a' || c[m]>'z') break;
n-=1; c[m]='&'; m-=1; kmp(c);
for(int i=2,j=0; i<=m;i++)
{
while(j && (c[i]!=c[j+1])) j=nex[j];
if(c[j+1]==c[i]) j+=1;
nex[i]=j;
}
for(i=1;i<=n;i++)
{
t=a[top][0];
if(c[a[top][0]+1]==d[i]) t=a[top][0];
else while(t && d[i]!=c[t+1]) t=nex[t];
if(c[t+1]!=d[i]) t=-1;
top+=1;
a[top][0]=t+1; a[top][1]=i;
if(a[top][0]==m) top-=m;
}
for(i=1;i<=top;i++) printf("%c",d[a[i][1]]);
}
3942: [Usaco2015 Feb]Censoring的更多相关文章
- 3942: [Usaco2015 Feb]Censoring [KMP]
3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 375 Solved: 206[Subm ...
- Bzoj 3942: [Usaco2015 Feb]Censoring(kmp)
3942: [Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hooveske ...
- BZOJ 3942: [Usaco2015 Feb]Censoring
Description 有两个字符串,每次用一个中取出下一位,放在一个字符串中,如果当前字符串的后缀是另一个字符串就删除. Sol KMP+栈. 用一个栈来维护新加的字符串就可以了.. 一开始我非常的 ...
- [BZOJ 3942] [Usaco2015 Feb] Censoring 【KMP】
题目链接:BZOJ - 3942 题目分析 我们发现,删掉一段 T 之后,被删除的部分前面的一段可能和后面的一段连接起来出现新的 T . 所以我们删掉一段 T 之后应该接着被删除的位置之前的继续向后匹 ...
- bzoj 3942: [Usaco2015 Feb]Censoring【kmp+栈】
好久没写kmp都不会写了-- 开两个栈,s存当前串,c存匹配位置 用t串在栈s上匹配,栈每次入栈一个原串字符,用t串匹配一下,如果栈s末尾匹配了t则弹栈 #include<iostream> ...
- BZOJ 3940: [Usaco2015 Feb]Censoring
3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 367 Solved: 173[Subm ...
- 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈
[BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...
- bzoj3940: [Usaco2015 Feb]Censoring
AC自动机.为什么洛谷水题赛会出现这种题然而并不会那么题意就不说啦 .终于会写AC自动机判断是否是子串啦...用到kmp的就可以用AC自动机水过去啦 #include<cstdio> #i ...
- bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机
3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Description Farmer John has ...
随机推荐
- Yii中文乱码 解决
需要将config/main.php改成utf-8编码即可
- 14:求10000以内n的阶乘
14:求10000以内n的阶乘 查看 提交 统计 提问 总时间限制: 5000ms 内存限制: 655360kB 描述 求10000以内n的阶乘. 输入 只有一行输入,整数n(0<=n< ...
- sqlserver年月日转汉字大写--自定义函数--繁体
两个自定义函数结合 函数一: create function convertNumToChinese ()) ) as begin ) ' set @temStr = '壹' ' set @temSt ...
- Django随笔
入门 小Demo设计介绍 本示例完成“图书-英雄”信息的维护,需要存储两种数据:图书.英雄 图书表结构设计: 表名:BookInfo 图书名称:btitle 图书发布时间:bpub_date 英雄表结 ...
- OpenGL学习--05--纹理立方体--BMP文件格式详解(转载)
http://blog.csdn.net/o_sun_o/article/details/8351037 BMP文件格式详解 BMP文件格式详解(BMP file format) BMP文件格式,又称 ...
- Vue 框架-04-计算属性
Vue 框架-04-计算属性 计算属性是什么? 大家可以去看官网解释:计算属性和侦听器 今天的第一个小实例: 为啥先放折磨一个实例,之前数据绑定的就已经可以实现了,看起来那么简单,就是为了告诉大家,当 ...
- redis HyperLogLog 基数估算
HyperLogLog 可以接受多个元素的输入,返回输入元素的基数估算值基数,集合中不同元素的数量.如集合{1,2,3,1,2,3,4}的基数是4.估算,HyperLogLog算法返回的基数不是完全精 ...
- linux 文件搜索命令locate及updatedb
windows 搜索工具Everything是根据NTFS日志来搜索的,所以速度特别快 locate 类似于windows的Everything,搜索速度比较快 如果没有locate命令,可安装 yu ...
- socat 的神奇使用方式
目的是实现科* 学 * 上*网,现在记录一下流程 先在服务器上安装(比如美国,香港,台湾,马来的云主机)squid,easy_rsa, centos 下可以用yum直接安装 $ yum install ...
- 全局css,js缓存及更新版本策略
在当今web世界里,CDN对于加速页面加载速度,提高用户体验起了非常重要的作用.但是问题也带来了:作为开发人员,可能需要不定时的更新部分静态文件,比如对网页的重新设计会涉及到css文件的更新,这时怎么 ...