hdu1251 统计难题 字典树
2015-10-18 11:02
561 查看
字典树模板题
#include<iostream> #include<cstdio> using namespace std; struct point { point* next[26]; int cot; //count how many prefixes from root to current point. point() //constructor method, initialize 26 child of pointer { cot = 0; memset(next,0,sizeof(next)); } }; void insert(point *rt,char *s) { point p = rt; int i=0; int k; while(s[i]) { k = (s[i++]-'a'); if(p->next[k]==NULL) { p->next[k] = new point(); } p->next[k]->cot++; p = p->next; } return ; } int search(point *rt,char *s) { point p = rt; int i = 0; int k; while(s[i]) { k = s[i++]-'a'; if(p->next[k]==NULL) //the trie tree doesn't have the prefix { return 0; } } return p->next[k]->cot; } int main() { point *rt = new point(); char s[25]; while(gets(s)!=NULL) { if(s[0]=='\0') break; cout<<s<<endl; insert(rt,s); } while(cin>>s) { cout<<search(rt,s)<<endl; } return 0; }注意 要用c++交,不然MLE,不知道为什么
相关文章推荐
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- C#数据结构揭秘一
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 数据结构之Treap详解
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总