您的位置:首页 > 其它

2015 四川省赛 C Censor(哈希 | KMP)

2015-10-02 17:42 274 查看
题意:

给出一个N<=5×106的串,并给出一个M<=5×106敏感词,不断从中删除敏感词直到不能删除为止,求剩余串

分析:

用栈来维护当前扫描的位置,每次判断栈顶M长度的元素是不是敏感词,是就删除栈顶M元素,重复该过程

如何高效的判断是不是敏感词,考虑KMP得到敏感词的next的数组,然后做KMP匹配,并用pre数组记录栈内元素的匹配位置

如果可以匹配长度等于M可以删除,并根据pre数组重置匹配位置

哈希同理,维护栈内元素的前缀哈希值hi,每次可以根据hm=hi−MAGICm∗hi−m得到栈顶M个元素的哈希值

代码:

哈希

//
//  Created by TaoSama on 2015-10-01
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 5e6 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int H = 107;

char s
, t
;
int stk
, top;

typedef long long LL;
LL h
;

int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);

while(scanf("%s%s", t, s) == 2) {
int n = strlen(s), m = strlen(t);
LL power = 1, target = 0;
for(int i = 0; i < m; ++i) {
power = power * H % MOD;
target = (target * H % MOD + t[i]) % MOD;
}

top = 0;
for(int i = 0; i < n; ++i) {
stk[++top] = s[i];
h[top] = (h[top - 1] * H % MOD + stk[top]) % MOD;
while(top >= m) {
LL cur = (h[top] - power * h[top - m] % MOD + MOD) % MOD;
if(cur == target) top -= m;
else break;
}
}
for(int i = 1; i <= top; ++i) putchar(stk[i]);
puts("");
}
return 0;
}


KMP

//
//  Created by TaoSama on 2015-10-01
//  Copyright (c) 2015 TaoSama. All rights reserved.
//
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 5e6 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;

int n, m;
char s
, t
;
int nxt
, pre
;

void getNxt(char *s) {
nxt[0] = -1;
int i = 0, j = -1;
while(i < m) {
if(j == -1 || s[i] == s[j]) nxt[++i] = ++j;
else j = nxt[j];
}
}

char stk
;
int top;
int h
;

int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);

while(scanf("%s%s", t, s) == 2) {
n = strlen(s), m = strlen(t);
getNxt(t);

//        for(int i = 0; i < n; ++i) printf("%3c", s[i]); puts("");
//        for(int i = 0; i < n; ++i) printf("%3d", i); puts("");

top = 0;
for(int i = 0, j = 0; i < n; ++i) {
stk[++top] = s[i];
while(j > 0 && stk[top] != t[j]) j = nxt[j];
if(stk[top] == t[j]) ++j;
if(j == m) {
top -= m;
j = pre[top];
}
pre[top] = j;
}
for(int i = 1; i <= top; ++i) putchar(stk[i]);
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  kmp 海尔