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 }
  • 相关阅读:
    T-SQL 查询数据库中各个表的使用空间
    T-SQL 拆分使用指定分隔符的字符串(split string)
    T-SQL 去除特定字段的前导0
    T-SQL 实现行转列
    T-SQL 将存储过程结果插入到表中
    T-SQL查看数据库恢复(RESTORE)时间
    Why Do We Need a Data Warehouse?
    T-SQL 常用DDL语句
    dedecms织梦笔记
    Lua 基础简明教程
  • 原文地址:https://www.cnblogs.com/crazyapple/p/2662950.html
Copyright © 2011-2022 走看看