zoukankan      html  css  js  c++  java
  • SCU 4438 Censor|KMP变形题

    传送门

    Censor

    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 string w. The second line contains 1string p.

    (1≤length of w,p≤5⋅10^6w,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

    题意:给出一个单词和一段英文,要你删除你找到的第一个这个单词,并将剩下的合并成一个新的串,再继续找,直到找不出这个单词,输出最后的串。

    题解:本题相当于在主串里面找模式串,找到之后删掉它然后从前往后找模式串。我们想到KMP就是用来串的模式匹配的,但是KMP和这个不一样的是KMP找到一个模式串之后直接往后面去找,而这个可能存在删除位置之前与删除位置之后拼起来组成模式串的情况。那我们怎么解决呢?我们可以用一个数组记录主串每个位置相匹配的模式串的下一个位置(next[j]),当主串匹配完一个模式串的时候将j(对应的模式串下标)跳到当前主串位置-模式串长度的位置相匹配的模式串下一位置继续比较即可。最后输出的结果字符串我们可以用一个数组在比较时记录。

    代码:

    #include <cstdio>
    #include <cstring>
    #define ll long long
    using namespace std;
    const int N = 5e6 + 10;
    const int INF= 1<<30;
    char s[N],t[N],ans[N];
    int l1,l2,nt[N],pre[N];
    void getNext() {
        int i = 0, j = -1;
        nt[0] = -1;
        while (i < l1) {
            if (j == -1||t[j] == t[i])
                nt[++i] = ++j;
            else j = nt[j];
        }
    }
    void KMP() {
        getNext();
        int cnt = 0, i = 0, j = 0;
        for (;i<l2;i++,cnt++) {
            ans[cnt] = s[i];
            while (j>0&&s[i] != t[j]) j = nt[j];
            if (s[i] == t[j]) j++;
            if (j == l1) {
                cnt-=l1;
                j = pre[cnt];
            }
            pre[cnt] = j;
            ans[cnt+1] = '';
        }
        printf("%s
    ",ans);
    }
    int main() {
        while (~scanf("%s%s",t,s)) {
            l1 = strlen(t);
            l2 = strlen(s);
            KMP();
        }
        return 0;
    }
    View Code

    Censor

    frog is now a editor to censor so-called sensitive words (敏感词).

    She has a long text pp. Her job is relatively simple -- just to find the first occurence of sensitive word ww 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 11 string ww. The second line contains 11 string pp.

    (1length of w,p51061≤length of w,p≤5⋅106w,pw,p consists of only lowercase letter)

    Output

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

    Sample Input

        abc
        aaabcbc
        b
        bbb
        abc
        ab

    Sample Output

        a
        
        ab
  • 相关阅读:
    聊聊用机器人做无人驾驶
    透过国外初创公司看高精度地图难题
    百度Apollo无人驾驶入门课程下载
    关于高精度地图定义的探讨
    camera理论基础和工作原理
    FPGA配置OV5640摄像头及RGB图像数据采集
    详细的摄像头模组工作原理!!!
    摄像头PIN脚功能作用
    详解摄像头各个引脚的作用关系
    Linux下UDP的组播接收和发送的简单例子
  • 原文地址:https://www.cnblogs.com/l999q/p/11383171.html
Copyright © 2011-2022 走看看