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 }

 

转载于:https://www.cnblogs.com/crazyapple/archive/2012/08/29/2662950.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值