• hdu 2222(AC自动机模版题)


    Keywords Search

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

    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
    AC自动机板子题
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cmath>
      4 #include<cstring>
      5 #include<algorithm>
      6 #include<queue>
      7 #include<map>
      8 #include<set>
      9 #include<vector>
     10 #include<cstdlib>
     11 #include<string>
     12 #define eps 0.000000001
     13 typedef long long ll;
     14 typedef unsigned long long LL;
     15 using namespace std;
     16 const int N=50000+100;
     17 struct node{
     18     int count;
     19     node *fail;
     20     node *next[26];
     21     node(){
     22         fail=NULL;
     23         count=0;
     24         memset(next,NULL,sizeof(next));
     25     }
     26 }*q[N];
     27 char str[1000000+10];
     28 char keyword[100];
     29 int head,tail;
     30 node *root;
     31 void insert(char *s){
     32     node *p=root;
     33     //cout<<3<<endl;
     34     int len=strlen(s);
     35     for(int i=0;i<len;i++){
     36         int v=s[i]-'a';
     37         if(p->next[v]==NULL)p->next[v]=new node();
     38         p=p->next[v];
     39     }
     40     p->count++;
     41    // cout<<2<<endl;
     42 }
     43 void build_AC_automation(node *root){
     44     root->fail=NULL;
     45     head=0,tail=0;
     46     q[head++]=root;
     47     while(head!=tail){
     48         node *p=NULL;
     49         node *temp=q[tail++];
     50         for(int i=0;i<26;i++){
     51             if(temp->next[i]!=NULL){
     52                 if(temp==root)temp->next[i]->fail=root;
     53                 else{
     54                     p=temp->fail;
     55                     while(p!=NULL){
     56                         if(p->next[i]!=NULL){temp->next[i]->fail=p->next[i];
     57                         break;
     58                         }
     59                         p=p->fail;
     60                     }
     61                     if(p==NULL)temp->next[i]->fail=root;
     62                 }
     63                 q[head++]=temp->next[i];
     64             }
     65         }
     66     }
     67 }/*
     68 int query(node *root){
     69      int i=0,cnt=0,index,len=strlen(str);
     70      node *p=root;
     71      while(str[i]){
     72          index=str[i]-'a';
     73          while(p->next[index]==NULL && p!=root) p=p->fail;
     74          p=p->next[index];
     75          p=(p==NULL)?root:p;
     76          node *temp=p;
     77          while(temp!=root && temp->count!=-1){
     78              cnt+=temp->count;
     79              temp->count=-1;
     80              temp=temp->fail;
     81          }
     82      }
     83      return cnt;
     84  } */
     85 int query(node *root){
     86     int ans=0;
     87     //cout<<2<<endl;
     88     int len=strlen(str);
     89     node *p=root;
     90     for(int i=0;i<len;i++){
     91         int v=str[i]-'a';
     92         while(p->next[v]==NULL&&p!=root)p=p->fail;
     93         p=p->next[v];
     94         if(p==NULL)p=root;
     95         node *temp=p;
     96         while(temp!=root){
     97             if(temp->count>=0){
     98                ans=ans+temp->count;
     99                 temp->count=-1;
    100 
    101             }
    102             else
    103                 break;
    104             temp=temp->fail;
    105         }
    106     }
    107  //cout<<2<<endl;
    108     return ans;
    109 }
    110 int main(){
    111     int n,t;
    112     scanf("%d",&t);
    113     while(t--){
    114         root=new node();
    115         scanf("%d",&n);
    116         getchar();
    117         for(int i=0;i<n;i++){
    118             gets(keyword);
    119             insert(keyword);
    120         }
    121         build_AC_automation(root);
    122         scanf("%s",str);
    123         cout<<query(root)<<endl;
    124     }
    125 }
     
    Sample Output
    3
  • 相关阅读:
    [SQLSERVER2005 ERROR]"附加数据库 对于 服务器“GNPC”失败"
    家用视频监控设备
    Layer Two Tunneling Protocol "L2TP"
    [转]char类型和string类型(C++,C#)
    Layer 2 Tunneling Protocol
    坚持坚持。。。
    转载几篇有用的文章
    常用的SQL语句(转载)
    python判断一个字符是否是xml合法字符
    搜索引擎推荐
  • 原文地址:https://www.cnblogs.com/Aa1039510121/p/6477136.html
Copyright © 2020-2023  润新知