hdu -1251 统计难题(字符串水题)

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 }
时间: 2024-08-09 19:51:44

hdu -1251 统计难题(字符串水题)的相关文章

hdu 1251 统计难题 (map水过)

# include <stdio.h> # include <algorithm> # include <string.h> # include <map> # include <iostream> using namespace std; int main() { char a; string x; map<string,int>q; while(true) { scanf("%c",&a); if(a=

[ACM] hdu 1251 统计难题 (字典树)

统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每一个提问都是一个字符串. 注意:本题仅仅有一组測试数据,处理到文件结束. Out

hdu 1251 统计难题 字典树

// hdu 1251 统计难题 字典树 // // 题目大意: // // 有一系列的单词表,以空行结尾,之后会有一些字母串,找出以这些字符串 // 作为前缀的单词的个数 // // // 解题思路: // // 字典树 Trie,在插入字符串的时候每遇到一个节点,该节点的值++.查找的时候 // 字符串时,如果找到了,那么返回当前的val,否则返回0,因为没有以这个字符串 // 为前缀的单词. // // // 感悟: // // 这段时间想学学数据结构,就看了看刘老的大白书,感觉用数组挺巧

HDU 1251 统计难题 Trie题解

基本上是标准的寻找前缀的问题,只需要insert和search函数就可以了. 我这里主要是修改一下n的记录方法,这里的n代表的不是叶子节点的标志,而是有多少单词经过了这条路径的标志. 然后是查找需要查找的前缀单词,如果没有找到,就返回0,表示没有单词以这个前缀单词为前缀,如果找到,直接返回n就是答案了.因为有n个单词经过了这条路径. 查找效率是常数. 使用静态分配空间的办法. #include <stdio.h> #include <string.h> const int MAX_

hdu 1251 统计难题 字典树水题

统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 19627    Accepted Submission(s): 8612 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的

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

统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 34909    Accepted Submission(s): 13109 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己

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

1 /******************************************************* 2 题目: 统计难题 (hdu 1251) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 4 算法: 字典树 5 提示: 这题压要用c++提交,G++会超内存 6 *******************************************************/ 7 #include<cstdio> 8

HDU 1251 统计难题 (字符串-Trie树)

统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每一个提问都是一个字符串. 注意:本题仅仅有一组測试数据,处理到文件结束. Out

HDU 1251 统计难题(字典树入门模板题 很重要)

统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 56382    Accepted Submission(s): 19709 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的