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


思路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. Django 发送邮件

    问题: 对于一些错误信息或用户注册账号的时候,需要给用户发送邮件进行验证. 以用户注册发邮件为例子,用户向后端提起注册,后端收到用户邮箱,对邮箱格式进行验证,然后发送邮件,邮件内容中包括邮件标题.邮件 ...

  2. python_16_序列化

    如何实现不同编程语言进行交互? json数据,相当于语言中间的沟通桥梁 什么是json数据? imoprt json json.dumps(内容)                    --把内容转换 ...

  3. Python--socketserve源码分析(二)

    BaseServer::self.process_request(request, client_address) 实现原理: 在类的继承关系中,当子类中没有相应的方法时就会去父类中寻找, 当继承多个 ...

  4. junit4初体验

    OK,现在我们正式开始junit4系列的整理.前面的junit38作为4的补充知道就好了,实际编码中我们以4为主.这里先来一把junit的初体验,同时也让我们来一步一步的了解下TDD的好处. ORM大 ...

  5. 解决eclipse maven工程中src/main/resources目录下创建的文件夹所显示样式不是文件夹,而是"包"图标样式的问题

    参考:http://blog.csdn.net/luwei42768/article/details/72268246 eclipse项目中创建maven项目后,有时在执行命令maven update ...

  6. MySQL数据库学习笔记----MySQL多表查询之外键、表连接、子查询、索引

    本章主要内容: 一.外键 二.表连接 三.子查询 四.索引 一.外键: 1.什么是外键 2.外键语法 3.外键的条件 4.添加外键 5.删除外键 1.什么是外键: 主键:是唯一标识一条记录,不能有重复 ...

  7. Python之算法

    一.什么算法 算法:一个计算过程,解决问题的方法 二.时间复杂度 看代码:                                                               ...

  8. CSS3属性详解(图文教程)

    本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. 前言 我们在上一篇文章中学习了CSS3的选择器,本文来学一下CSS3的一 ...

  9. CentOS7系统配置国内yum源和epel源

    1.首先进入/etc/yum.repos.d/目录下,新建一个repo_bak目录,用于保存系统中原来的repo文件 [root@bogon ~]# cd /etc/yum.repos.d/ [roo ...

  10. win7 MySQL Connector/Net 安装卸载问题

    问题1:卸载MySQL Connector Net 6.9.9 卸载程序无法卸载 方法:注册表搜索 MySQL Connector Net 6.9.9 全部删除 ******************* ...