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