zoukankan      html  css  js  c++  java
  • [HDOJ2222]Keywords Search

    AC自动机入门题目~

    View Code
    1 #include <cstdio>
    2 #include <cstring>
    3
    4 usingnamespace std;
    5
    6 constint NS =26;
    7 constint MAXWL =64;
    8 constint MAXDL =1000005;
    9 constint MAXN =500005;
    10 constint MAXD =10240;
    11
    12 char description[MAXDL];
    13
    14 struct TRIE
    15 {
    16 int lable;
    17 int cnt;
    18 TRIE * fail;
    19 TRIE * son[NS];
    20 TRIE()
    21 {
    22 cnt =0;
    23 lable =-1;
    24 fail = NULL;
    25 for (int i =0;i < NS;i++)
    26 son[i] = NULL;
    27 }
    28 };
    29
    30 TRIE * queue[MAXN];
    31
    32 bool vis[MAXD];
    33
    34 void TrieInsert(char* word,int id,TRIE * root)
    35 {
    36 char* key;
    37 TRIE * tmp;
    38
    39 key = word;
    40 tmp = root;
    41
    42 while (*key)
    43 {
    44 int idx = (*key++) -'a';
    45 if (tmp->son[idx] == NULL) tmp->son[idx] =new TRIE();
    46 tmp = tmp->son[idx];
    47 }
    48 tmp->cnt++;
    49 tmp->lable = id;
    50 }
    51
    52 void getFail(TRIE * root)
    53 {
    54 int qh =-1;
    55 int qe =0;
    56
    57 root->fail = NULL;
    58 queue[0] = root;
    59
    60 while (qh != qe)
    61 {
    62 qh++;
    63 TRIE * tmp = queue[qh];
    64
    65 for (int i =0;i < NS;i++)
    66 if (tmp->son[i])
    67 {
    68 if (tmp == root) tmp->son[i]->fail = root;
    69 else
    70 {
    71 TRIE * p = tmp->fail;
    72 while (p)
    73 {
    74 if (p->son[i])
    75 {
    76 tmp->son[i]->fail = p->son[i];
    77 break;
    78 }
    79 p = p->fail;
    80 }
    81 if (p == NULL) tmp->son[i]->fail = root;
    82 }
    83
    84 qe++;
    85 queue[qe] = tmp->son[i];
    86 }
    87 }
    88 }
    89
    90 int TrieFind(TRIE * root)
    91 {
    92 int cnt;
    93 char* key;
    94 TRIE * tmp;
    95
    96 cnt =0;
    97 key = description;
    98 tmp = root;
    99
    100 while (*key)
    101 {
    102 int idx =*key -'a';
    103 while (tmp->son[idx] == NULL && tmp != root) tmp = tmp->fail;
    104 tmp = tmp->son[idx] == NULL ? root : tmp->son[idx];
    105
    106 TRIE * p = tmp;
    107 while (p != root && p->lable !=-1&&!vis[p->lable])
    108 {
    109 cnt += p->cnt;
    110 vis[p->lable] =true;
    111 p = p->fail;
    112 }
    113
    114 key++;
    115 }
    116 return cnt;
    117 }
    118
    119 void TrieDelete(TRIE * root)
    120 {
    121 for (int i =0;i < NS;i++)
    122 if (root->son[i])
    123 TrieDelete(root->son[i]);
    124 delete(root);
    125 }
    126
    127
    128 int main()
    129 {
    130 int cas;
    131 int wnum;
    132 char word[MAXWL];
    133
    134 scanf("%d",&cas);
    135
    136 for (int cc =0;cc < cas;cc++)
    137 {
    138 memset(vis,false,sizeof(vis));
    139
    140 TRIE * root =new TRIE();
    141
    142 scanf("%d",&wnum);
    143 for (int i =0;i < wnum;i++)
    144 {
    145 scanf("%s",word);
    146 TrieInsert(word,i +1,root);
    147 }
    148
    149 getFail(root);
    150
    151 scanf("%s",description);
    152
    153 printf("%d\n",TrieFind(root));
    154
    155 TrieDelete(root);
    156 }
    157 return0;
    158 }
  • 相关阅读:
    买书求如何获得折扣使价格最低
    团队开发NABC特点
    《梦断代码》读后感3
    结对开发5
    找水王问题续
    对搜狗输入法的评价
    团队项目之典型用户
    找水王问题
    电梯调度练习
    团队项目用户需求调研报告
  • 原文地址:https://www.cnblogs.com/debugcool/p/HDOJ2222.html
Copyright © 2011-2022 走看看