zoukankan      html  css  js  c++  java
  • hdu2222 ac自动机入门

    Keywords Search

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27138    Accepted Submission(s): 8871

    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

     ac自动机,模板题:

      1 #include <iostream>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <algorithm>
      5 #include <math.h>
      6 #include <queue>
      7 #include <string.h>
      8 using namespace std;
      9 typedef struct abc
     10 {
     11      abc *next[26];
     12     int end;
     13     abc *fail;
     14 }abcd;
     15 int ans;
     16 abcd *inti()
     17 {
     18     abcd *t;
     19     t=(abcd *)malloc(sizeof(abcd));
     20     t->end=0;
     21     t->fail=NULL;
     22     for(int i=0;i<26;i++)
     23     t->next[i]=NULL;
     24     return t;
     25 }
     26 void insert(abcd *t,char z[])
     27 {
     28     if(*z=='')
     29     {
     30         t->end++;
     31         return ;
     32     }
     33     if(t->next[*z-'a']==NULL)
     34     t->next[*z-'a']=inti();
     35     insert(t->next[*z-'a'],z+1);
     36 }
     37 void ac(abcd *t)
     38 {
     39     queue< abcd* >a;
     40     abcd *r,*f;
     41     while(!a.empty())a.pop();
     42     for(int i=0;i<26;i++)
     43     {
     44         if(t->next[i]!=NULL)
     45         a.push(t->next[i]),t->next[i]->fail=t;
     46         else
     47         t->next[i]=t;
     48     }
     49     while(!a.empty())
     50     {
     51         r=a.front();
     52         a.pop();
     53         for(int i=0;i<26;i++)
     54         {
     55             if(r->next[i]!=NULL)
     56             {
     57                 a.push(r->next[i]);
     58                 f=r->fail;
     59                 while(f->next[i] == NULL) f = f->fail;
     60                 r->next[i]->fail=f->next[i];
     61             }
     62         }
     63     }
     64 }
     65 void query(abcd *t,char x[])
     66 {
     67     abcd *p = t, *f;
     68     int i;
     69     while(*x)
     70     {
     71         i = *x - 'a';
     72         while(p->next[i] == NULL) p = p->fail;
     73         p = p->next[i];
     74         f = p;
     75         while(f != t && f->end !=-1)
     76         {
     77             ans+=f->end;
     78             f->end =-1;
     79             f = f->fail;
     80         }
     81         x++;
     82     }
     83 }
     84 void del(abcd *t)
     85 {
     86     for(int i=0;i<26;i++)
     87     {
     88         if(!t->next[i])
     89         del(t->next[i]);
     90     }
     91     free(t);
     92 }
     93 int main()
     94 {
     95     int t,n,i;
     96     cin>>t;
     97     char z[60];
     98     char x[1000009];
     99     while(t--)
    100     {
    101         abcd *t;
    102         t=inti();
    103         cin>>n;
    104         for(i=0;i<n;i++)
    105         {
    106             cin>>z;
    107             insert(t,z);
    108         }
    109         cin>>x;
    110         ac(t);
    111         ans=0;
    112         query(t,x);
    113         cout<<ans<<endl;
    114         del(t);
    115     }
    116 }
    View Code
  • 相关阅读:
    算法时间复杂度、空间复杂度(大O表示法)
    六、Java“毒丸”使用示例,实现取消任务
    四、获取IP地址工具包
    SEDA架构程序实现
    二十一、curator recipes之TreeCache
    二十、curator recipes之NodeCache
    十九、curator recipes之PathChildrenCache
    十八、curator recipes之DistributedDelayQueue
    Mysql学习笔记【一、环境安装&配置】
    Go学习笔记【一、概述】
  • 原文地址:https://www.cnblogs.com/ERKE/p/3278197.html
Copyright © 2011-2022 走看看