hdu 2846 Repository

http://acm.hdu.edu.cn/showproblem.php?pid=2846

Repository

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2720    Accepted Submission(s): 1060


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
 
分析:
 
典型的字典树问题。
 
AC代码:
 
 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string.h>
 4 
 5 using namespace std;
 6 
 7 struct node
 8 {
 9     int count;
10     int value;
11     node *next[26];
12     node()
13     {
14         for(int i=0;i<26;i++)
15             next[i]=0;
16         count=0;
17         value=0;
18     }
19 };
20 
21 void insert(node *&root,char *s,int num)
22 {
23     int i=0,t=0;
24     node *p=root;
25     if(!p)
26     {
27         p=new node();
28         root=p;
29     }
30     while(s[i])
31     {
32         t=s[i]-'a';
33         if(!p->next[t])
34             p->next[t]=new node();
35         p=p->next[t];
36         if(p->value!=num)
37         {
38             p->count++;
39             p->value=num;
40         }
41     //    printf("%d\n",p->value);
42         i++;
43     }
44 }
45 
46 int find(node *root,char *s)
47 {
48     int i=0,t=0,ans;
49     node *p=root;
50     if(!p)
51         return 0;
52     while(s[i])
53     {
54         t=s[i]-'a';
55         if(!p->next[t])
56             return 0;
57         p=p->next[t];
58         ans=p->count;
59         i++;
60     }
61     return ans;
62 }
63 
64 int main()
65 {
66     int T;
67     scanf("%d",&T);
68     char str[21];
69     node *root=NULL;
70     while(T--)
71     {
72         scanf("%s",str);
73         for(int i=0;i<strlen(str);i++)
74             insert(root,str+i,T+1);
75     }
76     int Q;
77     scanf("%d",&Q);
78     while(Q--)
79     {
80         scanf("%s",str);
81         printf("%d\n",find(root,str));
82     }
83     return 0;
84 }
View Code

 

 

转载于:https://www.cnblogs.com/jeff-wgc/p/4449599.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值