字典树模板
2015-09-06 18:34
344 查看
字典树
这一个比较简单 现在回顾也挺简单
直接给出模板 有什么不会的直接去模板里理解
View Code
这一个比较简单 现在回顾也挺简单
直接给出模板 有什么不会的直接去模板里理解
#include <iostream> #include <cstdio> #include <cstring> using namespace std; struct Node { Node *next[26]; int cnt; Node() { for(int i = 0; i < 26; i++) { next[i] = NULL; } cnt = 0; } }root; void add(char *s) { Node *p = &root; for(int i = 0; s[i]; i++) { if(p -> next[s[i] - 'a'] == NULL) { p -> next[s[i] - 'a'] = new Node(); } p = p -> next[s[i] - 'a']; p -> cnt++; } } int cal(char *s) { Node *p = &root; for(int i = 0; s[i]; i++) { if(p -> next[s[i] - 'a'] == NULL) { return 0; } p = p -> next[s[i] - 'a']; } return p -> cnt; } void Free(Node *p) { for(int i = 0; i < 26; i++) { if(p -> next[i] != NULL) { Free(p -> next[i]); } } free(p); } char s[15]; int main() { while(gets(s)) { if(strlen(s) == 0) { break; } add(s); } while(EOF != scanf("%s",s)) { printf("%d\n",cal(s)); } Node *p = &root; // free(p); }
View Code
相关文章推荐
- iOS音频播放 (二):AudioSession
- fir.im Weekly - 每个程序员都应当拥有的技能树
- pink panther and pals(顽皮豹)
- LeetCode(59)SPiral Matrix II
- 华为时间管理法
- 更新日志 - fir.im 回归,上线 Android Studio 插件
- 灰度图像阈值化分割常见方法总结及VC实现
- LeetCode(59)SPiral Matrix II
- 结构体赋值及free()的问题 2010-10-27 12:42:15 http://blog.chinaunix.net/uid-21702630-id-205956.html
- UE4 编译虚幻引擎
- GitHub和SourceTree入门教程
- 微信开发学习—柳峰老师博客之目录总结
- AD采样问题总结
- hadoop 自学指南三之WordCount解析(3)
- 关于自定义布局的宽度问题
- POJ 1006 Biorhythms【中国剩余定理】
- js加日期版本,防止引用缓存
- hibernate中HQL查询count返回query取得其长度
- 重新认识二叉树
- Win7 配置 Git 客户端 图文详解