zoukankan      html  css  js  c++  java
  • hdu 2846 Repository(字典树)

    Problem Description
    When you go shopping, you can search in repository for avalible merchandises by the computers and internet. First you give the search system a name about something, then the system responds with the results. Now you are given a lot merchandise names in repository and some queries, and required to simulate the process.
     
    Input
    There is only one case. First there is an integer P (1<=P<=10000)representing the number of the merchanidse names in the repository. The next P lines each contain a string (it's length isn't beyond 20,and all the letters are lowercase).Then there is an integer Q(1<=Q<=100000) representing the number of the queries. The next Q lines each contains a string(the same limitation as foregoing descriptions) as the searching condition.
     
    Output
    For each query, you just output the number of the merchandises, whose names contain the search string as their substrings.
     
    Sample Input
    20 ad ae af ag ah ai aj ak al ads add ade adf adg adh adi adj adk adl aes 5 b a d ad s
     
    Sample Output
    0 20 11 11 2
     
    分析:对于每个单词,都给一个特定的标记,这样就不会在同一个单词上重复计算了,再加上一般的字典树,就可以解了
     
    代码:
     1 #include<iostream>
     2 using namespace std;
     3 struct Node
     4 {
     5     Node *next[26];
     6     int num;
     7     int flag;
     8     Node()
     9     {
    10         flag=-1;
    11         num=0;
    12         memset(next,NULL,sizeof(next));
    13     }
    14 };
    15 void insert_tree(Node *head,char *str,int flag)
    16 {
    17     Node *h=head;
    18     int len=strlen(str),i;
    19     for(i=0;i<len;i++)
    20     {
    21         int id=str[i]-'a';
    22         if(h->next[id]==NULL)
    23             h->next[id]=new Node;
    24         h=h->next[id];
    25         if(h->flag!=flag)
    26         {
    27             h->flag=flag;
    28             h->num++;
    29         }
    30     }
    31 }
    32 int find_tree(Node *head,char *str)
    33 {
    34     Node *h=head;
    35     int len=strlen(str),i;
    36     for(i=0;i<len;i++)
    37     {
    38         int id=str[i]-'a';
    39         h=h->next[id];
    40         if(h==NULL)
    41             return 0;
    42     }
    43     return h->num;
    44 }
    45 void freedom(Node *head)
    46 {
    47     int i;
    48     for(i=0;i<26;i++)
    49         if(head->next[i]!=NULL)
    50             freedom(head->next[i]);
    51     delete head;
    52 }
    53 int main()
    54 {
    55     int p,i,q;
    56     char str[21];
    57     while(~scanf("%d",&p))
    58     {
    59         getchar();
    60         Node *head=new Node;
    61         while(p--)
    62         {
    63             gets(str);
    64             int len=strlen(str);
    65             for(i=0;i<len;i++)
    66                 insert_tree(head,str+i,p);
    67         }
    68         scanf("%d",&q);
    69         getchar();
    70         while(q--)
    71         {
    72             gets(str);
    73             printf("%d\n",find_tree(head,str));
    74         }
    75         freedom(head);
    76     }
    77     return 0;
    78 }
  • 相关阅读:
    Go语言:如何解决读取不到相对路径配置文件问题
    Go组件学习:如何读取ini配置文件
    PMP学习笔记(一)
    SpringBoot安装与配置
    Homebrew中国镜像安装与配置
    Nginx日志常见时间变量解析
    openresty如何完美替换nginx
    Golang防止多个进程重复执行
    Windows 10 中CPU虚拟化已开启,但是docker无法运行
    彻底理解Python多线程中的setDaemon与join【配有GIF示意】
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2662950.html
Copyright © 2011-2022 走看看