3942: [Usaco2015 Feb]Censoring

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+栈模拟.
先将t串自匹配.
然后将s与t串匹配.
又是fail数组的套路题orz.
*/
#include<cstring>
#include<cstdio>
#define MAXN 1000001
using namespace std;
int l1,l2,top,next[MAXN],fail[MAXN];
char s1[MAXN],s2[MAXN],ans[MAXN];
void slove()
{
int p;
for(int i=2;i<=l1;i++)
{
p=next[i-1];
while(p&&s2[i]!=s2[p+1]) p=next[p];
if(s2[i]==s2[p+1]) p++;
next[i]=p;
}
}
void kmp()
{
int p;
for(int i=1;i<=l1;i++)
{
ans[++top]=s1[i];
p=fail[top-1];
while(p&&ans[top]!=s2[p+1]) p=next[p];
if(ans[top]==s2[p+1]) p++;
fail[top]=p;
if(p==l2) top-=l2;
}
for(int i=1;i<=top;i++) printf("%c",ans[i]);
}
int main()
{
scanf("%s",s1+1);l1=strlen(s1+1);
scanf("%s",s2+1);l2=strlen(s2+1);
slove();kmp();
return 0;
}

Bzoj 3942: [Usaco2015 Feb]Censoring(kmp)的更多相关文章

  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】

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

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

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

  4. BZOJ 3942: [Usaco2015 Feb]Censoring

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

  5. 3942: [Usaco2015 Feb]Censoring

    3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MB Submit: 964 Solved: 480 [Subm ...

  6. BZOJ 3940: [Usaco2015 Feb]Censoring

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

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

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

  8. BZOJ 3940: [Usaco2015 Feb]Censoring AC自动机_栈

    Description Farmer John has purchased a subscription to Good Hooveskeeping magazine for his cows, so ...

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

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

随机推荐

  1. Educational Codeforces Round 65 (Rated for Div. 2)

    A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 ...

  2. SQL Server 输出 XML

    一.概述 SELECT 查询将结果作为行集返回.在 SQL 查询中指定 FOR XML 子句,从而将该查询的正式结果作为 XML 来检索.FOR XML 子句可以用在顶级查询和子查询中.顶级 FOR ...

  3. webAPI中“System.Web.Http.HttpConfiguration”不包含“EnableSystemDiagnosticsTracing”的定义解决办法

    webAPI中“System.Web.Http.HttpConfiguration”不包含“EnableSystemDiagnosticsTracing”的定义 今天从 运行 WebAPI 工程的代码 ...

  4. c#基础知识梳理(三)

    上期回顾 - https://www.cnblogs.com/liu-jinxin/p/10824638.html 一.方法 一个方法是把一些相关的语句组织在一起,用来执行一个任务的语句块.每一个 C ...

  5. JS中的原型对象与构造器

    在Javascript中:原型对象是属于构造函数的,不属于实例:实例只能共享原型对象中的属性和方法(当然也可以有自己的属性和方法,或者覆盖原型中同名的属性和方法):构造器constructor属于原型 ...

  6. python实现暴力破解

    import urllib2 import urllib import cookielib import threading import sys import Queue from HTMLPars ...

  7. Redis 简单使用 and 连接池(python)

    Redis 简介 NoSQL(not only sql):非关系型数据库 支持 key-value,  list,  set,  zset,  hash 等数据结构的存储:支持主从数据备份,集群:支持 ...

  8. Java中的ThreadLocal详解

    一.ThreadLocal简介 多线程访问同一个共享变量的时候容易出现并发问题,特别是多个线程对一个变量进行写入的时候,为了保证线程安全,一般使用者在访问共享变量的时候需要进行额外的同步措施才能保证线 ...

  9. gradient 渐变

    看了大漠 写的关于 Gradient 的文章,我也想写点以便加深记忆. 首先gradient 分为linear-gradient (线性渐变) 和 radial-gradient(径向渐变),渐变是作 ...

  10. 关于Vue父组件把异步获取的数据传给子组件的问题

    由于父组件中的数据是异步获取的,而子组件在一开始便会渲染,所以会造成子组件渲染完成后,数据还未获取到的情况 这里有一个简单的解决方案:在子组件渲染前,判断父组件数据是否获取完成,数据获取完成后再渲染子 ...