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): 19902    Accepted Submission(s): 8720

Problem Description
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).
 
Input
输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串.

注意:本题只有一组测试数据,处理到文件结束.

 
Output
对于每个提问,给出以该字符串为前缀的单词的数量.
 
Sample Input
banana
band
bee
absolute
acm
 
ba
b
band
abc
 
Sample Output
2
3
1
0
 
思路:字典树
代码:

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
using namespace std; #define MAX 0x7fffffff struct node{
node* word[];
int n;
node(){
for(int i=;i<;i++) word[i]=NULL;
n=;
}
}*root; void Insert(char* s);
int Find(char* s); int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
char tmp[];
root=new node;
while(gets(tmp),strlen(tmp)){
Insert(tmp);
}
while(gets(tmp)!=NULL){
printf("%d\n",Find(tmp));
}
return ;
}
void Insert(char* s){
int len=strlen(s);
node *current=root,*new_node;
for(int i=;i<len;i++){
if(current->word[s[i]-'a']!=NULL){
current=current->word[s[i]-'a'];
current->n++;
}else{
new_node=new node;
current->word[s[i]-'a']=new_node;
current=current->word[s[i]-'a'];
}
}
}
int Find(char* s){
int len=strlen(s);
node *current=root;
for(int i=;i<len;i++){
if(current->word[s[i]-'a']!=NULL){
current=current->word[s[i]-'a'];
}else{ return ; }
}
return current->n;
}

hdoj1251-统计难题 【字典树】的更多相关文章

  1. hdoj1251 统计难题 字典树

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

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

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  3. HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

  4. hdu_1251统计难题(字典树Trie)

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

  5. hdu 1251 统计难题 字典树第一题。

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

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

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

  7. HDU-1251 统计难题,字典树或者map!

    统计难题 很久就看过这个题了,但不会~~~不会~~ 题意:给出一张单词表,然后下面有若干查询,每次给出一个单词,问单词表中是否存在以这个单词为前缀的单词,输出数量.本身也是自身的前缀.只有一组数据! ...

  8. HDU 1251 统计难题 字典树大水题

    今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧. HDU-1251 统计难题 这道题唯一的 ...

  9. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

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

  10. hdoj 1251 统计难题(字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 思路分析:该问题要求求出以某个字符串为前缀的单词数目,通过使用字典树,在字典树中添加count记 ...

随机推荐

  1. ES之六:ElasticSearch中Filter和Query的异同

    如下例子,查找性别是女,所在的州是PA,过滤条件是年龄是39岁,balance大于等于10000的文档: { "query": { "bool": { &quo ...

  2. 学习笔记之FluentAssertions

    dotnet/src/MoqSample at master · haotang923/dotnet · GitHub https://github.com/htanghtang/dotnet/tre ...

  3. 跨域获取json数据

    原文地址:http://my.oschina.net/LinBandit/blog/34570   前阵子做了一个前端动态加载json数据的应用,其中使用xmlhttprequest动态加载js,但是 ...

  4. 阿里云内网和公网NTP服务器和其他互联网基础服务时间同步服务器

    阿里云为云服务器ECS提供了内网NTP服务器,对于阿里云以外的设备,阿里云同时提供了 公网NTP服务器,供互联网上的设备使用. 内网和公网NTP服务器 以下为阿里云提供的内网和公网NTP服务器列表. ...

  5. 从jar包还原出java源码(项目文件)

    原文转载至:https://blog.csdn.net/mxmxz/article/details/73043156 上周接到个新任务,一个遗留的接口工程需要改造,然而根据前任开发留下的文档看,这个工 ...

  6. Source引擎多人模式网络同步模型

    转自:http://gad.qq.com/program/translateview/7168875 Source引擎的多人游戏使用基于UDP通信的C/S架构.游戏以服务器逻辑作为世界权威,客户端和服 ...

  7. ORA-03113:通信通道的文件结尾

    问题: 用命令startup启动实例时,报错“ORA-03113:通信通道的文件结尾”. 解决: SQL> startup mount ORACLE 例程已经启动. Total System G ...

  8. Log4net详细说明(全)

    转自:http://www.cnblogs.com/zhangchenliang/p/4546352.htmlhttp://www.cnblogs.com/zhangchenliang/p/45463 ...

  9. Oracle数据库的一些视图

    1.之前一直是SQL使用,没有深入学习数据库的底层知识,尤其使其进程以及锁以及底层的数据如何存储的这些知识. 2.渐渐的在开发中对数据库的要求慢慢高了 比如:临时表 -----BI里面,存储过程里面 ...

  10. Select算法(最坏复杂度O(n))

    #include<iostream> #include <stdio.h> #include <stdlib.h> #include <algorithm&g ...