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 ...
随机推荐
- iOS 交互h5 - WKWebView
众所周知,UIWebView存在内存问题,也就是当加载一个UIWebView时,内存会一直上升趋势无法得到释放.这样在使用UIWebView进行h5交互开发时会有很大的问题. 因而苹果增加了一个新的类 ...
- spring jpa和mybatis整合
spring jpa和mybatis整合 前一阵子接手了一个使用SpringBoot 和spring-data-jpa开发的项目 后期新加入一个小伙伴,表示jpa相比mybatis太难用,多表联合的查 ...
- 第二十四天- 模块导入 import from xxx import xxx
# 模块:# 模块就是⼀个包含了python定义和声明的⽂件,⽂件名就是模块的名字加上.py后缀# 换句话说我们⽬前写的所有的py⽂件都可以看成是⼀个模块# 为何用模块:写大项目时,把相关的功能进⾏分 ...
- [清华集训]Rmq Problem / mex
题目链接 我们离线处理这些询问 在右端点所在的位置用vector来push_back询问 维护每个数值最后出现的位置p[x] 从左往右扫,边走边回答询问 对于每个询问我们回答第一个p[x]<l的 ...
- pom.xml配置文件内容记录
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- cocos2d-x学习网站
非常好的学习cocos2d-x网站 强烈推荐!---游戏蛮牛
- java获取本月第一天和最后一天
public class CalendarTest { public static void main(String[] args) { // 获取当前年份.月份.日期 Calendar cale = ...
- 少个人保护?我来!——阿里云在ICANN第3届GDD峰会纪实
西班牙马德里以足球和斗牛闻名于世,2017年5月9日至11日,ICANN第三届全球域名部门行业峰会(GDD)在这里召开.阿里云作为亚洲域名保有量最高的注册商,代表成千上万客户的利益与权力,派出代表,前 ...
- BlockingQueue介绍及使用
1.BlockingQueue队列和平常队列一样都可以用来作为存储数据的容器,但有时候在线程当中 涉及到数据存储的时候就会出现问题,而BlockingQueue是空的话,如果一个线程要从Blockin ...
- JQuery-学习。
jQuery是一套跨浏览器的JavaScript库,简化HTML与JavaScript之间的操作.由约翰·雷西格(John Resig)在2006年1月的BarCamp NYC上发布第一个版本.目前是 ...