SCU 4438:Censor
2017-08-21 11:44
393 查看
Censor
frogisnowaeditortocensorso-calledsensitivewords(敏感词).Shehasalongtextp
.Herjobisrelativelysimple--justtofindthefirstoccurenceofsensitivewordw
andremoveit.
frogrepeatsoverandoveragain.Helpherdothetediouswork.
Input
Theinputconsistsofmultipletests.Foreachtest:Thefirstlinecontains1
stringw.Thesecondlinecontains1stringp
.
(1≤lengthofw,p≤5⋅106
,w,p
consistsofonlylowercaseletter)
Output
Foreachtest,write1stringwhichdenotesthecensoredtext.
SampleInput
abc aaabcbc b bbb abc ab
SampleOutput
a ab 题意:给你一个主串,递归删除模式串。 比如:T:abcS:aaabcbc aaabcbc->aabc->a 非常巧妙的KMP,我们用一个栈记录当前的字符以及其在模式串匹配的位置,当位置等于模式串长度之后,将模式串长度的串出栈,从栈顶元素开始继续匹配主串.时间复杂度O(n).
#include<stdio.h> #include<iostream> #include<string.h> #include<stack> #include<algorithm> usingnamespacestd; constintN=5000005; structNode{ charc; intk; }; charw ,t ,ans ; intNext ; voidget_next(char*p){ intlen=strlen(p); inti=0,k=-1; Next[0]=-1; while(i<len){ if(k==-1||p[i]==p[k]){ i++,k++; Next[i]=k; } elsek=Next[k]; } } voidKmp(char*s,char*p){ intlen1=strlen(s),len2=strlen(p); inti=0,j=0,len; stack<Node>stk; while(!stk.empty())stk.pop(); while(i<len1){ if(j==-1||s[i]==p[j]){ i++,j++; stk.push(Node{s[i-1],j}); }else{ j=Next[j]; } if(j==len2){ len=len2; while(!stk.empty()&&len--)stk.pop(); if(stk.empty())j=0; elsej=stk.top().k; } } intk=0; while(!stk.empty()){ ans[k++]=stk.top().c; stk.pop(); } for(inti=k-1;i>=0;i--)printf("%c",ans[i]); printf("\n"); } intmain(){ while(scanf("%s%s",w,t)!=EOF){ get_next(w); Kmp(t,w); } return0; }
相关文章推荐
- SCU 4438 Censor
- scu 4438 censor(KMP,栈保存匹配状态)
- SCU - 4438 Censor 【字符串好题】
- SCU 4438 Censor KMP/Hash
- SCU-4438 Censor(字符串HASH)
- 2015四川省赛 scu oj 4438 Censor
- 未解决 训练赛3 ,kmp || 字符串哈希(没学)Censor SCU - 4438
- SCU 4438 Censor(KMP / HASH)
- scu 4438 Censor
- SCU 4438 Censor (KMP)
- scu 4438 Censor 字符串哈希+前缀和
- SCU 4438 Censor (2015四川省省赛C题)
- SCU 4438 Censor (字符串哈希)
- SCU-4438 Censor(KMP算法)
- SCU 4438
- SCU - 4438(KMP)
- SCU 4438
- SCU 4438
- SCU 4438
- SCU 4438