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. scratch少儿编程第一季——04、想要做到有的放矢,瞄准方向很重要

    各位小伙伴大家好: 上期我们学习了动作模块的前面三个指令,今天我们继续学习下面的5个指令. 首先来看第一个(控制方向): 面向90方向默认就是屏幕的右边. 点击白色文本框上面的▼可以打开下拉菜单. 大 ...

  2. spring cloud 停止服务

    shutdown的默认url为host:port/shutdown,当需要停止服务时,向服务器post该请求即可,如:curl -X POST host:port/shutdown将得到形如{&quo ...

  3. Warning: popen() has been disabled for security reasons in OS/Guess.php on line 241

    今天使用pecl install swoole命令编译安装swoole的时候提示:Warning: popen() has been disabled for security reasons in ...

  4. 在.netcore webapi项目中使用后台任务工具Hangfire

    安装Hangfire 在webapi项目中通过nuget安装Hangfire.Core,Hangfire.SqlServer,Hangfire.AspNetCore,截止到目前的最新版本是1.7.6. ...

  5. .netcore 上传

    BS 上传文件,就是 <input type="file" name="file" />  这个选择文件之后,浏览器保存了文件路径,上传的时候,把这 ...

  6. 如何把Windows主机中的文件拉到centOS虚拟机中

    如何把Windows主机中的文件拉到centOS虚拟机中 2017年02月19日 22:19:12 Ariel_lin2017 阅读数:6023 标签: vmware tools共享文件   之前写了 ...

  7. Seaborn(二)之数据集分布可视化

    Seaborn(二)之数据集分布可视化 当处理一个数据集的时候,我们经常会想要先看看特征变量是如何分布的.这会让我们对数据特征有个很好的初始认识,同时也会影响后续数据分析以及特征工程的方法.本篇将会介 ...

  8. Uploadify 之使用

    uploadify 3.2.1是 jQuery提供的一个上传插件,其参数详解见 http://www.cnblogs.com/yangy608/p/3915349.html 这里列举一个实际应用的例子 ...

  9. 常用的bug管理工具

    1. QC(Quality Center)是原Mercury Interactive公司(现已被HP收购)生产的企业级基于WEB測试管理工具,须要安装配置IIS和数据库.系统资源消耗比較 大:功能非常 ...

  10. SQL中新建注释、查询注释和说明

    1.查询注释 SELECT A.name AS table_name, B.name AS column_name, C.value AS column_description FROM sys.ta ...