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 ...
随机推荐
- HDU 1495 非常可乐
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=103711#problem/M /*BFS简单题 链接地址: http://acm.hdu ...
- Dom初
DOM基础 •什么是DOM •浏览器支持情况 lDOM节点 •childNodes nodeType –获取子节点 <!DOCTYPE html PUBLIC "-//W3C//DT ...
- 自动化监控利器-Zabbix
转自: http://www.xuliangwei.com/xubusi/117.html 1.1为何需要监控系统 在一个IT环境中会存在各种各样的设备,例如:硬件设备.软件设备.其系统的构成也是 ...
- Split Array Largest Sum
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- 全局对象的构造函数会在main 函数之前执行
#include <iostream> using namespace std; class A { public: A() { cout << "Generator ...
- action 方法的访问
Action中的方法的访问: 访问Action的中的方法,默认情况下只能访问execute方法.那么多次请求就不能提交到一个Action.能不能一个模块的多次请求提交到一个Action中? * 需要使 ...
- 用pywinauto进行win32应用程序的测试
之前做win32应用测试时,用过很多大家耳熟成详的工具,接触pywinauto之前,对它的了解也不多,然而,随着对它了解的增多,发现它借助了python动态对象的能力,使得代码即便于书定,也便于阅读, ...
- Unity3d《Shader篇》法线贴图
效果图 贴图 法线贴图 //代码 Shader "Custom/NormalMap" { Properties { _MainTex ("Texture", 2 ...
- unity3d 特殊文件夹和脚本编译顺序
unity3d 特殊文件夹和脚本编译顺序 转自http://blog.csdn.net/u010019717/article/details/40474631 大多数情况下,您可以选择任何你喜欢的文件 ...
- 64位win系统上面tomcat6启动不了 window不能再本地计算机启动
64位的jdk装完之后,jre的bin目录下面没有client文件夹, 而tomcat6.0.20的默认配置启动在client文件夹下面. 所以打开tomcat6w,在java选项界面,取消Use d ...