hdu2222-Keywords Search 【AC自动机】 Keywords Search

http://acm.hdu.edu.cn/showproblem.php?pid=2222

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 49940    Accepted Submission(s): 16020


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
 
题意:多模式串匹配,AC自动机的入门版。求多个模式串在母串出现的总个数。
三步走,第一步建立字段数,把模式串整合为一棵树;第二步最重要,为各节点初始化fail指针,bfs搜索,功能类似于kmp的next数组,root的fail指向null,第二层指向root,第三层以后,判断其父亲的fail是否有同样的儿子,有就fail指过去那个儿子,没有就找其父亲的fail的fail,循环至root,没办法,就指向root(见下图);第三步就是搜索,与第二步类似,沿着fail指针找就对了,具体看代码。
hdu2222-Keywords Search   【AC自动机】
Keywords Search
代码:
  1 #include <fstream>
  2 #include <iostream>
  3 using namespace std;
  4 
  5 #define MAXLEN 1000005
  6 #define N 26
  7 #define QUELEN 500005
  8 #define SLEN 55
  9 
 10 struct Node
 11 {
 12     Node *word[N];
 13     Node *fail;
 14     int cnt;
 15     Node (): cnt (0), fail(NULL)
 16     {
 17         memset (word, NULL, sizeof (word));
 18     }
 19 }*root;
 20 
 21 int n;
 22 char str[MAXLEN];
 23 
 24 void Insert (char*);
 25 void Init ();
 26 void BuildACAutomation ();
 27 int ACSearch (char*);
 28 
 29 int main ()
 30 {
 31     //freopen("D:\input.in","r",stdin);
 32     int T;
 33     scanf ("%d", &T);
 34     while (T--)
 35     {
 36         Init ();
 37         BuildACAutomation ();
 38         scanf ("%s", str);
 39         printf ("%d
", ACSearch (str));
 40     }
 41     return 0;
 42 }
 43 void Insert (char *s)
 44 {
 45     Node *p = root;
 46     int i = 0;
 47     while (s[i])
 48     {
 49         int tmp = s[i] - 97;
 50         if (!p->word[tmp])
 51         {
 52             p->word[tmp] = new Node;
 53         }
 54         p = p->word[tmp];
 55         i++;
 56     }
 57     p->cnt++;
 58 }
 59 void Init ()
 60 {
 61     char s[SLEN];
 62     root = new Node;
 63     scanf ("%d", &n);
 64     for (int i = 0; i < n; i++)
 65     {
 66         scanf ("%s", s);
 67         Insert (s);
 68     }
 69 }
 70 void BuildACAutomation ()
 71 {
 72     Node *que[QUELEN];
 73     int front = 0, rear = 0;
 74     for (int i = 0; i < N; i++)
 75     {
 76         if (root->word[i])//处理第二层,使其fail指针都指向root,而root的fail默认指向null
 77         {
 78             que[rear++] = root->word[i];
 79             root->word[i]->fail = root;
 80         }
 81     }
 82     while (front < rear)//处理其他层
 83     {
 84         Node *p = que[front++], *cur;
 85         for (int i = 0; i < N; i++)
 86         {
 87             if (p->word[i])
 88             {
 89                 que[rear++] = p->word[i];
 90                 cur = p->fail;
 91                 while (cur)
 92                 {
 93                     if (cur->word[i])
 94                     {
 95                         p->word[i]->fail = cur->word[i];
 96                         break;
 97                     }
 98                     cur = cur->fail;
 99                 }
100                 if (!cur)
101                 {
102                     p->word[i]->fail = root;
103                 }
104             }
105         }
106     }
107 }
108 int ACSearch (char *s)
109 {
110     int i = 0, ans = 0;
111     Node *p = root, *cur;
112     while (s[i])
113     {
114         int tmp = s[i] - 97;
115         while ((!p->word[tmp]) && p != root)
116         {
117             p = p->fail;
118         }
119         p = p->word[tmp];
120         if (!p)
121         {
122             p = root;
123         }
124         cur = p;
125         while (cur && (~cur->cnt))
126         {
127             ans += cur->cnt;
128             cur->cnt = -1;
129             cur = cur->fail;
130         }
131         i++;
132     }
133     return ans;
134 }