SOJ 4438 Censor(KMP匹配)

C - Censor
Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Censor

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

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

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

Output

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

Sample Input

    abc
    aaabcbc
    b
    bbb
    abc
    ab

Sample Output

    a
    
    ab

/*头文件模板*/

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)

typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef pair<LL, LL>PLL;
typedef unsigned long long uLL;

template<typename T>
void print (T* p, T* q, string Gap = " ", bool flag = false) {
    int d = p < q ? 1 : -1;
    while (p != q) {
        if (flag) cout << Gap[0] << *p << Gap[1];
        else cout << *p;
        p += d;
        if (p != q && !flag) cout << Gap;
    }
    cout << endl;
}

template<typename T>
void print (const T &a, string bes = "") {
    int len = bes.length();
    if (len >= 2) cout << bes[0] << a << bes[1] << endl;
    else cout << a << endl;
}

template<typename T>
void debug (T* p, T* q, string Gap = " ", bool flag = false) {
    int d = p < q ? 1 : -1;
    cout << "Debug out : ";
    while (p != q) {
        if (flag) cout << Gap[0] << *p << Gap[1];
        else cout << *p;
        p += d;
        if (p != q && !flag) cout << Gap;
    }
    cout << endl;
}

template<typename T>
void debug (const T &a, string bes = "") {
    int len = bes.length();
    cout << "Debug out : ";
    if (len >= 2) cout << bes[0] << a << bes[1] << endl;
    else cout << a << endl;
}

void IO_Init() {
    ios::sync_with_stdio (false);
}

LL LLabs (const LL a) {
    return a >= 0 ? a : -a;
}

const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 5e6 + 5;
const int INF = 0x3f3f3f3f;

/*头文件模板*/




char a[MAXN], b[MAXN], L[MAXN];
int nexts[MAXN];

struct o {
    char c;
    int l;
    o() {}
    o (char c, int l) : c (c), l (l) {}
};




void getNextval (char *p) {
    int plen = strlen (p);
    int k = nexts[0] = -1;
    int j = 0;
    while (j < plen - 1) {
        if (k == -1 || p[j] == p[k]) {
            j++;
            k++;
            if (p[j] != p[k]) {
                nexts[j] = k;
            } else {
                nexts[j] = nexts[k];
            }
        } else {
            k = nexts[k];
        }
    }
}

int kmp (char *s, char *p) {
    int i = 0, j = 0;
    int slen = strlen (s);
    int plen = strlen (p);
    stack<o>O;
    while (i < slen) {
        if (j == -1 || s[i] == p[j]) {
            i++;
            j++;
            O.push (o (s[i - 1], j) );
        } else {
            j = nexts[j];
        }
        if (j == plen) {
            int l = plen;
            while (l --) O.pop();
            if (O.empty()) {
                j = 0;
            } else {
                j = O.top().l;
            }
        }
    }


    int cnt = 0;
    while(!O.empty()){
        L[cnt ++] = O.top().c;
        O.pop();
    }
    for(int i = cnt - 1;i >= 0;i --){
        printf("%c", L[i]);
    }
    printf("\n");
}




int main() {
#ifndef ONLINE_JUDGE
    //FIN;
    //FOUT;
#endif
    IO_Init();
    while (~scanf ("%s%s", a, b) ) {

        getNextval (a);

        kmp (b, a);



    }


    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值