SCU 4438 Censor KMP/Hash
题意:给定一个模式串和文本,要求删除所有模式串。可能删除后会形成新的模式串,必须全部删除。
思路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的更多相关文章
- ACM: SCU 4438 Censor - KMP
SCU 4438 Censor Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice D ...
- SCU 4438 Censor|KMP变形题
传送门 Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text P. He ...
- SCU 4438 Censor(Hash)题解
题意:找出字符串p中的w串删除,反复操作,直到找不到w,输出这个串 思路:哈希处理前缀和,如果值相同就删掉. 代码: #include<iostream> #include<algo ...
- SCU 4438 Censor(哈希+模拟栈)
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text \(p\). He ...
- SCU 4438:Censor
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text p . Her j ...
- Codeforces 1090J $kmp+hash+$二分
题意 给出两个字符串\(s\)和\(t\),设\(S\)为\(s\)的任意一个非空前缀,\(T\)为\(t\)的任意一个非空前缀,问\(S+T\)有多少种不同的可能. Solution 看了一圈,感觉 ...
- Censor SCU - 4438
frog is now a editor to censor so-called sensitive words (敏感词). She has a long text (p). Her job is ...
- 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈
[BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...
- Censor(KMP)
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...
随机推荐
- linkin大话设计模式--简单工厂
linkin大话设计模式--工厂方法 什么是工厂方法:将多个类对象交给工厂来生成的设计被称为简单工厂模式,个人认为主要是为了实现解耦,在代码重构的时候会很重要. 代码如下: public class ...
- grep的小技巧
grep '^[^#]' /etc/openvpn/server.conf 中括号必须匹配一个字符^$属于标志位,不属于字符 grep没把\n看成字符 grep把空行看成^$ 还是perl的标准,空行 ...
- 【转】新手该如何学python怎么学好python?
1)学好python的第一步,就是马上到www.python.org网站上下载一个python版本.我建议初学者,不要下载具有IDE功能的集成开发环境,比如Eclipse插件等. 2) 下载完毕后,就 ...
- Cacti在selinux开启的情况下使用
# chcon -R -t httpd_sys_content_t /var/www/html/cacti
- 【Shell脚本学习指南笔记】重定向文件描述符 2>&1
如: make > results 2>&1 重定向 > results让文件描述符1(标准输出)作为文件results,接下来的重定向2>&1有两个部分.2& ...
- SpringMVC数据验证(AOP处理Errors和方法验证)
什么是JSR303? JSR 303 – Bean Validation 是一个数据验证的规范,2009 年 11 月确定最终方案. Hibernate Validator 是 Bean Valida ...
- MVVM之旅(1)创建一个最简单的MVVM程序
这是MVVM之旅系列文章的第一篇,许多文章和书喜欢在开篇介绍某种技术的诞生背景和意义,但是我觉得对于程序员来说,一个能直接运行起来的程序或许能够更直观的让他们了解这种技术.在这篇文章里,我将带领大家一 ...
- linux修改TCP最大连接数
环境 操作系统: oracle-linux7.3 修改系统支持的最大TCP连接 最大tcp连接数和系统允许打开的最大文件数,用户允许打开的最大文件数,TCP网络连接可用的端口范围有关,取上述的最小值: ...
- Django跨域请求之JSONP和CORS
现在来新建一个Django项目server01,url配置为 url(r'^getData.html$',views.get_data) 其对应的视图函数为get_data: from django. ...
- CentOS下内存使用率查看
freetotal used free shared buffers cachedMem: 1815340 1628680 ...