Keywords Search HDU - 2222
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters ‘a’-‘z’, and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
Output
Print how many keywords are contained in the description.
Sample Input
1
5
she
he
say
shr
her
yasherhs
Sample Output
3
题意:
给你一堆单词,再给定一个长字符串,问长字符串中,能找到多少子串是所给的单词
分析:
AC自动机的模板,它就是专门解决这类问题的
code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000010;
int cnt;
struct node{
node *Next[26];
node *fail;
int sum;
};//字典树节点
node *root;
char key[70];//单词
node *q[maxn];//模拟队列
int head,tail;
node *newnode;
char pattern[maxn];//模式串
int n;
void Insert(char *s){//建立字典树
node *p = root;
for(int i = 0; s[i]; i++){
int x = s[i] - 'a';
if(p->Next[x] == NULL){//还没有这个节点新建一个
newnode = (node*)malloc(sizeof(node));
for(int j = 0; j < 26; j++){//初始化
newnode->Next[j] = NULL;
}
newnode->fail = NULL;
newnode->sum = 0;
p->Next[x] = newnode;
}
p = p->Next[x];//走到下一个节点
}
p->sum++;//以最后一个字母为结尾的单词个数加一
}
void getFail(){//得到fail指针,形成ac自动机
head = 0;
tail = 0;
q[tail++] = root;
node *p;
node *tmp;
while(head < tail){
tmp = q[head++];
for(int i = 0; i < 26; i++){
if(tmp->Next[i]){//如果当前节点的下一个节点有i这个字母,下面我们要确定当前节点下一个节点的fail指针指向哪里
if(tmp == root){
tmp->Next[i]->fail = root;//下一个节点fail指针直接指向根节点
}
else{
p = tmp->fail;//否则先找到当前节点的fail指向的节点
while(p){
if(p->Next[i]){//如果当前节点的fail指向的节点下一个有我们需要在字符,就让当前节点下一个节点的fail指向它
tmp->Next[i]->fail = p->Next[i];
break;
}
p = p->fail;
}
if(p == NULL) tmp->Next[i]->fail = root;//如果找不到符合的就直接指回根,相当于要从头开始
}
q[tail++] = tmp->Next[i];//下一个节点入队
}
}
}
}
void ac_Actomation(char *ch){
node *p = root;
int len = strlen(ch);
for(int i = 0; i < len; i++){
int x = ch[i] - 'a';
while(!p->Next[x] && p != root) p = p->fail;//如果进入这个循环说明失配,即当前节点下一个没有字符ch[i],所以找fail
p = p->Next[x];//从fail下一个开始
if(!p) p = root;//如果是空的就说明确实没有以当前字符为结尾的单词,让他等于根
node *tmp = p;
while(tmp != root){//若不为根(说明能够找到以当前字符为结尾的单词)
if(tmp->sum >= 0){
cnt += tmp->sum;
tmp->sum = -1;
}
else{
break;//没有就终止,因为如果这个都没有,它的fail也不可能有
}
tmp = tmp->fail;
}
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
root = (node*)malloc(sizeof(node));
for(int j = 0; j < 26; j++){
root->Next[j] = NULL;
}
root->fail = NULL;
root->sum = 0;
scanf("%d",&n);
for(int i = 0; i < n; i++){
scanf("%s",key);
Insert(key);
}
scanf("%s",pattern);
cnt = 0;
getFail();
ac_Actomation(pattern);
printf("%d\n",cnt);
}
return 0;
}