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. Java中反射和Unsafe破坏单例设计模式

    有如下单例模式设计代码: class Singleton { private String info = "HELLO SHIT"; private static Singleto ...

  2. WinForm中使用CrystalReport水晶报表——基础,分组统计,自定义数据源

    开篇 本篇文章主要是帮助刚开始接触CrystalReport报表的新手提供一个循序渐进的教程.该教程主要分为三个部分1)CrystalReport的基本使用方法:2)使用CrystalReport对数 ...

  3. YII中利用urlManager将URL改写成restful风格

    这里主要涉及url显示样式 1.打开config文件夹下面的mian.php   2.修改内容   如把地址http://www.test.com/index.php?r=site/page/sid/ ...

  4. springboot 文件上传和下载

    文件的上传和下载 1.文件上传 html页面代码如下 <form method="post" action="/file/upload1" enctype ...

  5. AngularJS学习 之 安装

    1. 安装好Node.js 2. 安装好Git 3. 安装好Yeoman 以管理员身份打开cmd 输入 npm install -g yo 回车即可开始安装Yeoman,具体的安装行为最好看官网的介绍 ...

  6. Asp.net MVC 移除不用的视图引擎

    Asp.net MVC 默认提供两个视图引擎,分别为: WebFormViewEngine 和 RazorViewEngine.MVC在查找视图时,会按照指定的顺序进行查找.当我们的MVC程序未找到相 ...

  7. Spring Boot—05页面跳转

    package com.smartmap.sample.ch1.controller.view; import org.springframework.stereotype.Controller; i ...

  8. WebGIS点要素渲染性能测试

    $('#stationQuery').bind('click', function(){ var drawStyle = $.extend( { }, map.geomap( "option ...

  9. hidden field implements session

    隐藏域实现会话管理概述 如果你正在制作一个网络问卷,由于问卷内容很长,因此必须分几个页面,上一页面作答完后,必须请求服务器显示下一个页面. 但是在HTTP协议中,服务器并不会记得上一次请求的状态.既然 ...

  10. EF增删查改基类

    /// <summary> /// EF DAL CURD基类 /// </summary> /// <typeparam name="T">& ...