码迷,mamicode.com
首页 > 其他好文 > 详细

【HDOJ】1277 全文检索

时间:2014-12-26 22:55:42      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

AC自动机,静态数组,动态分配TLE。

  1 /* 1277 */
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <cstdlib>
  6 #include <queue>
  7 using namespace std;
  8 
  9 #define MAXL 60005
 10 #define TRIEN 10
 11 
 12 typedef struct Trie {
 13     int n;
 14     Trie *fail;
 15     Trie *next[TRIEN];
 16 } Trie;
 17 
 18 Trie tries[1000024];
 19 int nTrie;
 20 char s[MAXL];
 21 char str[65];
 22 int ans[10005], n;
 23 bool flag;
 24 Trie *root;
 25 
 26 Trie *newNode() {
 27     memset(tries[nTrie].next, 0, sizeof(tries[nTrie].next));
 28     tries[nTrie].n = 0;
 29     tries[nTrie].fail = NULL;
 30     return &tries[nTrie++];
 31 }
 32 
 33 void create(char str[], int v) {
 34     int i = 0, id;
 35     Trie *p = root, *q;
 36 
 37     while (str[i]) {
 38         id = str[i] - 0;
 39         ++i;
 40         if (p->next[id] == NULL) {
 41             q = newNode();
 42             p->next[id] = q;
 43         }
 44         p = p->next[id];
 45     }
 46     p->n = v;
 47 }
 48 
 49 void build_fail() {
 50     int i;
 51     Trie *p, *q;
 52     queue<Trie *> Q;
 53 
 54     for (i=0; i<TRIEN; ++i) {
 55         if (root->next[i]) {
 56             root->next[i]->fail = root;
 57             Q.push(root->next[i]);
 58         }
 59     }
 60 
 61     while (!Q.empty()) {
 62         p = Q.front();
 63         Q.pop();
 64         for (i=0; i<TRIEN; ++i) {
 65             if (p->next[i]) {
 66                 q = p->fail;
 67                 while (q) {
 68                     if (q->next[i]) {
 69                         p->next[i]->fail = q->next[i];
 70                         break;
 71                     }
 72                     q = q->fail;
 73                 }
 74                 if (q == NULL)
 75                     p->next[i]->fail = root;
 76                 Q.push(p->next[i]);
 77             }
 78         }
 79     }
 80 }
 81 
 82 void search(char s[]) {
 83     int i = 0, id;
 84     Trie *p = root, *q;
 85 
 86     while (s[i]) {
 87         id = s[i] - 0;
 88         ++i;
 89         while (p->next[id]==NULL && p!=root)
 90             p = p->fail;
 91         p = p->next[id];
 92         if (p == NULL)
 93             p = root;
 94         q = p;
 95         while (q != root) {
 96             if (q->n) {
 97                 flag = true;
 98                 ans[n++] = q->n;
 99                 q->n = 0;
100             }
101             q = q->fail;
102         }
103     }
104 }
105 
106 void del(Trie *t) {
107     if (t == NULL)
108         return ;
109     for (int i=0; i<TRIEN; ++i)
110         del(t->next[i]);
111     free(t);
112 }
113 
114 void init() {
115     n = 0;
116     flag = false;
117     nTrie = 0;
118     root = newNode();
119 }
120 
121 int main() {
122     int t, m;
123     int i, j, k;
124 
125     #ifndef ONLINE_JUDGE
126         freopen("data.in", "r", stdin);
127         //freopen("data.out", "w", stdout);
128     #endif
129 
130     while (scanf("%d %d", &t, &m) != EOF) {
131         for (i=0; i<t; ++i) {
132             scanf("%s%*c", str);
133             strcat(s, str);
134         }
135         getchar();
136         init();
137         for (i=1; i<=m; ++i) {
138             scanf("%*s %*s %*s %s", str);
139             create(str, i);
140         }
141         build_fail();
142         search(s);
143         if (flag) {
144             printf("Found key:");
145             for (i=0; i<n; ++i)
146                 printf(" [Key No. %d]", ans[i]);
147             printf("\n");
148         } else {
149             printf("No key can be found !\n");
150         }
151     }
152 
153     return 0;
154 }

 

【HDOJ】1277 全文检索

标签:

原文地址:http://www.cnblogs.com/bombe1013/p/4187632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!