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

建树之后 查询即可.

G++提交 ME不知道为什么,c++就对了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <vector>
 5 #include <cstring>
 6 #include <string>
 7 #include <algorithm>
 8 #include <string>
 9 #include <set>
10 #include <functional>
11 #include <numeric>
12 #include <sstream>
13 #include <stack>
14 #include <map>
15 #include <queue>
16 #include <deque>
17 //#pragma comment(linker, "/STACK:102400000,102400000")
18 #define CL(arr, val)    memset(arr, val, sizeof(arr))
19
20 #define ll long long
21 #define INF 0x7f7f7f7f
22 #define lc l,m,rt<<1
23 #define rc m + 1,r,rt<<1|1
24 #define pi acos(-1.0)
25
26 #define L(x)    (x) << 1
27 #define R(x)    (x) << 1 | 1
28 #define MID(l, r)   (l + r) >> 1
29 #define Min(x, y)   (x) < (y) ? (x) : (y)
30 #define Max(x, y)   (x) < (y) ? (y) : (x)
31 #define E(x)        (1 << (x))
32 #define iabs(x)     (x) < 0 ? -(x) : (x)
33 #define OUT(x)  printf("%I64d\n", x)
34 #define lowbit(x)   (x)&(-x)
35 #define Read()  freopen("a.txt", "r", stdin)
36 #define Write() freopen("b.txt", "w", stdout);
37 #define maxn 310
38 #define maxv 50010
39 #define mod 1000000000
40 using namespace std;
41
42 typedef struct node
43 {
44     int count;
45     struct node *next[26];
46 }*tree;
47
48 void insert(tree p,char *s)
49 {
50     tree h=p;
51     int l=strlen(s);
52     for(int i=0;i<l;i++)
53     {
54         int index=s[i]-'a';
55         if(h->next[index]!=NULL)
56         {
57             h=h->next[index];
58             h->count++;
59         }
60         else
61         {
62             tree tem=(tree)calloc(1,sizeof(node));
63             tem->count=1;
64             h->next[index]=tem;
65             h=tem;
66         }
67     }
68 }
69
70 int find(tree p,char *s)
71 {
72     tree h=p;
73     int l=strlen(s);
74     for(int i=0;i<l;i++)
75     {
76         int index=s[i]-'a';
77         if(h->next[index]==NULL) return 0;
78         h=h->next[index];
79     }
80     return h->count;
81 }
82 int main()
83 {
84     //Read();
85     char s[15];
86     tree head=(tree)calloc(1,sizeof(node));
87     while(gets(s)&&s[0])
88     {
89         insert(head,s);
90         //printf("%s\n",s);
91     }
92     //printf("1\n");
93     while(~scanf("%s",s))
94     {
95         printf("%d\n",find(head,s));
96     }
97     free(head);
98     return 0;
99 }

转载于:https://www.cnblogs.com/nowandforever/p/4572162.html

hdu -1251 统计难题(字典树水题)相关推荐

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

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

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

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

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

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

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

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

  5. hdu 1251 统计难题(trie树入门)

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

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

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

  7. hdu1305 字典树水题

    题意:      给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路:       字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个 ...

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

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

  9. hdu 1251统计难题

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

最新文章

  1. 三代组装软件canu学习笔记
  2. 【c语言】蓝桥杯算法提高 一元一次方程
  3. Brilliant Programmers Show
  4. pytorch中torch.manual_seed()的理解
  5. React之state总结
  6. Android studio 设置默认打开项目,默认打开项目方式
  7. php+memcached缓存技术实例
  8. 区域增长 matlab,图像分割 区域增长
  9. Kali渗透测试:使用Word宏病毒进行渗透攻击
  10. 碰撞检测经典解决方案
  11. 访问服务器显示我被拒绝,连接到服务器localhost:8080被拒绝(The connection to the server localhost:8080 was refused)...
  12. SSM框架实现的垃圾分类管理系统+文档
  13. IPO (Python)
  14. 百度地图API实现 地址经纬度 互相转换
  15. Gzip Zlib PNG 压缩算法【转】
  16. java基础学习笔记——system
  17. 马斯克为房地产代言“献身”,洗泡泡浴还被五花大绑,看完广告我瞎了
  18. Jetson WIFI 驱动安装(intel ax200* or inte 9260* 无线模块)
  19. Win系统 - 怎么在电脑上玩抖音,抖音电脑版怎么玩?
  20. Qt中setData()和data()用法

热门文章

  1. php5.6 mysql被重置_php5.6连接mysql8出现错误解决方法
  2. 二等水准数据平差_自编单次二等水准平差计算表
  3. c+字符串数组_了解C ++字符串数组
  4. Java字符串equals()
  5. primefaces教程_Primefaces日历组件示例教程
  6. 非基元类型数据结构_Java数据类型–基元和二进制文字
  7. scala 构造_Scala咖喱和自动类型依赖的封闭构造
  8. 学以致用六---Centos7.2+python3.6.2+django2.1.1 --搭建一个网站
  9. 3.5框架不能创建就加这一段代码
  10. 【Ubuntu】在Ubuntu 12.04 LTS上安装JDK6