bzoj3942[Usaco2015 Feb]Censoring*

bzoj3942[Usaco2015 Feb]Censoring

题意:

有一个S串和一个T串,不断地在S串里匹配T串,然后将其删除。S串、T串长度≤1000000。

题解:

用1、2两个栈,每次将S串的当前字符压入1栈,当前匹配到T串的位置压入2栈,如果匹配出一个T串,则让1、2栈中匹配T串的子串出栈,然后令当前匹配到T串的位置变为2栈顶的数,匹配过程可以用KMP加速。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #define maxn 1000010
 5 #define inc(i,j,k) for(int i=j;i<=k;i++)
 6 using namespace std;
 7 
 8 char s[maxn],t[maxn],st1[maxn]; int lens,lent,st2[maxn],top,fail[maxn];
 9 void getfail(){
10     int j=0;
11     inc(i,2,lent){
12         while(j&&t[i]!=t[j+1])j=fail[j];
13         if(t[i]==t[j+1])j++; fail[i]=j;
14     }
15 }
16 int main(){
17     char ch=getchar(); while(ch<'a'||ch>'z')ch=getchar();
18     lens=0; while(ch>='a'&&ch<='z')s[++lens]=ch,ch=getchar();
19     ch=getchar(); while(ch<'a'||ch>'z')ch=getchar();
20     lent=0; while(ch>='a'&&ch<='z')t[++lent]=ch,ch=getchar();
21     getfail(); int j=0,top=0;
22     inc(i,1,lens){
23         while(j&&s[i]!=t[j+1])j=fail[j]; if(s[i]==t[j+1])j++;
24         st1[++top]=s[i],st2[top]=j;
25         if(j==lent){while(j)top--,j--; j=st2[top];}
26     }
27     inc(i,1,top)printf("%c",st1[i]); return 0;
28 }

 

20160825

转载于:https://www.cnblogs.com/YuanZiming/p/5811572.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值