弱校联萌十一大决战之强力热身 C.Censor (KMP变形 好题)

129 篇文章 0 订阅
70 篇文章 0 订阅

C. Censor

Time Limit: 2000ms
Memory Limit: 65536KB

frog is now a editor to censor so-called sensitive words (敏感词).
She has a long text p . Her job is relatively simple -- just to find the first occurence of sensitive word w and remove it.
frog repeats over and over again. Help her do the tedious work.

Input

The input consists of multiple tests. For each test:
The first line contains 1 string w . The second line contains 1 string p .
( 1length of w,p5106 , w,p consists of only lowercase letter)

Output

For each test, write 1 string which denotes the censored text.

Sample Input

abc
aaabcbc
b
bbb
abc
ab

Sample Output

a

ab


题目链接:http://www.bnuoj.com/v3/contest_show.php?cid=6865#problem/C


题目大意:一个子串,一个母串,要求删除母串中的子串,删完剩余部分合并继续删,直到母串中不存在子串为止,输出此时的母串

题目分析:不要求合并的话直接kmp即可,现在删完要合并,其实只要记录每次删完,指向子串的指针回溯到哪个位置即可

#include <cstdio>
#include <cstring>
int const MAX = 5e6 + 5;
char s1[MAX], s2[MAX], ans[MAX];
int next[MAX], pos[MAX];
int l1, l2, cnt;

void get_next()
{
    int i = 0, j = -1;
    next[0] = -1;
    while(s2[i] != '\0')
    {
        if(j == -1 || s2[i] == s2[j])
        {
            i ++;
            j ++;
            if(s2[i] == s2[j])
                next[i] = next[j];
            else
                next[i] = j;
        }
        else
            j = next[j];
    }
}

void KMP()
{
    get_next();
    int i = 0, j = 0;
    cnt = 0;
    while(s1[i] != '\0')
    {
        ans[cnt] = s1[i ++];
        while(!(j == -1 || ans[cnt] == s2[j]))
            j = next[j];
        j ++;
        cnt ++;
        pos[cnt] = j;
        if(j == l2)
        {
            cnt -= l2;
            j = pos[cnt];
        }
    }
}

int main()
{
    while(scanf("%s %s", s2, s1) != EOF)
    {
        l1 = strlen(s1);
        l2 = strlen(s2);
        KMP();
        for(int i = 0; i < cnt; i++)
            printf("%c", ans[i]);
        printf("\n");
    }
}    


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值