• 统计难题 HDOJ--2222


    Keywords Search

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


    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代码:
      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<stdlib.h>
      4 typedef struct Node_Tree
      5 {
      6     int cnt; 
      7     struct Node_Tree *child[26]; 
      8     struct Node_Tree *fail; 
      9 }Node; 
     10 Node *root; 
     11 char keywd[51]; 
     12 char decpt[1000001]; 
     13 Node *q[500001]; 
     14 int tail = 0, head = 0; 
     15 void insert()
     16 {
     17     if(keywd == NULL)
     18         return ; 
     19     int i; 
     20     char *p = keywd;
     21     Node *t = root;
     22     while(*p != '')
     23     {
     24         if(t->child[*p - 'a'] == NULL)
     25         {
     26             Node *temp = (Node *)malloc(sizeof(Node)); 
     27             memset(temp, 0, sizeof(Node)); 
     28             for(i = 0; i < 26; i ++)
     29             {
     30                 temp->child[i] = NULL; 
     31             }
     32             temp->cnt = 0;
     33             temp->fail = NULL; 
     34             t->child[*p - 'a'] = temp; 
     35         }
     36         t = t->child[*p - 'a']; 
     37         p ++; 
     38     }
     39     t->cnt ++; 
     40 }
     41 
     42 void getfail()
     43 {
     44     int i; 
     45     q[tail++] = root;
     46     while(tail != head)             //BFS; 
     47     {
     48         Node *p = q[head++];
     49         Node *temp = NULL;
     50         for(i = 0; i < 26; i ++)
     51         {
     52             if(p->child[i] != NULL)
     53             {
     54                 if(p == root)
     55                 {
     56                     p->child[i]->fail = root; 
     57                 }
     58                 else
     59                 {
     60                     temp = p->fail; 
     61                     while(temp != NULL)
     62                     {
     63                         if(temp->child[i] != NULL)
     64                         {
     65                             p->child[i]->fail = temp->child[i]; 
     66                             break ; 
     67                         }
     68                         temp = temp->fail; 
     69                     }
     70                     if(temp == NULL)
     71                         p->child[i]->fail = root; 
     72                 }
     73                 q[tail++] = p->child[i]; 
     74             }
     75         }
     76     }
     77 }
     78 
     79 int search()
     80 {
     81     int i, ret = 0; 
     82     char *p = decpt; 
     83     Node *t = root;
     84     while(*p != '')
     85     {
     86         while(t->child[*p - 'a'] == NULL && t != root)
     87             t = t->fail; 
     88         t = t->child[*p - 'a']; 
     89         if(t == NULL)
     90             t = root;
     91         Node *temp = t; 
     92         while(temp != root && temp->cnt != -1)
     93         {
     94             ret += temp->cnt;
     95             temp->cnt = -1;
     96             temp = temp->fail;
     97         }
     98         p ++; 
     99     }
    100     return ret; 
    101 }
    102 
    103 int main(int argc, char const *argv[]) 
    104 {
    105     int c, i, t; 
    106     scanf("%d", &c);
    107     Node TREEROOT; 
    108     root = &TREEROOT; 
    109     while(c --)
    110     {
    111         for(i = 0; i < 30; i ++)
    112         {
    113             root->child[i] = NULL; 
    114             root->cnt = 0;
    115             root->fail = NULL; 
    116         }
    117         tail = head = 0; 
    118         memset(decpt, 0, sizeof(decpt));
    119         memset(keywd, 0, sizeof(keywd)); 
    120         scanf("%d", &t); 
    121         while(t --)
    122         {
    123             scanf("%s", keywd); 
    124             insert(); 
    125             memset(keywd, 0, sizeof(keywd)); 
    126         }
    127         getfail(); 
    128         scanf("%s", decpt);
    129         printf("%d
    ", search());
    130     }
    131     return 0; 
    132 }
  • 相关阅读:
    hadoop的运行模式
    集群之间配置 SSH无密码登录
    NameNode故障处理方法
    HDFS的HA(高可用)
    DataNode的工作机制
    NameNode和SecondaryNameNode的工作机制
    HDFS读写数据流程
    Linux软件包管理
    DNS服务之二:Bind97服务安装配置
    ssl协议、openssl及创建私有CA
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3484845.html
Copyright © 2020-2023  润新知