链接:

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

 

 在自己敲了一遍后终于懂了,这不就用了链表的知识来建立了树,对!就是这样的,然后再查找

代码:

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<stdlib.h>
 4 using namespace std;
 5 
 6 struct node
 7 {
 8     int sum;
 9     node *next[26];
10 };
11 
12 void BuildLibTree(node *head, char s[])
13 {
14     node *p = head;
15 
16     for(int i=0; s[i]; i++)
17     {
18         int k = s[i]-'a';
19 
20         if(p->next[k] == 0)
21             p->next[k] = new node();
22         p = p->next[k];
23         p->sum++;
24     }
25 }
26 int Query(node *head, char s[])
27 {
28     node *p = head;
29 
30     for(int i=0; s[i]; i++)
31     {
32         int k = s[i]-'a';
33 
34         if(p->next[k] == 0)
35             return 0;
36         p = p->next[k];
37     }
38 
39     return p->sum;
40 }
41 
42 int main()
43 {
44     char s[20];
45     node *head = new node();
46 
47     while(gets(s), s[0])
48         BuildLibTree(head, s);
49     while(scanf("%s", s) != EOF)
50         printf("%d\n", Query(head, s));
51 
52     return 0;
53 }

 

posted on 2015-07-30 14:16  栀蓝  阅读(141)  评论(0编辑  收藏  举报

levels of contents