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 }
  • 相关阅读:
    java 浅显的会议预约-没有测试过
    Postgresql 与 Spring的集成
    对象转JSON正则查找替换
    Java 对象与JSONString的互相转换
    excel 中涉及到金额显示0E-8的 可以使用以下语句来把excel导出修改为0
    Java 非正则方式校验数据
    泛型与反射的使用
    正则表达式,匹配 URL 是IP还是域名
    对外接口加密
    自定义SQL查询的使用
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2662950.html
Copyright © 2011-2022 走看看