HD2222 Keywords Search(AC自动机入门题)

然而还不是很懂=_=

  1 #include <iostream>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <cstdio>
  5 #include <queue>
  6 using namespace std;
  7 const int Max = 1000000+100;
  8 char str[Max];
  9 struct node
 10 {
 11     int cnt;
 12     struct node * Next[26];
 13     struct node * fail;
 14     void init()
 15     {
 16         for (int i = 0; i < 26; i++)
 17             Next[i] = NULL;
 18         cnt = 0;
 19         fail = NULL;
 20     }
 21 };
 22 node * root;
 23 void Insert()
 24 {
 25     node * p = root;
 26     int len = strlen(str);
 27     for (int i = 0; i < len; i++)
 28     {
 29         int id = str[i] - 'a';
 30         if (p->Next[id] == NULL)
 31         {
 32             p->Next[id] = new node();
 33             p->Next[id]->init();
 34         }
 35         p = p->Next[id];
 36     }
 37     p->cnt++;
 38 }
 39 void getFail()
 40 {
 41     node * p = root, *temp, *son;
 42     queue<struct node * > que;
 43     que.push(p);
 44     while (!que.empty())
 45     {
 46         temp = que.front();
 47         que.pop();
 48         for (int i = 0; i < 26; i++)
 49         {
 50             son = temp->Next[i];
 51             if (son != NULL)
 52             {
 53                 if (temp == root)
 54                 {
 55                     son->fail = root;
 56                 }
 57                 else
 58                 {
 59                     p = temp->fail;
 60                     while (p)
 61                     {
 62                         if (p->Next[i])
 63                         {
 64                             son->fail = p->Next[i];
 65                             break;
 66                         }
 67                         p = p->fail;
 68                     }
 69                     if (!p)
 70                         son->fail = root;
 71                 }
 72                 que.push(son);
 73             }
 74         }
 75     }
 76 }
 77 void querry()
 78 {
 79     int len, cnt = 0;
 80     len = strlen(str);
 81     node * p, * temp;
 82     p = root;
 83     for (int i = 0; i < len; i++)
 84     {
 85         int pos = str[i] - 'a';
 86         while (!p->Next[pos] && p != root)
 87             p = p->fail;
 88         p = p->Next[pos];
 89         if (!p)
 90             p = root;
 91         temp = p;
 92         while (temp != root)
 93         {
 94             if (temp->cnt >= 0)
 95             {
 96                 cnt += temp->cnt;
 97                 temp->cnt = -1;
 98             }
 99             else
100                 break;
101             temp = temp->fail;
102         }
103     }
104     printf("%d\n", cnt);
105 }
106 int main()
107 {
108     int test, n;
109     scanf("%d", &test);
110     while (test--)
111     {
112         root = new node();
113         root->init();
114         root->fail = NULL;
115         scanf("%d", &n);
116         getchar();
117         for (int i = 0; i < n; i++)
118         {
119             gets(str);
120             Insert();
121         }
122         getFail();
123         gets(str);
124         querry();
125     }
126     return 0;
127 }
View Code

 

posted @ 2016-04-28 21:30  zhaop  阅读(183)  评论(0编辑  收藏  举报