题目分析
这道题就是一个字符串匹配问题,对于单词建AC自动机,然后对于模式串进行匹配,俗话说套模板这些都不是问题,但是我们在对于文本串进行匹配时需要注意的问题就是如果我们进行匹配的时候有可能已经遇到了单词,但是匹配的时候我们会发现如果一直往后匹配的话那么有可能这个串的失败指针指向的节点是单词尾,但是在当前串并不是单词为,这时候就会发生错误,比如这组数据。2 asdf sd asd,如果不把失败指针的val值往上推显然是不正确的。这样一来就很明显了,在建AC自动机的时候进行,或者文本串匹配的时候一直沿着fail指针递推到root节点找最长单词节点也是可以的。这道题我借鉴了别人的代码,我看了好多,因为别人都是静态建树,我习惯于动态建树,但是发现了一个问题,如果你动态建树一定要用C++交不然极其容易超空间,同样我的代码的运行时间很长900+ms,还是推荐大家用静态建树吧,至少不会爆空间。
动态建树
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX 26
const int maxn = 1e6+10;
struct Trie{
Trie *next[MAX], *fail;
int len, val;
Trie(){
memset(next, 0, sizeof(next));
fail = NULL;
len = val = 0;
}
}*root;
void Insert(char *s){
Trie *p = root;
for(int i = 0; s[i]; i++){
int id = s[i] - 'a';
if(p->next[id] == NULL) p->next[id] = new Trie();
p->next[id]->len = p->len + 1;
p = p->next[id];
}
p->val = p->len;
}
void build(){
root->fail = NULL;
queue <Trie * > que;
que.push(root);
while(!que.empty()){
Trie *cur = que.front(); que.pop();
if(cur->fail != NULL) cur->val = max(cur->val, cur->fail->val);
for(int i = 0; i < MAX; i++){
if(cur->next[i] == NULL) continue;
if(cur == root) cur->next[i]->fail = root;
else{
Trie *tmp = cur->fail;
while(tmp != NULL && tmp->next[i] == NULL) tmp = tmp->fail;
cur->next[i]->fail = tmp == NULL?root:tmp->next[i];
}
que.push(cur->next[i]);
}
}
}
char str[maxn];
int pos[maxn];
void solve(){
int id;
memset(pos, -1, sizeof(pos));
Trie *p = root;
for(int i = 0; str[i]; i++){
if(str[i] >= 'A' && str[i] <= 'Z') id = str[i] - 'A';
else if(str[i] >= 'a' && str[i] <= 'z') id = str[i] - 'a';
else{p = root; continue; }
while(p != NULL && p->next[id] == NULL) p = p->fail;
if(p != NULL) p = p->next[id];
else p = root;
if(p->val) pos[i-(p->val)+1] = i;
}
int en = -1;
for(int i = 0; str[i]; i++){
en = max(en, pos[i]);
if(i <= en) putchar('*');
else putchar(str[i]);
}
puts("");
}
void Delete(Trie *rt){
if(rt == NULL) return;
for(int i = 0; i < MAX; i++)
if(rt->next[i] != NULL) Delete(rt->next[i]);
delete(rt);
rt = NULL;
}
int main(){
int T,n;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
root = new Trie();
while(n--){
scanf("%s", str);
Insert(str);
}
build();
getchar();
gets(str);
solve();
Delete(root);
}
return 0;
}
静态建树
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e6+100;
struct Trie{
int next[26], fail, len; //孩子结点指针, 失败指针, 单词的长度
int val; //单词尾部标记
}trie[maxn];
char str[maxn]; //文本串
char ps[maxn]; //单词
int tot; //for count
int NewTrie(){
memset(trie[tot].next, -1, sizeof(trie[tot].next));
trie[tot].val = 0;
trie[tot].fail = -1;
return tot++;
}
void Insert(int root){
for(int i = 0; ps[i]; i++){
int id = ps[i] - 'a';
if(trie[root].next[id] == -1) trie[root].next[id] = NewTrie();
trie[trie[root].next[id]].len = trie[root].len + 1;
root = trie[root].next[id];
}
trie[root].val = trie[root].len;
}
void build(int root){ //建AC自动机
queue <int> q;
q.push(root);
trie[root].fail = 0;
while(!q.empty()){
root = q.front();
trie[root].val = max(trie[root].val, trie[trie[root].fail].val);
q.pop();
for(int i = 0; i < 26; i++){
if(trie[root].next[i] == -1) continue;
if(!root) trie[trie[root].next[i]].fail = 0;
else{
int tmp = trie[root].fail;
while(tmp && trie[tmp].next[i] == -1) tmp = trie[tmp].fail;
trie[trie[root].next[i]].fail = trie[tmp].next[i] != -1?trie[tmp].next[i]:0;
}
q.push(trie[root].next[i]);
}
}
}
int pos[maxn];
void solve(int root){
int id;
memset(pos, -1, sizeof(pos));
for(int i = 0; str[i]; i++){
if(str[i] >= 'A' && str[i] <= 'Z') id = str[i] - 'A';
else if(str[i] >= 'a' && str[i] <= 'z') id = str[i] - 'a';
else{ root = 0; continue;}
while(root && trie[root].next[id] == -1) root = trie[root].fail;
if(trie[root].next[id] != -1) root = trie[root].next[id];
if(trie[root].val) pos[i-trie[root].val+1] = i;
}
int en = -1;
for(int i = 0; str[i]; i++){
en = max(en, pos[i]);
if(i <= en) putchar('*');
else putchar(str[i]);
}
printf("\n");
}
int main(){
int T,n;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
tot = 0;
int root = NewTrie();
trie[root].len = 0;
trie[root].val = 0;
while(n--){
scanf("%s", ps);
Insert(root);
}
build(root);
getchar();
gets(str);
solve(root);
}
return 0;
}