8288分类目录 8288分类目录 8288分类目录
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

BZOJ 3942: [Usaco2015 Feb]Censoring

来源:本站原创 浏览:145次 时间:2021-10-11
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).

 

OutputThe 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

 

Solution:

  本题KMP+栈模拟。

  首先求出模式串的next数组,再在主串中去匹配,匹配的同时记录答案并用栈维护当前位的模式串指针$p$,然后若在第$i$位匹配到了模式串,则将模式串指针回到栈中维护的第$i-m$个的指针位置,继续匹配,重复此过程,最后输出答案就好了。

代码:

 

#include#define il inline#define ll long long#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)#define Bor(i,a,b) for(int (i)=(b);(i)>=(a);(i)--)using namespace std;const int N=1000005;int n,m,top,now,f[N],stk[N];char s[N],t[N],ans[N];int main(){    scanf("%s%s",t,s),n=strlen(t),m=strlen(s);    int p=0;    For(i,1,m-1){        while(p&&s[i]!=s[p]) p=f[p];        if(s[i]==s[p]) f[i+1]=(++p);        else f[i+1]=0;    }    p=0;    For(i,0,n-1){        ans[++top]=t[i];        while(p&&ans[top]!=s[p]) p=f[p];        if(s[p]==ans[top]) p++;        if(p==m) top-=m,p=stk[top];        else stk[top]=p;    }    For(i,1,top) printf("%c",ans[i]);    return 0;}

 

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net