HDU——T 1251 统计难题

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

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


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   |   We have carefully selected several similar problems for you:  1075 1247 1671 1298 1800 
 
初学trie、、建树+查找
 1 #include <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4 
 5 using namespace std;
 6 
 7 int tot;
 8 char s[11];
 9 struct Trie
10 {
11     int next[27];
12     int sum;
13 }tr[3000000];
14 inline void Trie_build()
15 {
16     int now=0,len=strlen(s);
17     for(int x,i=0;i<len;i++)
18     {
19         x=s[i]-'a';
20         if(tr[now].next[x])
21             now=tr[now].next[x],tr[now].sum++;
22         else
23         {
24             tr[now].next[x]=++tot;
25             now=tot; tr[now].sum++;
26         }
27     }
28 }
29 inline int Trie_find()
30 {
31     int len=strlen(s);
32     int now=0,p=0;
33     for(;p<len;)
34         if(tr[now].next[s[p]-'a'])
35             now=tr[now].next[s[p]-'a'],p++;
36         else return 0;
37     return tr[now].sum;
38 }
39 
40 int main()
41 {
42     for(;gets(s)&&strlen(s);)
43     {
44         Trie_build();
45         memset(s,0,sizeof(s));
46     }
47     for(;gets(s)&&strlen(s);)
48     {
49         printf("%d
",Trie_find());
50         memset(s,0,sizeof(s));
51     }
52     return 0;
53 }
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/7398706.html