首页 > 其他 > 详细

hdu 1251 统计难题 (字典树入门题)

时间:2016-08-13 20:55:18      阅读:236      评论:0      收藏:0      [点我收藏+]
 1 /*******************************************************
 2 题目:  统计难题 (hdu 1251)
 3 链接:   http://acm.hdu.edu.cn/showproblem.php?pid=1251
 4 算法:  字典树
 5 提示:   这题压要用c++提交,G++会超内存
 6 *******************************************************/
 7 #include<cstdio>
 8 #include<cstring>
 9 #include<algorithm>
10 #include<iostream>
11 using namespace std;
12 char s[11];
13 typedef struct Node       
14 {
15     Node *next[26];
16     int cut;
17 }Node;
18 Node *root;
19 void inser(char *s)
20 {
21     Node *p=root;
22     for (int i=0;s[i];i++)
23     {
24         int x=s[i]-a;
25         if (p->next[x]==NULL)
26         {
27             p->next[x]=(Node *)malloc(sizeof(Node));
28             p->next[x]->cut=0;
29             for (int i=0;i<26;i++) p->next[x]->next[i]=NULL;
30         }
31         p=p->next[x];
32         p->cut++;
33     }
34 }
35 int Find(char *s)
36 {
37     Node *p=root;
38     for (int i=0;s[i];i++)
39     {
40         int x=s[i]-a;
41         if (p->next[x]==NULL) return 0;
42         p=p->next[x];
43     }
44     return p->cut;
45 }
46 int main()
47 {
48     root=new Node();
49     while (gets(s))
50     {
51         if (strcmp(s,"")==0) break;
52         else inser(s);
53     }
54     while (gets(s))
55     {
56         printf("%d\n",Find(s));
57     }
58     return 0;
59 }

 

hdu 1251 统计难题 (字典树入门题)

原文:http://www.cnblogs.com/pblr/p/5768729.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!