统计难题

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 24521    Accepted Submission(s): 10133

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

坑坑:用G++在杭电oj上提交会一直内存超限~

 1 #include<iostream>
 2 #include<vector>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <math.h>
 7 #include<algorithm>
 8 #define ll long long
 9 #define eps 1e-8
10 using namespace std;
11
12 struct nodes
13 {
14     int cnt;
15     struct nodes *next[26];
16     nodes()
17     {
18         int i;
19         cnt = 0;
20         for(i = 0; i < 26; i++)
21             next[i] = NULL;
22     }
23 } root,*temp;
24
25 void inserts(char *word)
26 {
27     nodes *cur = &root;
28     while(*word )
29     {
30         int t = *word - 'a';
31         if(cur->next[t] == NULL)
32         {
33             temp = (nodes *)malloc(sizeof(nodes));
34             temp->cnt = 0;
35             for(int i = 0; i < 26; i++)
36                 temp->next[i] = NULL;
37             cur->next[t] = temp;
38         }
39         cur = cur->next[t];
40         cur->cnt++;
41         word++;
42     }
43 }
44
45 void searchs(char *word)
46 {
47     nodes *cur = &root;
48     int ans = 0;
49     while(*word && cur)
50     {
51         cur = cur->next[*word - 'a'];
52         if(cur)
53             ans = cur->cnt;
54         else
55         {
56             ans = 0;
57             break;
58         }
59         word++;
60     }
61     printf("%d\n",ans);
62 }
63
64 int main(void)
65 {
66     char bank[22];
67     char ss[50];
68
69     while(gets(bank) && bank[0] )
70     {
71         inserts(bank);
72     }
73     while(scanf("%s",ss) != -1)
74     {
75         searchs(ss);
76     }
77     return 0;
78 }

新增省内存方法,STL中的map:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cmath>
 7 #include <map>
 8 #include <algorithm>
 9 #define N 500015
10 #define INF 1000000
11 #define ll long long
12 using namespace std;
13
14 int main(void)
15 {
16     map<string,int>Q;
17     char temp[15];
18     int l;
19     while(gets(temp) && temp[0])
20     {
21         l = (int)strlen(temp);
22         for(int i = l; i > 0; i--)
23         {
24             temp[i] = '\0';
25             Q[temp]++;
26         }
27     }
28     while(scanf("%s",temp) != -1)
29     {
30         printf("%d\n",Q[temp]);
31     }
32     return 0;
33 }

转载于:https://www.cnblogs.com/henserlinda/p/4721169.html

hdu 1251 统计难题(trie树入门)相关推荐

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

    1 /******************************************************* 2 题目: 统计难题 (hdu 1251) 3 链接: http://acm.hd ...

  2. hdu 1251 统计难题 (Trie树)

    本题是trie树模板题,如果不用trie而用map写可以看出trie处理这类问题有明显的时间优势. 在trie树中查找一个关键字的时间和树中包含的结点数无关,而取决于组成关键字的字符数.(对比:二叉查 ...

  3. HDU 1251 统计难题 字典树/STL

    统计难题 Time Limit:2000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Description Ig ...

  4. HDU - 1251 统计难题(字典树)

    题目链接:点击查看 题目大意:给出一些单词,后续再给出一些前缀,询问包含此前缀的单词一共有多少个 题目分析:这个题目的数据可能有点水,而且时间给的也很足,给了两秒,而且加上是hdu的,可以用无序map ...

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

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. 1 #include <iost ...

  6. HDU 1251 统计难题(Trie模版题)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  7. hdu 1251统计难题

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  8. HDU 1251 统计难题

    简单字典树  这是我初次接触字典树,代码的效率还不是很高,有什么建议,敬请指教 题目: 统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写 ...

  9. hdu 1251 统计难题(字典树)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 分析: 关于字典树的题目似乎都有一个普通适用性的模板,有时只是稍加改动来满足临时的要求,我的一 ...

最新文章

  1. mysql keepalived低版本_Mysql+keepalived主主切换
  2. hdu4974 简单题
  3. BLE进阶——链路层(1)
  4. 树形结构:使用栈实现,快排,先序遍历,归并排序,后序遍历
  5. Java并发与锁设计实现详述 - Java中的Condition
  6. mycli一个非常有趣的bug
  7. python学习第25天
  8. 两种方法清空memcache
  9. 用户研究三部曲:有关用户研究的战略思考
  10. 面试官:你的缺点是什么?这样回答漂亮!
  11. 搭建公司内部的NuGet服务器
  12. prcs6汉化补丁怎么替换_美少女万华镜5汉化版百度云下载-美少女万华镜5汉化版百度网盘下载-附全cg...
  13. 从Technorati看博客搜索的发展
  14. RKH81 键盘快捷键
  15. 牛津花卉数据集(Oxford 17/Oxford 102)官网
  16. springboot疑难杂症
  17. python期末考试重点_Python期末复习笔记
  18. 小程序 zoom_Zoom是否真正监视您在通话中使用的应用程序?
  19. 映美Jolimark CFP-535G 打印机驱动
  20. 在Windows10上通过Virtualbox安装Ubuntu操作系统教程

热门文章

  1. data stucture at the xuetang x
  2. python script 95% interval
  3. 2018 blockchain innovation final round of the chain valley
  4. 2021的第一封拒信来自2021年年度青年活动家本科生奖!
  5. 如果要和外国人做项目,加入一个teams是第一步,就跟我们的企业微信,钉钉差不多
  6. 根据皮肤亮度来区分salmon和sea bass,这个比较好
  7. U3D 如何计算一个UI四个角的绝对坐标
  8. android唯一设备标识、设备号、设备ID的获取方法
  9. 中科点击矩阵式推进大数据落地与应用
  10. C语言指针和链表的体会