Hiho+Trie数求字符串前缀的典型模板
2016-05-02 22:03
375 查看
点击打开链接
#include<stdio.h> #include<stdlib.h> #include<iostream> #include<string.h> #include<cstring> #include<string> #include<math.h> #include<algorithm> #define LL long long #define inf 0x3f3f3f3f #define mod 1e9+7 using namespace std; struct Tire { int num; Tire* next[26]; Tire(){ num=0; int i; for(i=0; i<26; i++) next[i] = NULL; } } tree; void Insert(char word[]) { Tire* p = &tree; int i; for(i=0; word[i]; i++){ int t = word[i]-'a'; if(p->next[t]==NULL) p->next[t] = new Tire; p = p->next[t]; p->num++; } } int Find(char word[]) { Tire* p = &tree; int i; for(i=0; word[i]; i++){ int t = word[i]-'a'; if(p->next[t]==NULL) return 0; p = p->next[t]; } return p->num; } int main() { int n; char word[11]; ///字典 scanf("%d",&n); gets(word); while(n--){ ///读取单词n次 gets(word); Insert(word); ///将单词插入到字典树中 } ///询问 scanf("%d",&n); gets(word); ///将回车读入 while(n--){ gets(word); printf("%d\n",Find(word)); ///查询以word为前缀的单词出现过几次 } return 0; }
相关文章推荐
- 六、树和二叉树--(1)什么是二叉树
- 第一次盲打,感觉好难啊,~~~~(>_<)~~~~
- linux下解压命令大全
- ./configure,make,make install的作用
- 剑指offer:孩子们的游戏(圆圈中最后剩下的数)
- 计算机科学只存在两个难题:缓存失效和命名
- 20145110 《Java程序设计》第四次实验报告
- Java中获取路径的方法_自我分析
- POJ 1751 Highways
- 【Unity】11.5 物理材质 (Physics Material)
- Immutable 在 JavaScript 中的应用
- Mybatis获取插入记录的自增长ID
- 解决Android Studio倒入项目或者打开项目卡死
- 在Linux中安装dnw
- 【Unity】11.4 车轮碰撞体(Wheel Collider)
- 【iCore3双核心板】iCore3双核心板使用说明(图文)
- BZoj 1016: [JSOI2008]最小生成树计数【最小生成树】
- 深入 Docker:容器和镜像
- 面向接口可扩展框架
- Hadoop HDFS概念学习系列之fs中相关API接口的说明(二十一)