zoukankan      html  css  js  c++  java
  • 【AC自动机】HDU中模板题

    【HDU2222】

    最纯粹的裸题,错误点详见注释。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cstring>
      4 #include<algorithm>
      5 #include<queue>
      6 using namespace std;
      7 struct ACauto
      8 {
      9     int sum;
     10     ACauto* next[26];
     11     ACauto* fail;
     12     ACauto()
     13     {
     14         sum=0;
     15         for (int i=0;i<26;i++) next[i]=NULL;
     16         fail=NULL;
     17     }
     18 };
     19 
     20 void insert(ACauto* root,char* str)
     21 {
     22     int len=strlen(str);
     23     ACauto* now=root;
     24     for (int i=0;i<len;i++)
     25     {
     26         int index=str[i]-'a';
     27         if (now->next[index]==NULL)
     28         {
     29             ACauto* tmp=new ACauto;
     30             now->next[index]=tmp;
     31         }
     32         now=now->next[index]; 
     33     }
     34     now->sum++;
     35 }
     36 
     37 void build(ACauto* root)
     38 {
     39     queue<ACauto*> que;
     40     que.push(root);
     41     while (!que.empty())
     42     {
     43         ACauto* tmp=que.front();que.pop();
     44         for (int i=0;i<26;i++)
     45         {
     46             if (tmp->next[i]==NULL) continue;
     47             if (tmp==root)
     48                 tmp->next[i]->fail=root;
     49             else
     50             {
     51                 ACauto* p=tmp->fail;
     52                 while (p!=NULL)
     53                 {
     54                     if (p->next[i]!=NULL)
     55                     {
     56                         tmp->next[i]->fail=p->next[i];
     57                         break;
     58                     }
     59                     p=p->fail;
     60                 }
     61                 if (p==NULL) tmp->next[i]->fail=root; 
     62             }
     63             que.push(tmp->next[i]);
     64         }
     65     }
     66 }
     67 
     68 int query(ACauto* root,char* str)
     69 { 
     70     int ans=0,len=strlen(str);
     71     ACauto* p=root;
     72     for (int i=0;i<len;i++)
     73     {
     74         int index=str[i]-'a';
     75         while (p->next[index]==NULL && p!=root) p=p->fail;
     76         /*错误点:上述语句是while语句不是if语句*/ 
     77         p=p->next[index];
     78         p=(p==NULL)?root:p;
     79         ACauto* tmp=p;
     80         while (tmp!=root)
     81         /*-1表示这个单词之前已经被统计过了,不再重复计算*/ 
     82         {
     83             if (tmp->sum>=0)
     84             {
     85                 ans+=tmp->sum;
     86                 tmp->sum=-1;
     87             }
     88             else
     89             break;
     90             tmp=tmp->fail;
     91         }
     92     }
     93     return ans;
     94 }
     95 
     96 void submain()
     97 {
     98     ACauto* root=new ACauto;
     99     int n;
    100     char str[1000000+50];
    101     scanf("%d",&n);
    102     for (int i=0;i<n;i++)
    103     {
    104         scanf("%s",str);
    105         insert(root,str);
    106     } 
    107     build(root);
    108     scanf("%s",str);
    109     cout<<query(root,str)<<endl;
    110 }
    111 
    112 int main() 
    113 {
    114     int T;
    115     scanf("%d",&T);
    116     for (int kase=0;kase<T;kase++) submain();
    117     return 0;
    118 } 
  • 相关阅读:
    今週のschedule
    软件架构师应该知道的97件事
    没办法的复习
    优秀程序员的45个习惯
    程序员如何追女孩
    那些相见恨晚的 JavaScript 技巧
    CodeSmith开发系列资料总结
    HR的至高机密:20个公司绝对不会告诉你的潜规则
    asp.net页面出错时的处理方法
    Asp.net 文件上传的 FileUpload FileName 和 FileUpload PostedFile.FileName的细节问题
  • 原文地址:https://www.cnblogs.com/iiyiyi/p/5184589.html
Copyright © 2011-2022 走看看