zoukankan      html  css  js  c++  java
  • HDOJ 2222 AC自动机模板题

    Keywords Search

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 44704    Accepted Submission(s): 14109


    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自动机
    然后加上AC自动机的介绍AC自动机算法详解
    #include<iostream>
    #include<stdio.h>
    #include<cstring>
    #include<queue>
    using namespace std;
    struct node{
           node *fail,*s[27];
           int w;
    }*head;
    int t,n,i,j,sum;
    char temp[51],str[1000001];
    queue<node*> myqueue;
    node *getfail(node *p,int k){
         if (p->s[k]!=NULL) return p->s[k];
         else 
              if (p==head) return head;
              else return getfail(p->fail,k);
    }
    void built_trie(){
         node *root=head;
         node *tep;
         for (j=0;j<strlen(temp);j++){
             if (root->s[temp[j]-'a']==NULL){
                tep=new node;
                for (int k=0;k<26;k++)
                    tep->s[k]=NULL;
                tep->w=0;
                tep->fail=head;
                root->s[temp[j]-'a']=tep;
                }
             
             root=root->s[temp[j]-'a'];
             if (j==strlen(temp)-1) root->w+=1;
             }
         return ;
    }
    void built_ac(){
         node *root;
         while (!myqueue.empty()) myqueue.pop();
         myqueue.push(head);
         while (!myqueue.empty()){//构造fail指针用BFS的方法扩展
               root=myqueue.front();
               myqueue.pop();
               for (j=0;j<26;j++)
                   if (root->s[j]!=NULL){
                      myqueue.push(root->s[j]);
                      if (root==head) root->s[j]->fail=head;
                      else root->s[j]->fail=getfail(root->fail,j);
                      }
               }
         return ;
    }
    void find(){
         int len=strlen(str);
         node* tep;
         node *root=head;
         for (j=0;j<len;j++){
             while (root->s[str[j]-'a']==NULL && root!=head) root=root->fail;
             root=(root->s[str[j]-'a']==NULL)?head:root->s[str[j]-'a'];
             tep=root;
             while (tep!=head){//如果单词A出现过了,那么与他具有相同前缀的单词也都出现过了
                   sum+=tep->w;
                   tep->w=0;
                   tep=tep->fail;
                   }
             }
         return ;
         }
    int main(){
        scanf("%d",&t);
        while (t--){
              sum=0;
              head=new node;
              for (i=0;i<26;i++)
                  head->s[i]=NULL;
              head->fail=head;
              head->w=0;
              scanf("%d",&n);
              for (i=0;i<n;i++){
                  scanf("%s",temp);
                  built_trie();
                  }
              built_ac();
              scanf("%s",str);
              find();
              printf("%d
    ",sum);
              }
        return 0;
    }


    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    iOS仿UC浏览器顶部频道滚动效果
    OC中NSClassFromString()与NSStringFromClass()的用法及应用场景
    利用工具MailUtils实现邮件的发送,遇到的大坑,高能预警!!
    使用response实现文件下载注意点
    mac版MyEclipse的安装及创建web项目
    Mac系统下安装Tomcat,以及终端出现No such file or directory的错误提示解决方案
    机器学习笔记-Python简介
    解决mscordacwks.dll不一致问题
    IIS日志如何记录X-Forwarded-For
    深入理解Redis(番外)——持久化
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768455.html
Copyright © 2011-2022 走看看