HDU 2222 Keywords Search(AC自动机)

来源:互联网 发布:adobe pdf mac版 编辑:程序博客网 时间:2024/06/03 17:12
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
15shehesayshrheryasherhs
 
Sample Output
3
 
题目大意:给定一段文本,查询给定个单词出现了多少个。

AC自动机裸题,就是贴个板子。

代码:

#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>#include<queue>using namespace std;typedef struct node{    node *next[26];    node *fail;    int sum;}node;queue<node *> q;const int maxn = 1e6 + 5;char s[maxn];node *root = NULL;int ans;// 建立字典树void Insert(){    int len = strlen(s);    node *p = root;    for(int i = 0;i < len; ++i)    {        int x = s[i] - 'a';        if(p->next[x] == NULL)        {            node *temp = (node *)malloc(sizeof(node));            temp->fail = NULL;            for(int i = 0;i < 26; ++i) temp->next[i] = NULL;            temp->sum = 0;            p->next[x] = temp;        }        p = p->next[x];    }    p->sum++;}// 构造fail指针void Build_fail_pointer(){    while(!q.empty()) q.pop();    node *p = NULL,*temp = NULL;    q.push(root);    while(!q.empty())    {        temp = q.front(),q.pop();        for(int i = 0;i < 26; ++i)        {            if(temp->next[i])            {                if(temp == root) temp->next[i]->fail = root;                else                {                    p = temp->fail;                    while(p)                    {                        if(p->next[i])                        {                            temp->next[i]->fail = p->next[i];                            break;                        }                        p = p->fail;                    }                    if(p == NULL) temp->next[i]->fail = root;                }                q.push(temp->next[i]);            }        }    }}void Freenode(node *ro){    if(ro == NULL) return;    for(int i = 0;i < 26; ++i)    {        if(ro->next[i]) Freenode(ro->next[i]);    }    free(ro);}// AC自动机匹配void AC_automation(){    int len = strlen(s);    node *p = root;    for(int i = 0;i < len; ++i)    {        int x = s[i] - 'a';        while(!p->next[x] && p != root) p = p->fail;        p = p->next[x];        if(!p) p = root;        node *temp = p;        while(temp != root)        {            if(temp->sum > 0)            {                ans += temp->sum;                temp->sum = 0;            }            else break;            temp = temp->fail;        }    }}int main(){    int t,n;    scanf("%d",&t);    while(t--)    {        root = (node *)malloc(sizeof(node));        for(int i =0;i < 26;++i) root->next[i] = NULL;        root->fail = NULL;        root->sum = 0;        ans = 0;        scanf("%d",&n);        while(n--)        {            scanf(" %s",s);            Insert();        }        Build_fail_pointer();        scanf(" %s",s);        AC_automation();        printf("%d\n",ans);    }    return 0;}