【KMP】Censoring

题目描述

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.

输入

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). 

输出

The string S after all deletions are complete. It is guaranteed that S will not become empty during the deletion process. 

样例输入

whatthemomooofun
moo

样例输出

whatthefun

参考博客:

Censoring(栈+KMP)


【题解】:

  正常匹配,不过是加了一个栈来维护。

【代码】:

 #include<cstdio>
#include<cstring>
using namespace std;
const int N = 1e6 + ;
char s[N],p[N],ans[N];
int n,m,top,Next[N],f[N]; void get_Next(){
for(int i=,j= ; i<=m ; i++ ){
while ( j && p[i] != p[j+] ) j = Next[j] ;
if( p[i] == p[j+] ) j++;
Next[i] = j ;
}
}
void Kmp(){ //if( !(m == 1 && s[1] == p[1]) )
//ans[++top] = s[1];
for(int i=,j= ; i<=n ; i++){
ans[++top] = s[i] ;
while ( j && ( j==m || s[i] != p[j+]) ) j = Next[j] ;
if ( s[i] == p[j+] ) j++ ; f[top] = j ;
if( j == m ){
top = top - m ;
j = f[top];
}
}
ans[++top] = '\0';
printf("%s\n",ans+);
}
int main()
{
scanf("%s%s",s+,p+);
n = strlen ( s+ ) ;
m = strlen ( p+ ) ; get_Next();
Kmp();
return ;
}
/*
whatthemomooofun
moo
*/

【KMP】Censoring的更多相关文章

  1. 【KMP】【最小表示法】NCPC 2014 H clock pictures

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1794 题目大意: 两个无刻度的钟面,每个上面有N根针(N<=200000),每个 ...

  2. 【动态规划】【KMP】HDU 5763 Another Meaning

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...

  3. HDOJ 2203 亲和串 【KMP】

    HDOJ 2203 亲和串 [KMP] Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  4. 【KMP】OKR-Periods of Words

    [KMP]OKR-Periods of Words 题目描述 串是有限个小写字符的序列,特别的,一个空序列也可以是一个串.一个串P是串A的前缀,当且仅当存在串B,使得A=PB.如果P≠A并且P不是一个 ...

  5. 【KMP】Radio Transmission

    问题 L: [KMP]Radio Transmission 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的.但是这个字符串是不确定的,现在只想知道它的最短长度是多少. 输入 第一行给出字 ...

  6. 【kmp】似乎在梦中见过的样子

    参考博客: BZOJ 3620: 似乎在梦中见过的样子 [KMP]似乎在梦中见过的样子 题目描述 「Madoka,不要相信QB!」伴随着Homura的失望地喊叫,Madoka与QB签订了契约. 这是M ...

  7. 【KMP】BZOJ3942-[Usaco2015 Feb] Censoring

    [题目大意] 有一个S串和一个T串,长度均小于1,000,000,设当前串为U串,然后从前往后枚举S串一个字符一个字符往U串里添加,若U串后缀为T,则去掉这个后缀继续流程.输出最后的S串. [思路]三 ...

  8. 【POJ2752】【KMP】Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  9. 【POJ2406】【KMP】Power Strings

    Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...

随机推荐

  1. Js中Array常用方法小结

    说起Array的方法,不免让人皱一下眉头,下面我们从增删改查角度依次来总结. 1.增 push: 将传入的参数 ,插入数组的尾部,并返回新数组的长度.不管传入参数为一个值还是一个数组,都作为插入数组的 ...

  2. Flask 生成下载文件

    1 后台程序直接生成文件内容 from flask import make_response @app.route('/testdownload', methods=['GET']) def test ...

  3. 通过Nginx部署flask项目

    用Flask开发之后,很多人,喜欢用nohup python manage.py & 这样的形式,放到后台运行,其实这样只是个发开模式,很简陋,无法支持并发,进程监控等功能.所以采用nginx ...

  4. Java并发指南6:Java内存模型JMM总结

    本文转载自互联网,侵删   在前面的文章中我们介绍了Java并发基础和线程安全的概念,以及JMM内存模型的介绍,包括其定义的各种规则.同时我们也介绍了volatile在JMM中的实现原理,以及Lock ...

  5. 【Phoenix】2、初始化 Phoenix 项目后的 目录结构

    1.当我们创建的时候,Phoenix 为我们建立的根目录架构,如下: ├── _build ├── assets // 这个是放一下静态文件的,不如 js.css img 和 node_module ...

  6. python笔记5 接口类抽象类 封装 反射 设计模式 模块 :random随机数 josn shelve持久化存储

    接口类抽象类 接口类:接口类就是制定一个规则,让其他人按照我的规则去写程序. #!/usr/bin/env python from abc import ABCMeta,abstractmethod ...

  7. ERS:百科

    ylbtech-ERS:百科 ERS=(Enterprises Run System)企业运行系统 企业运行系统-ERS,是继ERP系统之后的企业管理控制软件系统的终极形式,有着非常美好的市场前景,尤 ...

  8. start开启服务的生命周期

    * 完整生命周期:onCreate()-->onStartCommand()-->onDestroy() * 开启服务:onCreate()-->onStartCommand() * ...

  9. c++自定义数组越异常 ArrayIndexOutOfBoundsException (学习)

    #include <iostream> using namespace std; const int DefaultSize = 10; class Array{public: Array ...

  10. centos安装tidy扩展

    wget http://pecl.php.net/get/tidy-1.2.tgztar -xvzf tidy-1.2.tgzcd tidy-1.2/usr/local/php/bin/phpize. ...