Keywords Search-hdu2222-AC自动机

Problem Description
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
 
题意:输入n个模式串,和一个目标串,问在目标串中共有多少个不同的模式串与其匹配。此题运用AC自动机的模板来解决。
AC自动机的概念以及fail指针如何得来,请参见博客:https://blog.csdn.net/creatorx/article/details/71100840
#include <cstdio>
#include <string>
#include <queue>
#include <cstring>
using namespace std;
const int ALPHABET = 26;
const int MAXN = 1e7+5;
int cnt,N,head,tail,num;
char key[70],pattern[MAXN];
struct node{
    int count;//用于记录是否已被访问过
    node *next[ALPHABET];//用于指向下一个指针
    node *fail;//作为失败指针,每个节点要沿着它父亲的失败指针走
};
node Tree[500005];
node *root,*q[MAXN];

node* create(){
    node* p=&Tree[num++];
    p->count=0;
    p->fail=NULL;
    for(int i=0;i<ALPHABET;i++)
        p->next[i]=NULL;
    return p;
}

void insert(char *s){
    node *p=root;
    for(int i=0;s[i];i++){
        int x=s[i]-'a';
        if(p->next[x]==NULL)//注意next[]数组含有26个元素的容量
        {
             p->next[x]=create();
        }
        p=p->next[x];
    }
    p->count++;
}

void create_fail_pointer(){
    head=0;tail=1;
    q[head]=root;
    node *p,*tmp;
    while(head<tail){
        tmp=q[head++];
        for(int i=0;i<ALPHABET;i++){
            if(tmp->next[i]){
                if(tmp==root)
                {
                    tmp->next[i]->fail=root;//如果是除root外的第一层节点,那么这些节点的fail指针指向root
                }else{
                    p=tmp->fail;
                    while(p && p->next[i]==NULL)//注意此处为while循环,如果p的next[i]为空,说明当前节点的父亲节点没有与之相等的元素
                        p=p->fail;//p指向父亲节点的fail节点,如果为空,则当前节点的fail指针指向root,否则指向父亲节点下的相同元素
                    if(p==NULL) tmp->next[i]->fail=root;
                    else tmp->next[i]->fail=p->next[i];
                }
                q[tail++]=tmp->next[i];
            }
        }
    }
}

void ac(char *s)
{
    node* p =root;
    node* tmp;
    for(int i=0;s[i];i++){
        int x=s[i]-'a';
        if(p->next[x]==NULL){
            while(p && p->next[x]==NULL)
                p=p->fail;
            if(p==NULL) p=root;
            else{
                p=p->next[x];
                tmp=p;
                while(tmp!=root && tmp->count>=0){
                    cnt+=tmp->count;
                    tmp->count=-1;
                    tmp=tmp->fail;
                }
            }
        }else{
            p=p->next[x];
            tmp=p;
            while(tmp!=root && tmp->count>=0){
                cnt+=tmp->count;
                tmp->count=-1;
                tmp=tmp->fail;
            }
        }
    }
}

int main(){
    int T;scanf("%d",&T);
    while(T--){
        root=create();
        scanf("%d",&N);
        for(int i=1;i<=N;i++){
            scanf("%s",key);
            insert(key);
        }//整个步骤创建下来的树是一棵典型的字典树
        scanf("%s",pattern);
        cnt=0;
        create_fail_pointer();
        ac(pattern);
        printf("%d\n",cnt);
    }
    return 0;
}

 

posted @ 2019-07-16 20:46  里昂静  阅读(294)  评论(0编辑  收藏  举报