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 ...
随机推荐
- python_18_反射
什么是反射? -- 通过输入字符串来获取和修改 类(属性+方法),用字符串来映射内存对象,用于人机交互 反射有哪几种方法? -- getattr() --获取字符串 ...
- JMeter之断言 - 响应文本
1. 响应数据: 2. 添加响应断言: 3.设置响应断言,本例中 设置 响应文本 中 包括 success 字符串的 为真,即通过. 4.如果设置 响应文本 中 包括 error 字符串的 为真, ...
- CSS常用字体名称
CSS样式中常用的字体名称 css中引入字体: @font-face { font-family: "AncientWar"; src: url('style/css/font ...
- Linux Shell 文件描述符 及 stdin stdout stderr 重定向
Abstract: 1) Linux Shell 命令的标准输入.标准输出.标准错误,及其重定位: 2)Linux Shell 操作自定义文件描述符: 文件描述符是与文件相关联的一些整数,他们保持与已 ...
- Struts2是什么?
Struts2是什么: Struts2是整合了struts1和webwork的技术优点的使用广泛的MVC框架: Struts2的特点: 1.基于MVC框架,结构清晰,便于开发人员掌控开发流程: 2.使 ...
- VNC配置
简介 VNC (Virtual Network Console)是虚拟网络控制台的缩写.它 是一款优秀的远程控制工具软件,由著名的 AT&T 的欧洲研究实验室开发的.VNC 是在基于 UNIX ...
- 细数Python Flask微信公众号开发中遇到的那些坑
最近两三个月的时间,断断续续边学边做完成了一个微信公众号页面的开发工作.这是一个快递系统,主要功能有用户管理.寄收件地址管理.用户下单,订单管理,订单查询及一些宣传页面等.本文主要细数下开发过程中遇到 ...
- 应用ntpdate小工具同步时间
应用ntpdate小工具同步时间: ntpdate pool.ntp.org 中国的时间服务器有: ntpdate .cn.pool.ntp.org ntpdate .asia.pool.ntp.or ...
- CF 455D. Serega and Fun [分块 deque]
Serega and Fun 题意: [l,r]循环右移一位,查询区间内某个数出现次数 为什么好多人用链表?反正我是不会写双向链表 完全可以分块然后模拟啊...中间的块只会插入删除一个元素呀....用 ...
- BZOJ 1355: [Baltic2009]Radio Transmission [KMP 循环节]
1355: [Baltic2009]Radio Transmission Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 792 Solved: 535 ...