SCU 4438 Censor (2015四川省省赛C题)

题意很简单,感觉这道题是陈题额。。
就是给两个字符串,A和B,把B中包含A的字符串都递归删除。
很明显的KMP了。。这题比赛的时候我推了很久。。因为字符串是弱项没有专门刷过。。。
代码:

//author: CHC
//First Edit Time:  2015-06-23 11:56
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <algorithm>
#include <limits>
using namespace std;
typedef long long LL;
const int MAXN=1000000*5 + 1000;
const int INF = numeric_limits<int>::max();
const LL LL_INF= numeric_limits<LL>::max();
char strs[MAXN],tmp[MAXN],ans[MAXN];
int pre[MAXN],next[MAXN];
void InitP(char *B){
    next[0]=next[1]=0;
    for(int i=2,j=0;B[i];++i){
        while(j>0&&B[j]!=B[i-1])j=next[j];
        if(B[j]==B[i-1])++j;
        next[i]=j;
    }
}
int main()
{
    while(~scanf(" %s %s",tmp,strs)){
        InitP(tmp);
        int len=strlen(strs);
        int len1=strlen(tmp);
        for(int i=0,j=0,k=0;i<len;i++,k++){
            ans[k]=strs[i];
            while(j>0&&strs[i]!=tmp[j])j=next[j];
            if(strs[i]==tmp[j])++j;
            if(j==len1){
                k-=len1;
                j=pre[k];
            }
            ans[k+1]=0;
            pre[k]=j;
        }
        puts(ans);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值