题意:给定一个模式串和文本,要求删除所有模式串。可能删除后会形成新的模式串,必须全部删除。


思路1:kmp算法求得失配数组,用一个match数组记录文本串中第i字符和未删除的字符能匹配模式串的长度。这样每次删除字符串之后就不用再匹配,直接查询match数组即可。用栈模拟,自己实现的栈可以加快速度。

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 5e6 + 5;

int fail[maxn]; //失配数组
int match[maxn];
char p[maxn], w[maxn], ans[maxn];

void getFail(char *s, int *fail, int n) {
    fail[0] = -1;
    for(int i = 1; i < n; ++i) {
        int j = fail[i-1];
        while(j != -1 && s[j+1] != s[i]) j = fail[j];
        if(s[j+1] == s[i]) fail[i] = j+1;
        else fail[i] = -1;
    }
}
int top;
int sta[maxn];
void kmp(char *p, char *w, int *fail) {
    int n = strlen(w), m = strlen(p);
    getFail(w, fail, n);

    top = 0;

    int now = -1;
    for(int i = 0; i < m; ++i) {
        ans[top] = p[i];
        while(now != -1 && w[now+1] != p[i]) now = fail[now];
        if(w[now+1] == p[i]) {
            now = now + 1;
        }
        match[i] = now;
        sta[++top] = i;
        //成功匹配w
        if(now == n-1) {
            top -= n;
            if(top == 0) now = -1;
            else now = match[sta[top]];
        }
    }
    ans[top] = '\0';
}
int main() {
    while(scanf("%s%s", w, p) == 2) {
        kmp(p, w, fail);
        printf("%s\n", ans);
    }
    return 0;
}

思路2:哈希技术真的好玄学。一直判断最后strlen(w)字符的哈是值是否和模式串的哈希一致,如果一致就删除。

#include <cstdio>
#include <cmath>
#include <cctype>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 5e6 + 5;
const int seed = 100003;
LL bit[maxn];
char p[maxn], w[maxn], ans[maxn];
LL sta[maxn];
void getBit() {
    bit[0] = 1;
    for(int i = 1; i < maxn; ++i)
        bit[i] = bit[i-1]*seed;
}

LL getHash(char *s, int len) {
    LL res = 0;
    for(int i = 0; i < len; ++i)
        res = res*seed + s[i];
    return res;
}

void solve(char *p, char *w) {
    int n = strlen(w), m = strlen(p);
    LL goal = getHash(w, n); 

    //栈 top=0表示栈空
    int top = 0;
    sta[top] = 0;

    for(int i = 0; i < m; ++i) {
        ans[top] = p[i];
        LL res = sta[top] * seed + p[i];
        sta[top++] = res;
        if(top >= n && res - sta[top-n]*bit[n] == goal) {
            top -= n;
        }
    }

    ans[top] = '\0';
    printf("%s\n", ans);
}

int main() {
    getBit();
    while(scanf("%s%s", w, p) == 2) {
        solve(p, w);
    }
    return 0;
}

如有不当之处欢迎指出!

SCU 4438 Censor KMP/Hash的更多相关文章

  1. ACM: SCU 4438 Censor - KMP

     SCU 4438 Censor Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice D ...

  2. SCU 4438 Censor|KMP变形题

    传送门 Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text P. He ...

  3. SCU 4438 Censor(Hash)题解

    题意:找出字符串p中的w串删除,反复操作,直到找不到w,输出这个串 思路:哈希处理前缀和,如果值相同就删掉. 代码: #include<iostream> #include<algo ...

  4. SCU 4438 Censor(哈希+模拟栈)

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text \(p\). He ...

  5. SCU 4438:Censor

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text p . Her j ...

  6. Codeforces 1090J $kmp+hash+$二分

    题意 给出两个字符串\(s\)和\(t\),设\(S\)为\(s\)的任意一个非空前缀,\(T\)为\(t\)的任意一个非空前缀,问\(S+T\)有多少种不同的可能. Solution 看了一圈,感觉 ...

  7. Censor SCU - 4438

    frog is now a editor to censor so-called sensitive words (敏感词). She has a long text (p). Her job is ...

  8. 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈

    [BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...

  9. Censor(KMP)

    Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...

随机推荐

  1. java基础-静态,非静态(构造)代码块,类加载

    static block and non-static block(constructor block) [toc] 想来想去,先来一题比较好 public class Foo { public st ...

  2. 企业级分布式存储应用与实战FastDFS实现

    FASTDFS是什么 FastDFS是由国人余庆所开发,其项目地址:https://github.com/happyfish100 FastDFS是一个轻量级的开源分布式文件系统,主要解决了大容量的文 ...

  3. MySQL存储过程中declare和set定义变量的区别

    在存储过程中常看到declare定义的变量和@set定义的变量.简单的来说,declare定义的类似是局部变量,@set定义的类似全局变量. 1.declare定义的变量类似java类中的局部变量,仅 ...

  4. 【转】globk中的控制文件

    globk_comb.cmd * This group must be first eq_file ../tables/eq_renames make_svs ../tables/sat1.apr c ...

  5. JavaScript:事件对象Event和冒泡

    本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. 绑定事件的两种方式 我们在上一篇文章中已经讲过事件的概念.这里讲一下注册 ...

  6. javascript中this指向问题

    本文参考http://www.ruanyifeng.com/blog/2010/04/using_this_keyword_in_javascript.html this是JavaScript的一个关 ...

  7. 用Lua定制Redis命令

    * { color: #3e3e3e } body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans ...

  8. Android 初了解

    1.1G-4G 1G 大哥大  语音通话 2G 小灵通  采用GSM,美国的一个军方标准,后来被民用了. 可以发短信了,上网的网址不是www,是wap.baidu.com 3G 可以上网了,直接用ww ...

  9. typedef和#define的简单比较

    1.通常说typedef比#define要好,尤其在有指针的情况下 typedef char* pStr1; #define pStr2 char* pStr1 s1,s2; pStr2 s3,s4; ...

  10. jupyter扩展插件Nbextensions使用

    本节主要解释jupyter中各种插件 原创文章,转载请务必注明原作者出处:http://www.cnblogs.com/cloud-ken/p/7401534.html Exercise Exerci ...