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 ...
随机推荐
- linux_常用命令_2
rev 反向读取, reverse echo 123456 | rev # 结果为 654321 rev Name.txt # 行号没变,每一行的数据翻转过来 less 具有more命令所有功能,更加 ...
- Ipython的安装/ipython notebook的简单使用
安装了pyhton的windows版的shell好久了,今天才听说Ipython这个东西,确实在windows下的python shell很难用,一旦输入错误就要重新来过 了解了下ipython,然后 ...
- JDK8 HashMap 源码解析
HashMap中数据结构 在jdk1.7中,HashMap采用数组+链表(拉链法).因为数组是一组连续的内存空间,易查询,不易增删,而链表是不连续的内存空间,通过节点相互连接,易删除,不易查询.Has ...
- ferror,clearerr和EOF含义
1.我们并不是实时操纵文件,也不是实时生效,它依赖于缓冲区.非缓冲模式编程与常规区别,就是实时与不实时的区别. 2.//fgetc fputc, fgets fputs, fgetwc fputwc, ...
- exit、_exit、abort、return的区别
转自:http://www.cnblogs.com/fixer/archive/2013/05/14/3078660.html _exit(): 跟exit功能大致相同,区别在于_exit不会清空所有 ...
- 基于redis的cas实现
cas是我们常用的一种解决并发问题的手段,小到CPU指令集,大到分布式存储,都能看到cas的影子.本文假定你已经充分理解一般的cas方案,如果你还不知道cas是什么,请自行百度 我们在进行关系型数据库 ...
- iOS-Xcode编码自动补全失效
1. 退出 Xcode 2. 重启电脑 3. 找到 这个 DerivedData 文件夹 删除 (路径: ~/Library/Developer/Xcode/DerivedData) 4. 删除这个 ...
- 如何使用Python读取大文件
背景 最近处理文本文档时(文件约2GB大小),出现memoryError错误和文件读取太慢的问题,后来找到了两种比较快Large File Reading 的方法,本文将介绍这两种读取方法. 准备工作 ...
- LongAdder基础
LongAdder是JDK8中并发包中的一个新类,和AtomicLong都使用CAS,但是性能比AtomicLong更好. LongAdder在AtomicLong的基础上进行了热点分离,热点分离类似 ...
- java HotSpot 内存管理白皮书
原文见:http://www.open-open.com/lib/view/open1381034220705.html.查阅资料后,对原文做了补充. 文中关于JVM的介绍基于JDK1.6的Hotsp ...