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


思路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. linux_磁盘体系

    未曾习艺先学礼,未曾学武先习德 当今磁盘都是温室磁盘,原理是一样的,高速转动的的盘,磁头做径向运动 当今磁盘的发展趋势: 体积更小.速度更快.容量更大.使用更安全 速度更快: 主轴转速: 10000/ ...

  2. js_1_变量类型

    js中有哪些变量类型? 数字(包括int和float),字符串,数组(字典,js没有字典类型,把字典看成一个对象) 如何把字符转成数字呢? obj.parseInt()         //  转化成 ...

  3. 小白的Python之路 day5 re正则模块

    re正则模块 一.概述 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,要讲他的具体用法要讲一本书!它内嵌在Python中,并通过 re 模块实现.你可以为想要匹配的相应字符串 ...

  4. 如何用Python爬虫实现百度图片自动下载?

    Github:https://github.com/nnngu/LearningNotes 制作爬虫的步骤 制作一个爬虫一般分以下几个步骤: 分析需求 分析网页源代码,配合开发者工具 编写正则表达式或 ...

  5. ubuntu网桥设置

    什么是桥接? 桥接(Bridging),是指依据OSI网络模型的链路层的地址,对网络数据包进行转发的过程,工作在OSI的第二层.一般的交换机,网桥就有桥接作用. 一般的交换机,网桥就有桥接作用.就交换 ...

  6. DELL服务器硬件信息采集SHELL脚本

    DELL服务器硬件信息采集SHELL脚本最近需要做资产列表,要采集DELL服务器的硬件信息,包括如下信息:1.操作系统信息(类型,版本,内核,平台,主机名)2.主板信息(厂商,机型,序列号)3.CPU ...

  7. iOS-Wonderful 完美颜色库

    开发中颜色的使用也是非常频繁的,这里推荐一个dsxNiubility大牛写的颜色库:Wonderful:它的好用就是很清楚的把每个常用的颜色进行了由浅到深的分层,让我们使用时可以根据自己对颜色的深浅直 ...

  8. C# 语法学习整理

    1.协变与逆变的概念 文章地址:https://segmentfault.com/a/1190000007005115 **************************************** ...

  9. python---协程 学习笔记

    协程 协程又称为微线程,协程是一种用户态的轻量级线程 协程拥有自己的寄存器和栈.协程调度切换的时候,将寄存器上下文和栈都保存到其他地方,在切换回来的时候,恢复到先前保存的寄存器上下文和栈,因此:协程能 ...

  10. 测试SM图床