zoukankan      html  css  js  c++  java
  • HDU 2222 AC自动机

    Keywords Search

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


    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
     
    Author
    Wiskey
     
      1 #pragma comment(linker, "/STACK:1024000000,1024000000")
      2 #include<iostream>
      3 #include<cstdio>
      4 #include<cmath>
      5 #include<string>
      6 #include<queue>
      7 #include<algorithm>
      8 #include<stack>
      9 #include<cstring>
     10 #include<vector>
     11 #include<list>
     12 #include<set>
     13 #include<map>
     14 #include<bitset>
     15 #include<time.h>
     16 using namespace std;
     17 typedef struct trie_node{
     18     struct trie_node *fail;
     19     struct trie_node *next[27];
     20     int coun;
     21     trie_node(){
     22     fail=NULL;
     23     coun=0;
     24     memset(next,NULL,sizeof(next));
     25     }
     26 }trie;
     27 trie *q[500010];
     28 char keyword[55];
     29 char str[1000006];
     30 int head,tail;
     31 
     32 void insert(char *str,trie_node *root){
     33   trie_node *p=root;
     34   int i=0;
     35   int index;
     36   while(str[i]){
     37     index=str[i]-'a';
     38     if(p->next[index]==NULL) p->next[index]=new trie_node();
     39     p=p->next[index];
     40     ++i;
     41   }
     42   p->coun++;
     43 }
     44 
     45 
     46 void build_ac_automation(trie_node *root){
     47     int i;
     48     head=tail=0;
     49     root->fail=NULL;
     50     q[tail++]=root;
     51     while(head!=tail){
     52         trie_node *temp=q[head++];
     53         trie_node *p=NULL;
     54         for(i=0;i<26;i++){
     55             if(temp->next[i]!=NULL){
     56                 if(temp==root) temp->next[i]->fail=root;
     57                 else{
     58                     p=temp->fail;
     59                     while(p!=NULL){
     60                         if(p->next[i]!=NULL){
     61                         temp->next[i]->fail=p->next[i];
     62                         break;
     63                     }
     64                     p=p->fail;
     65                 }
     66                 if(p == NULL) temp->next[i]->fail=root;
     67             }
     68             q[tail++]=temp->next[i];
     69         }
     70     }
     71   }
     72 }
     73 
     74 
     75 int query(char *s,trie_node *root){
     76     int len=strlen(s);
     77     int cnt=0;
     78     trie_node *p=root;
     79     for(int i=0;i<len;i++){
     80         int index=s[i]-'a';
     81         while(p->next[index]==NULL&&p!=root) p=p->fail;
     82         p=p->next[index];
     83         p=(p==NULL)?root:p;
     84         trie_node *temp = p;
     85         while(temp!=root&&temp->coun!=-1){
     86             cnt+=temp->coun;
     87             temp->coun=-1;
     88             temp=temp->fail;
     89         }
     90     }
     91     return cnt;
     92 }
     93 
     94 int T,n;
     95 char s[1000006];
     96 int main(){
     97     scanf("%d",&T);
     98     for(int j=1;j<=T;j++){
     99         trie_node *root =new trie_node();
    100         scanf("%d",&n);
    101         for(int i=1;i<=n;i++){
    102             scanf("%s",s);
    103             insert(s,root);
    104         }
    105         build_ac_automation(root);
    106         scanf("%s",str);
    107         printf("%d
    ",query(str,root));
    108     }
    109 }
  • 相关阅读:
    《分析服务从入门到精通读书笔记》第五章、创建多维数据集之浏览数据(1)
    灵活运用SQL Server2008 SSIS变量
    SSIS几个通用属性
    [原创]Silverlight开发实践系列导航(提供源码)
    【转】Scott_ASP.NET MVC框架(第四部分) 处理表单编辑和提交场景
    C#数据结构(二)栈和队列
    C#数据结构(一)线性表
    django学习(一)
    谈谈电子商务网站的促销规则(定价策略)设计(三)
    GAE使用中的问题
  • 原文地址:https://www.cnblogs.com/hsd-/p/7324437.html
Copyright © 2011-2022 走看看