hdu 2896(AC自动机)

  1 #include <cstdio>

  2 #include <cstring>

  3 #include <cstdlib>

  4 #include <iostream>

  5 

  6 using namespace std;

  7 

  8 const int N = 205;

  9 const int L = 10005;

 10 const int M = 100010;

 11 

 12 int ans[5];

 13 char pat[N], str[L];

 14 struct node {

 15     int n;

 16     node *prefix;

 17     node *child[128];

 18     node() {

 19         n = 0;

 20         prefix = NULL;

 21         for (int i=0; i<128; ++i) child[i] = NULL;

 22     }

 23 }*root, *Q[M];

 24 int front, rear;

 25 

 26 int cmp(const void *a, const void *b) {

 27     return *(int *)a - *(int *)b;

 28 }

 29 

 30 void insert(char pat[], int n) {

 31     node *p = root;

 32     for (int k,i=0; pat[i]; ++i, p=p->child[k]) {

 33         k = (int)pat[i];

 34         if (!p->child[k]) p->child[k] = new node();

 35     }

 36     p->n = n;

 37 }

 38 

 39 void buildAc(node *root) {

 40     node *p, *q;

 41     root->prefix = NULL;

 42     front = rear = 0;

 43     Q[front++] = root;

 44     while (rear < front) {

 45         p = Q[rear++];

 46         for (int i=0; i<128; ++i) {

 47             if (p->child[i]) {

 48                 if (p == root) p->child[i]->prefix = root;

 49                 else {

 50                     q = p->prefix;

 51                     while (q!=root && !q->child[i]) q = q->prefix;

 52                     if (q->child[i]) p->child[i]->prefix = q->child[i];

 53                     else p->child[i]->prefix = root;

 54                 }

 55                 Q[front++] = p->child[i];

 56             }

 57         }

 58     } 

 59 }

 60 

 61 int match(node *root, char str[]) {

 62     node *p = root;

 63     int cnt = 0;

 64     for (int k,i=0; str[i]; ++i) {

 65         k = (int)str[i];

 66         while (p!=root && !p->child[k]) p = p->prefix;

 67         p = p->child[k];

 68         if (!p) p = root;

 69         node *tmp = p;

 70         while (tmp!=root && tmp->n) {

 71             ans[cnt++] = tmp->n;

 72             tmp = tmp->prefix;

 73         }

 74     }

 75     return cnt;

 76 }

 77 

 78 void del(node *p) {

 79     if (!p) return ;

 80     for (int i=0; i<128; ++i) {

 81         if (p->child[i]) del(p->child[i]);

 82     }

 83     delete p;

 84     p = NULL;

 85 }

 86 

 87 int main() {

 88     int n, cs, ss;

 89     while (scanf("%d", &n) != EOF) {

 90         ss = 0;

 91         root = new node();

 92         for (int i=1; i<=n; ++i) scanf("%s", pat), insert(pat, i);

 93         buildAc(root);

 94         scanf ("%d", &n);

 95         for (int i=1; i<=n; ++i) {

 96             scanf ("%s", str);

 97             if (cs = match(root, str)) {

 98                 qsort(ans, cs, sizeof(int), cmp);

 99                 printf ("web %d:", i);

100                 for (int i=0; i<cs; ++i) printf (" %d", ans[i]);

101                 puts("");

102                 ++ss;

103             }

104         }

105         printf ("total: %d\n", ss);

106         del(root);

107     }

108     return 0;

109 }

 

你可能感兴趣的:(AC自动机)