【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. JAVA基础知识|堆和栈

    一.java六个存储数据的地方 1)寄存器(register):这是最快的存储区,因为它位于不同于其他存储区的地方——处理器内部.但是寄存器的数量极其有限,所以寄存器由编译器根据需求进行分配.你不能直 ...

  2. vmware压缩vmdk文件大小

    在搭建靶机环境的过程中总是遇见vmdk越来越大,导致上传时间变长.记一下压缩vmdk的方法 ;sync;rm -f zero.fill /usr/bin/vmware-toolbox-cmd disk ...

  3. PHP多进程开发与Redis结合实践

    原文:https://blog.51cto.com/laok8/2107892?source=drh 业务逻辑介绍: 用户在 APP 上发帖子,然后存储到 Redis 的 List 列表中 利用 Li ...

  4. SQL-W3School-基础:SQL INSERT INTO 语句

    ylbtech-SQL-W3School-基础:SQL INSERT INTO 语句 1.返回顶部 1. INSERT INTO 语句 INSERT INTO 语句用于向表格中插入新的行. 语法 IN ...

  5. vue 组件之间通信

    父传子 **父组件代码** <template> <header-box :title-txt="showTitleTxt"></header-box ...

  6. CentOS下yum安装jdk

    jdk安装极其简单,因为java应用太广泛.先看下系统中是否已安装,已安装了先卸载,没安装则直接通过yum一步到位安装即可. 1.看本机是否已有jdk # java -version -bash: j ...

  7. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_24-页面预览-页面预览测试

    通过nginx转发到预览的地址 重启nginx 添加页面预览按钮 调整下列表的列的宽度

  8. Tomcat下配置javaWeb访问路径-Linux

    首先找到你的Tomcat的server.xml文件 笔者直接编辑 sudo vi /app/prod/conf/server.xml 访问只需要您的IP地址就可以访问项目了 第二个配置则是访问 ip+ ...

  9. EasyUI之toolTip

    <a class="easyui-tooltip" title="提示框" href="http://www.baidu.com"&g ...

  10. 关于比较js中两个对象相等 ==

    “如果两个操作数都是对象,则比较他们是不是同一个对象(引用的对象在内存中的地址一样),如果两个操作数都指向同一个对象,则相等操作符返回true,否则,返回false”. 我做了一个例子 functio ...