hdu 1251:统计难题(字典树,经典题)
统计难题
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)
Total Submission(s): 16905 Accepted Submission(s): 7273
注意:本题只有一组测试数据,处理到文件结束.
#include <iostream>
#include <string.h>
using namespace std; struct Trie{ //字典树定义
Trie* next[];
int num; //以当前字符串为前缀的单词的数量
Trie() //构造函数
{
int i;
for(i=;i<;i++){
next[i] = NULL;
}
num=;
}
};
Trie root;
void Insert(char word[]) //将字符串word插入到字典树中
{
Trie *p = &root;
int i;
for(i=;word[i];i++){ //遍历word的每一个字符
if(p->next[word[i]-'a']==NULL) //如果该字符没有对应的节点
p->next[word[i]-'a'] = new Trie; //创建一个
p = p->next[word[i]-'a'];
p->num++;
}
}
int Find(char word[]) //返回以字符串word为前缀的单词的数量
{
Trie *p = &root;
int i;
for(i=;word[i];i++){ //在字典树找到该单词的结尾位置
if(p->next[word[i]-'a']==NULL)
return ;
p = p->next[word[i]-'a'];
}
return p->num;
} int main()
{
char word[];
while(cin.getline(word,)){ //输入单词
if(strlen(word)== || word[]==' ') //如果读入字符串的长度为0或者是空格,说明读入的是空行
break;
Insert(word);
}
while(scanf("%s",word)!=EOF){
printf("%d\n",Find(word)); //返回word为前缀的单词的数量
}
return ;
}
代码二(数组):
#include <iostream>
#include <stdio.h>
using namespace std; int trie[][]; //数组形式定义字典树,值存储的是下一个字符的位置
int num[]={}; //附加值,以某一字符串为前缀的单词的数量
int pos = ; void Insert(char word[]) //在字典树中插入某个单词
{
int i;
int c = ;
for(i=;word[i];i++){
int n = word[i]-'a';
if(trie[c][n]==) //如果对应字符还没有值
trie[c][n] = pos++;
c = trie[c][n];
num[c]++;
}
}
int Find(char word[]) //返回以某个字符串为前缀的单词的数量
{
int i;
int c = ;
for(i=;word[i];i++){
int n = word[i]-'a';
if(trie[c][n]==)
return ;
c = trie[c][n];
}
return num[c];
} int main()
{
char word[];
while(gets(word)){
if(word[]==NULL) //空行。gets读入的回车符会自动转换为NULL。
break;
Insert(word);
}
while(gets(word))
printf("%d\n",Find(word));
return ;
}
SUM:
以下是2种做法的效率对比:

第二行的是第一种做法,第一行的是第二种做法,很明显,数组做法不管是在时间,空间,代码量上都要优于链表做法。
但是数组的不太好理解,如何取舍看各位看官的意思吧。
Freecode : www.cnblogs.com/yym2013
hdu 1251:统计难题(字典树,经典题)的更多相关文章
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- hdu 1251 统计难题 字典树第一题。
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 1251 统计难题 字典树大水题
今天刚看的字典树, 就RE了一发, 字典树原理还是很简单的, 唯一的问题就是不知道一维够不够用, 就开的贼大, 这真的是容易MLE的东西啊, 赶紧去学优化吧. HDU-1251 统计难题 这道题唯一的 ...
- hdu -1251 统计难题(字典树水题)
http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. #include <iostre ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- HDU 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- HDU 1251统计难题 字典树
字典树的应用. 数据结构第一次课的作业竟然就需要用到树了!!!这不科学啊.赶紧来熟悉一下字典树. 空间开销太大T T #include<cstdio> #include<cstrin ...
- HDU 1251 统计难题(Trie模版题)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
随机推荐
- BZOJ 4580: [Usaco2016 Open]248
Description 一个序列,每次可以把相邻的两个数合为一个,价值+1,求最后的最大价值. Sol 区间DP. \(f[i][j]\) 表示 \(i-j\) 中合成一个数字为多少,转移就是枚举断点 ...
- windows下nodejs常见错误
1.express-session express-session deprecated undefined resave option; provide resave option auth_s e ...
- python self introspection
http://www.ibm.com/developerworks/cn/linux/l-pyint/index1.html
- java前后台之间传值的几种方式
自己写的代码太少,有时候前后台传值还写的不是很熟练,现在总结一下,加深下印象. 1.jquery的Ajax传值 ---->前台到后台 期望功能:把前台用户输入的信息保存在数据库里. 前台jsp代 ...
- 【GoLang】panic defer recover 深入理解
唉,只能说C程序员可以接受go的错误设计,相比java来说这个设计真的很差劲! 我认为知乎上说的比较中肯的: 1. The key lesson, however, is that errors ar ...
- JavaScript——Prototype详探
用过JavaScript的同学们肯定都对prototype如雷贯耳,但是这究竟是个什么东西却让初学者莫衷一是,只知道函数都会有一个prototype属性,可以为其添加函数供实例访问,其它的就不清楚了, ...
- Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- vs2008与windbg的使用
WinDbg是微软发布的一款相当优秀的源码级(source-level)调试工具,可以用于Kernel模式调试和用户模式调试,还可以调试Dump文件.使用windgb进行程序调试是一个很有必要的技能. ...
- cxGRID中的字段怎么能以0.00的格式显示
CXGRID中的字段如何能以0.00的格式显示在CXGRID中如何让字段能以0.00的格式显示,我的字段是FLOAT类型,满意的马上给分! ------解决方案-------------------- ...
- ffmpeg-20160512-git-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...