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

Silver


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的更多相关文章

  1. 3942: [Usaco2015 Feb]Censoring [KMP]

    3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 375  Solved: 206[Subm ...

  2. Bzoj 3942: [Usaco2015 Feb]Censoring(kmp)

    3942: [Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hooveske ...

  3. BZOJ 3942: [Usaco2015 Feb]Censoring

    Description 有两个字符串,每次用一个中取出下一位,放在一个字符串中,如果当前字符串的后缀是另一个字符串就删除. Sol KMP+栈. 用一个栈来维护新加的字符串就可以了.. 一开始我非常的 ...

  4. [BZOJ 3942] [Usaco2015 Feb] Censoring 【KMP】

    题目链接:BZOJ - 3942 题目分析 我们发现,删掉一段 T 之后,被删除的部分前面的一段可能和后面的一段连接起来出现新的 T . 所以我们删掉一段 T 之后应该接着被删除的位置之前的继续向后匹 ...

  5. bzoj 3942: [Usaco2015 Feb]Censoring【kmp+栈】

    好久没写kmp都不会写了-- 开两个栈,s存当前串,c存匹配位置 用t串在栈s上匹配,栈每次入栈一个原串字符,用t串匹配一下,如果栈s末尾匹配了t则弹栈 #include<iostream> ...

  6. BZOJ 3940: [Usaco2015 Feb]Censoring

    3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 367  Solved: 173[Subm ...

  7. 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈

    [BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...

  8. bzoj3940: [Usaco2015 Feb]Censoring

    AC自动机.为什么洛谷水题赛会出现这种题然而并不会那么题意就不说啦 .终于会写AC自动机判断是否是子串啦...用到kmp的就可以用AC自动机水过去啦 #include<cstdio> #i ...

  9. bzoj 3940: [Usaco2015 Feb]Censoring -- AC自动机

    3940: [Usaco2015 Feb]Censoring Time Limit: 10 Sec  Memory Limit: 128 MB Description Farmer John has ...

随机推荐

  1. SQLite的Integer类型

    SQLite 中的 INTEGER:带符号的整型,具体取决有存入数字的范围大小,根据大小可以使用1,2,3,4,6,8字节来存储. 在SQLite中,存储分类和数据类型也有一定的差别,如INTEGER ...

  2. Spring 、SpringMVC 、Struts2之间的区别

    一.Spring与SpringMVC的区别: spring是一个开源框架,是为了解决企业应用程序开发,功能如下: 功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能 范围:任何Ja ...

  3. 0<Double.MIN_VALUE

    好吧, 吐嘈一下: 前几天写代码时发现 Double 有几个静态成员变量, 如 MAX_VALUE , MIN_VALUE 等, 当时就自己"故名思意"了, 分别当成了 doubl ...

  4. 【java错误】错误: 编码GBK的不可映射字符

    java源代码 今天在写java是出现一个编码错误,这里先将书上的java源代码贴出来. import java.io.Console; public class ConsoleTest { //用j ...

  5. CSS中表示大小的单位

    以下是DIVCSS5为大家总结网页中常见html单位介绍,在css+div布局中长度单位介绍篇. 其实css中的长度单位一共有8个,分别是px,em,pt,ex,pc,in,mm,cm: px:像素( ...

  6. Android属性动画简单剖析

    运行效果图: 先看布局文件吧,activity_main.xml: <?xml version="1.0" encoding="utf-8"?> & ...

  7. 【Android】Retrofit 2.0 的使用

    一.概述 Retrofit是Square公司开发的一个类型安全的Java和Android 的REST客户端库.来自官网的介绍: A type-safe HTTP client for Android ...

  8. 多张报表导出到一个多sheet页excel

     业务需求: 通过勾选不同的报表名称,然后直接执行导出excel.并且这些报表需要统一导入到一个excel的多个sheet页中,并且对某些报表可能需要增加一些类似'已审核'之类的图片(展现时并没有 ...

  9. RollViewPager图片轮播效果开源框架的使用

    RollViewPager是一个自动轮播的Viewpager, 支持无限循环. 触摸时会暂停播放,直到结束触摸一个延迟周期以后继续播放. 看起来就像这样.指示器可以为点可以为数字还可以自定义,位置也可 ...

  10. Linux安装Tomcat服务器发布项目教程

    前面小Alan跟大家聊了在Linux服务器上jdk运行环境的安装以及redis非关系型数据库的安装,今天继续跟大家聊聊Tomcat的安装,以及将我们的项目发布上去并成功的访问. 第一步:将tomcat ...