题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251

分析:

关于字典树的题目似乎都有一个普通适用性的模板,有时只是稍加改动来满足临时的要求,我的一个简单的模板

#define MAXNUM 26
//定义字典树结构体
typedef struct Trie
{bool flag;//从根到此是否为一个单词Trie *next[MAXNUM];
}Trie;
//声明一个根
Trie *root;
//初始化该根
void init()
{root = (Trie *)malloc(sizeof(Trie));root->flag=false;for(int i=0;i<MAXNUM;i++)root->next[i]=NULL;
}
//对该字典树的插入单词操作
void insert(char *word)
{Trie *tem = root;while(*word!='\0'){if(tem->next[*word-'a']==NULL){Trie *cur = (Trie *)malloc(sizeof(Trie));for(int i=0;i<MAXNUM;i++)cur->next[i]=NULL;cur->flag=false;tem->next[*word-'a']=cur;}tem = tem->next[*word-'a'];word++;}tem->flag=true;
}
//查询一个单词的操作
bool search(char *word)
{Trie *tem = root;for(int i=0;word[i]!='\0';i++){if(tem==NULL||tem->next[word[i]-'a']==NULL)return false;tem=tem->next[word[i]-'a'];}return tem->flag;
}//释放字典树内存操作,由于本题测试数据后程序自动跳出,所以这里没写释放内存函数void del(Trie *cur){    for(int i=0;i<MAXNUM;i++)    {        if(cur->next[i]!=NULL)        del(cur->next[i]);    }    free(cur);}

这道题目是一道典型的字典树题目(Trie),题目解法类似于 hdu 1247 Hat’s Words    的解法,这里搜索前缀,然后对后边用一个递归搜索就可以求出有多少字符串是以此字符串为前缀,注意本身也是自己的前缀。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXNUM 26
using namespace std;
//单词
char words[50005][12];
//定义字典树
typedef struct Trie
{bool flag;//从根到此是否为一个单词Trie *next[MAXNUM];
}Trie;Trie *root;
int count_num;void init()
{root = (Trie *)malloc(sizeof(Trie));root->flag=false;for(int i=0;i<MAXNUM;i++)root->next[i]=NULL;
}void insert(char *word)
{Trie *tem = root;while(*word!='\0'){if(tem->next[*word-'a']==NULL){Trie *cur = (Trie *)malloc(sizeof(Trie));for(int i=0;i<MAXNUM;i++)cur->next[i]=NULL;cur->flag=false;tem->next[*word-'a']=cur;}tem = tem->next[*word-'a'];word++;}tem->flag=true;
}void dfs(Trie *cur)
{if(cur->flag)count_num++;for(int i=0;i<MAXNUM;i++){if(cur->next[i]!=NULL)dfs(cur->next[i]);}
}int search(char *word)
{Trie *tem = root;count_num = 0;for(int i=0;word[i]!='\0';i++){if(tem==NULL||tem->next[word[i]-'a']==NULL)return 0;tem=tem->next[word[i]-'a'];}dfs(tem);return 1;
}int main()
{init();int t=0;char w[12];while(gets(w)){if(*w=='\0')break;insert(w);t++;}while(scanf("%s",w)!=EOF){search(w);printf("%d\n",count_num);}return 0;
}

hdu 1251 统计难题(字典树)相关推荐

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

    1 /******************************************************* 2 题目: 统计难题 (hdu 1251) 3 链接: http://acm.hd ...

  2. HDU 1251 统计难题 字典树/STL

    统计难题 Time Limit:2000MS     Memory Limit:65535KB     64bit IO Format:%I64d & %I64u Description Ig ...

  3. HDU - 1251 统计难题(字典树)

    题目链接:点击查看 题目大意:给出一些单词,后续再给出一些前缀,询问包含此前缀的单词一共有多少个 题目分析:这个题目的数据可能有点水,而且时间给的也很足,给了两秒,而且加上是hdu的,可以用无序map ...

  4. hdu -1251 统计难题(字典树水题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 建树之后 查询即可. G++提交 ME不知道为什么,c++就对了. 1 #include <iost ...

  5. hdu 1251 统计难题(trie树入门)

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

  6. hdu 1251 统计难题 (Trie树)

    本题是trie树模板题,如果不用trie而用map写可以看出trie处理这类问题有明显的时间优势. 在trie树中查找一个关键字的时间和树中包含的结点数无关,而取决于组成关键字的字符数.(对比:二叉查 ...

  7. hdu 1251统计难题

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

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

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

  9. HDU 1251 统计难题

    简单字典树  这是我初次接触字典树,代码的效率还不是很高,有什么建议,敬请指教 题目: 统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写 ...

最新文章

  1. Linux 关机命令介绍shutdown
  2. Python零碎知识(6):split 和 join
  3. oracle11g自动内存管理
  4. OOo on ready---VB篇
  5. python文件操作总结
  6. linux系统将python升级到2.7.10版本
  7. CuteEditor 5.0 的使用
  8. RabbitMQ 的安装----Linux环境
  9. makefile how to
  10. 孙鑫-MFC笔记四--文本编程
  11. a4在html中的尺寸,网页设立A4大小
  12. 动态电路中的动态元件——电容和电感
  13. linux授权文件夹777,服务器上如何修改文件夹777权限
  14. 【原创】Moon在2005的辉煌
  15. 八皇后(USACO)
  16. html代码轮播图片错位,可拖动选项卡嵌套图片轮播时图片错位的问题
  17. ElasticSearch 之 文本搜索
  18. 主机模拟i2c检测设备时出现错误死循环_适用于PLC/DCS应用,支持HART和Modbus连接的模拟I/O系统...
  19. EFCore中的主键
  20. 设计师学python有意义吗-如果你有设计师朋友,请对他好一些...

热门文章

  1. Java实现选择排序及其优化
  2. #define宏定义中的#,##,@#,\
  3. c# mysql timeout expired_C#百万数据查询出现超时问题的解决方法
  4. pythonsubprocess执行多条shell命令_python中subprocess批量执行linux命令
  5. 50万数据生成6位数不重复字符串_JAVA技术分享:单号的生成
  6. 第三届山东大数据-威海赛区-民宿空置预测-排行榜
  7. 操作系统设计与实现第3版笔记与minix3心得(1)-minix3简介
  8. rust(29)-元组结构体
  9. 【干货】求之不得的 Java 文档教程大汇总!
  10. 【机器学习基础】朴素贝叶斯的算法实现