【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
*/

最新文章

  1. navicat注册码
  2. 改用C++生成自动化数据表
  3. linux下JsonServer启动
  4. Qt::QObject类
  5. 解决 java 使用ssl过程中出现&quot;PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target&quot;
  6. 【卡西欧Fx5800-p程序】01 坐标转换程序-带注释
  7. onmouseenter与onmouseover
  8. 【温故知新】C#委托delegate
  9. 查看80端口被占用的方法(IIS、apmserv、system)
  10. Linux操作杂记
  11. PHP+ MongoDB
  12. Xcode7设置生成DSYM出现大量警告
  13. 一旦rhel5.8造成只读文件系统ORA-00354: corrupt redo log block header
  14. PhpSrom安装xdebug
  15. Ubuntu 16.04 安装wine QQ
  16. Centos6.9minimal安装图形化界面
  17. 蚂蚁 RPC 框架 SOFA-RPC 初体验
  18. 关于Python的协程问题总结
  19. Snowflake Snow Snowflakes POJ - 3349(hash)
  20. 使用call、apply和bind解决js中烦人的this,事件绑定时的this和传参问题

热门文章

  1. ajax 的 get 方式
  2. spring+mybatis事务的readonly属性无效
  3. spaCy 第二篇:语言模型
  4. ubantu 安装boost库 c++connector
  5. 指定版本下载yum离线安装包
  6. 移动端—— 兼容PC端,移动端的点击事件
  7. kotlin 泛型中类型投射
  8. 013-java中的IO操作-InputStream/Reader、OutputStream/Writer
  9. NIO单一长连接——dubbo通信模型实现
  10. mysql连接工具记录