zoukankan      html  css  js  c++  java
  • hdu 2222 Keywords Search(AC自动机)

    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
     
    分析:

    在构造完这棵Tire之后,接下去的工作就是构造下失败指针。构造失败指针的过程概括起来就一句话:设这个节点上的字母为C,
    沿着他父亲的失败指针走,直到走到一个节点,他的儿子中也有字母为C的节点。然后把当前节点的失败指针指向那个字母也为C
    的儿子。如果一直走到了root都没找到,那就把失败指针指向root。具体操作起来只需要:先把root加入队列(root的失败指针
    指向自己或者NULL),这以后我们每处理一个点,就把它的所有儿子加入队列,队列为空。


    最后,我们便可以在AC自动机上查找模式串中出现过哪些单词了。匹配过程分两种情况:(1)当前字符匹配,表示从当前节点沿着
    树边有一条路径可以到达目标字符,此时只需沿该路径走向下一个节点继续匹配即可,目标字符串指针移向下个字符继续匹配;
    (2)当前字符不匹配,则去当前节点失败指针所指向的字符继续匹配,匹配过程随着指针指向root结束。重复这2个过程中的任意
    一个,直到模式串走到结尾为止。

    代码:

      1 #include<iostream>
      2 #include<queue>
      3 using namespace std;
      4 struct Node
      5 {
      6     Node *fail;
      7     Node *next[26];
      8     int count;
      9     Node()
     10     {
     11         fail=NULL;
     12         count=0;
     13         memset(next,NULL,sizeof(next));
     14     }
     15 };
     16 char str[1000001];
     17 void buildingtree(Node *head)
     18 {
     19     Node *h=head;
     20     int i=0;
     21     for(i=0;str[i];i++)
     22     {
     23         int id=str[i]-'a';
     24         if(h->next[id]==NULL)
     25             h->next[id]=new Node;
     26         h=h->next[id];
     27     }
     28     h->count++;
     29 }
     30 void buildingfail(Node *head)
     31 {
     32     Node *h=head,*p;
     33     queue<Node *> q;
     34     q.push(h);
     35     while(!q.empty())
     36     {
     37         h=q.front();
     38         q.pop();
     39         int i;
     40         for(i=0;i<26;i++)
     41             if(h->next[i]!=NULL)
     42             {
     43                 p=h->fail;
     44                 while(p!=NULL)
     45                 {
     46                     if(p->next[i]!=NULL)
     47                     {
     48                         h->next[i]->fail=p->next[i];
     49                         break;
     50                     }
     51                     p=p->fail;
     52                 }
     53                 if(p==NULL)
     54                     h->next[i]->fail=head;
     55                 q.push(h->next[i]);
     56             }
     57     }
     58 }
     59 void freetree(Node *h)
     60 {
     61     int i;
     62     for(i=0;i<26;i++)
     63         if(h->next[i]!=NULL)
     64             freetree(h->next[i]);
     65     delete h;
     66 }
     67 int searchans(Node *head)
     68 {
     69     Node *h=head;
     70     int ans=0,i;
     71     for(i=0;str[i];i++)
     72     {
     73         int id=str[i]-'a';
     74         while(h->next[id]==NULL&&h!=head)
     75             h=h->fail;
     76         h=h->next[id];
     77         if(h==NULL)
     78             h=head;
     79         Node *temp=h;
     80         while(temp!=head&&temp->count!=-1)
     81         {
     82             ans+=temp->count;
     83             temp->count=-1;
     84             temp=temp->fail;
     85         }
     86     }
     87     return ans;
     88 }
     89 int main()
     90 {
     91     int t,n;
     92     scanf("%d",&t);  
     93     while(t--)
     94     {
     95         scanf("%d",&n);
     96         getchar();
     97         Node *head=new Node;
     98         while(n--)
     99         {
    100             gets(str);
    101             buildingtree(head);
    102         }
    103         buildingfail(head);
    104         gets(str);
    105         printf("%d\n",searchans(head));
    106         freetree(head);
    107     }
    108     return 0;
    109 }
  • 相关阅读:
    人脸识别常用数据集大全(12/20更新)
    103 保序回归 isotonic regression
    SVM的概率输出(Platt scaling)
    scikit_learn 中文说明入门
    外点惩处函数法·约束优化问题
    unigui 设置单元格颜色
    一些仪器的解码程序(delphi)
    phpstudy 配置 memcached / memcache
    OmniThreadLibrary学习笔记
    注意微信支付的配置
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2677071.html
Copyright © 2011-2022 走看看