码迷,mamicode.com
首页 > 其他好文 > 详细

hdu--1251 统计难题(字典树水题)

时间:2016-11-12 22:24:48      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:测试数据   空行   sample   搜索   print   close   malloc   tor   content   

Description

Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). 

Input

输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 

注意:本题只有一组测试数据,处理到文件结束. 

Output

对于每个提问,给出以该字符串为前缀的单词的数量. 

Sample Input

banana
band
bee
absolute
acm

ba
b
band
abc

Sample Output

2
3
1
0
题意:我就不说了,你懂的^_^
思路:这是一个简单的字典树模板题
1 struct note//树的定义
2 {
3     note *next[26];//a~z是26,再加A~Z是52,再加0~9是62,
4     int v;//存储这个节点被使用的次数
5 };
 1 int ins(char *s)//更新树中的节点
 2 {
 3     note *p,*q;
 4     int len=strlen(s);
 5     if(len==0)
 6         return 0;
 7     p=root;
 8     for(int i=0;i<len;i++)//新建节点
 9     {
10         int d=s[i]-a;
11         if(p->next[d]==0)
12         {
13             q=(note *)malloc(sizeof(note));
14             q->v=1;//新建的节点最开始肯定是被使用过1次的
15             for(int j=0;j<26;j++)
16             q->next[j]=0;
17             p->next[d]=q;
18             p=q;
19         }
20         else//被使用过得节点更新
21         {
22             p=p->next[d];
23             p->v=p->v+1;
24         }
25     }
26     return 0;
27 }
 1 int sea(char *s)//搜索前缀
 2 {
 3     int len=strlen(s);
 4     if(len==0)
 5         return 0;
 6     note *p,*q;
 7     p=root;
 8     for(int i=0;i<len;i++)
 9     {
10         int d=s[i]-a;
11         p=p->next[d];
12         if(p==0)
13             return 0;
14     }
15     return p->v;
16 }

AC代码:

技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cstdlib>
 6 using namespace std;
 7 struct note//树的定义
 8 {
 9     note *next[26];//a~z是26,再加A~Z是52,再加0~9是62,
10     int v;//存储这个节点被使用的次数
11 };
12  struct note *root;
13 int ins(char *s)//更新树中的节点
14 {
15     note *p,*q;
16     int len=strlen(s);
17     if(len==0)
18         return 0;
19     p=root;
20     for(int i=0;i<len;i++)//新建节点
21     {
22         int d=s[i]-a;
23         if(p->next[d]==0)
24         {
25             q=(note *)malloc(sizeof(note));
26             q->v=1;//新建的节点最开始肯定是被使用过1次的
27             for(int j=0;j<26;j++)
28             q->next[j]=0;
29             p->next[d]=q;
30             p=q;
31         }
32         else//被使用过得节点更新
33         {
34             p=p->next[d];
35             p->v=p->v+1;
36         }
37     }
38     return 0;
39 }
40 int sea(char *s)//搜索前缀
41 {
42     int len=strlen(s);
43     if(len==0)
44         return 0;
45     note *p,*q;
46     p=root;
47     for(int i=0;i<len;i++)
48     {
49         int d=s[i]-a;
50         p=p->next[d];
51         if(p==0)
52             return 0;
53     }
54     return p->v;
55 }
56 int main()
57 {
58     root=(note *)malloc(sizeof(note));
59     for(int i=0;i<26;i++)
60         root->next[i]=0;
61     root->v=2;
62     char s[20];
63     int flag;
64     flag=0;
65     while(gets(s))
66     {
67         if(strcmp(s,"")==0)
68         {
69             break;
70         }
71         else
72             ins(s);
73     }
74     while(gets(s))
75     {
76         if(strcmp(s,"")==0)
77         {
78             break;
79         }
80         else
81         printf("%d\n",sea(s));
82     }
83     return 0;
84 }
View Code

 

hdu--1251 统计难题(字典树水题)

标签:测试数据   空行   sample   搜索   print   close   malloc   tor   content   

原文地址:http://www.cnblogs.com/wang-ya-wei/p/6057512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!