题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3065
flag:AC自动机
*在这里,AC自动机应用于处理统计任何单词(许多个)在文本中出现的次数;
*需要每遍历到文本串的一个字母x,已经确定好位置(loc指于当前某个单词前缀的结尾单词,此前缀记为A),利用失配指针,回溯所有能有为A的后缀子串,统计出这些是单词的后缀子串(idx 不为0);
*这里没有两个单词是相同的,减少了一些麻烦;
*比较坑的是: ①文本串word很长,而且带空格和非字母字符,所有输入必须用gets(word),前面有输入n,所以我们还得
去掉n后面的换行getchar();遍历文本word的时候,遇到非大写字母,loc指向Node(根),跳过!
②题目的意思只有一组数据,测试却有多组,要注意!
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <string.h>
#define Mod 998244353
using namespace std;
char s[1005][55];
char word[2000010];
long long num[1005]={0};
struct node_t{
node_t* child[26];
node_t* failer;
int idx;
}Node[50100];
int toUsed=1;
void Insert(int a){
node_t* loc=Node;
int str=strlen(s[a]);
for(int i=0;i<str;++i){
int sn=s[a][i]-'A';
if(loc->child[sn]==0){
memset(Node+toUsed,0,sizeof(node_t));
loc->child[sn]=Node + toUsed++;
}
loc=loc->child[sn];
}
loc->idx=a;
}
void mk_failer(){
queue<node_t*>Q;
Q.push(Node);
while(!Q.empty()){
node_t* loc=Q.front();Q.pop();
for(int i=0;i<26;++i){
if(!loc->child[i])continue;
Q.push(loc->child[i]);
if(loc==Node){loc->child[i]->failer=Node;continue;}
node_t* p=loc->failer;
while(p){
if(p->child[i]){loc->child[i]->failer=p->child[i];break;}
p=p->failer;
}
if(p==NULL)loc->child[i]->failer=Node;
}
}
}
void ac_automaton(const char word[ ]){
node_t* loc=Node;
int str=strlen(word);
for(int i=0;i<str;++i){
int sn=word[i]-'A';
if(sn<0||sn>25){loc=Node;continue;}
while(!loc->child[sn]&&loc!=Node)loc=loc->failer;
loc=loc->child[sn];
if(!loc)loc=Node;
node_t* temp=loc;
while(temp){
if(temp->idx) ++num[temp->idx];
temp=temp->failer;
}
}
}
int main(){
int n;
while(~scanf("%d",&n)){
toUsed=1;
memset(num,0,sizeof(num));
memset(Node,0,sizeof(node_t));
int maxn=0;int idx=0;
for(int i=1;i<=n;++i){
scanf("%s",s[i]);
Insert(i);
}
getchar();
gets(word);
mk_failer();
ac_automaton(word);
for(int i=1;i<=n;++i)
if(num[i])cout<<s[i]<<": "<<num[i]<<endl;
}
return 0;
}