Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 14434 Accepted Submission(s): 6219
Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束.
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
Sample Input
banana
band
bee
absolute
acm
ba
b
band
abc
Sample Output
2
3
1
0
Author
Ignatius.L
Recommend
Ignatius.L
字典树.....
代码:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4
5 typedef struct trie
6 {
7 //由于只有小写字母a~z-->26
8 struct trie *child[26];
9 int deep; //--->相似的程度
10 }Trie;
11
12 /*作为一个头指针*/
13
14 Trie *root;
15
16 void Insert(char *a)
17 {
18 int len,i;
19 Trie *current, *creatnew;
20 len=strlen(a);
21 if(len)
22 {
23 current=root;
24 for(i=0;i<len;i++)
25 {
26 if(current->child[a[i]-'a']!=0)
27 {
28 current=current->child[a[i]-'a'];
29 current->deep=current->deep+1;
30 }
31 else
32 {
33 creatnew=(Trie *)malloc(sizeof(Trie));
34 for(int j=0;j<26;j++)
35 {
36 creatnew->child[j]=0;
37 }
38 current->child[a[i]-'a']=creatnew;
39 current=creatnew;
40 current->deep=1;
41 }
42 }
43 }
44 }
45
46
47 int Triefind(char *a)
48 {
49 int i,len;
50 Trie *current;
51 len=strlen(a);
52 if(!len) return 0;
53 current=root;
54 for(i=0;i<len;i++)
55 {
56 if(current->child[a[i]-'a']!=0)
57 {
58 current=current->child[a[i]-'a'];
59 }
60 else
61 return 0;
62 }
63 return current->deep;
64 }
65
66 int main()
67 {
68 int i;
69 char temp[12];
70 root=(Trie *)malloc(sizeof(Trie));
71 for(i=0;i<26;i++)
72 {
73 root->child[i]=0;
74 }
75 root->deep=2;
76 while(gets(temp),strcmp(temp,""))
77 Insert(temp);
78 memset(temp,'\0',sizeof(temp));
79 while(~scanf("%s",temp))
80 printf("%d\n",Triefind(temp));
81 free(root);
82 return 0;
83 }
84
85
模板:
代码:
1 /*hdu 1251 字典树之统计*/
2 #define LOCAL
3 #include<cstdio>
4 #include<cstring>
5 #include<cstdlib>
6 typedef struct Trie
7 {
8 struct Trie *child[26];
9 int deep;
10 }trie;
11
12 int idx(char s){
13 return s-'a';
14 }
15 void Insert(char *s,trie *root)
16 {
17 trie *cur,*newcur;
18 cur=root;
19 int i,j;
20 for(i=0;s[i]!='\0';i++)
21 {
22 if(cur->child[idx(s[i])]==NULL) //为空指针
23 {
24 newcur=(trie *)malloc(sizeof(trie));
25 newcur->deep=0;
26 for(j=0;j<26;j++)
27 newcur->child[j]=NULL; //设置为空指针
28 cur->child[idx(s[i])]=newcur;
29 }
30 cur=cur->child[idx(s[i])]; //向下推一层
31 cur->deep+=1; //层数加一
32 }
33 }
34 int query(char *s,trie *root)
35 {
36 trie *cur=root;
37 int i;
38 for(i=0;s[i]!='\0';i++){
39 if(cur->child[idx(s[i])]!=NULL)
40 cur=cur->child[idx(s[i])];
41 else
42 return 0;
43 }
44 int tem=cur->deep;
45 return tem;
46 }
47 void del(trie *root)
48 {
49 int i=0;
50 for(i=0;i<26;i++){
51 if(root->child[i]!=NULL)
52 del(root->child[i]);
53 }
54 free(root);
55 }
56 char ss[12];
57 int main()
58 {
59 #ifdef LOCAL
60 freopen("test.in","r",stdin);
61 #endif
62 trie *root=(trie *)malloc(sizeof(trie));
63 for(int i=0;i<26;i++)
64 root->child[i]=NULL; //设置为空指针
65 while(gets(ss),strcmp(ss,""))
66 Insert(ss,root);
67 while(scanf("%s",ss)!=EOF)
68 printf("%d\n",query(ss,root));
69 del(root);
70 return 0;
71 }