Tire树(字典树、前缀树)的简介和构造
来源:互联网 发布:火炬之光2mac 编辑:程序博客网 时间:2024/06/08 07:51
先留着填个坑,免得哪天忘记写了。。。
先把代码写下,注释以后再补,现在在等offer心焦中
namespace CjpSTL{ class Trie { struct Node; using NodePtr = Node*; struct Node { unordered_map<char, NodePtr> dict; char ch; bool hasVal; int count; Node(char _ch) :ch(_ch), dict(), count(1), hasVal(false) {} }; public: Trie(); void add(const string& str) const { auto cur = root; for (auto ch : str) { auto des = cur->dict.find(ch); if (des != cur->dict.end()) { ++cur->dict[ch]->count; cur = cur->dict[ch]; } else { cur->dict[ch] = new Node(ch); cur = cur->dict[ch]; } } cur->hasVal = true; } bool find(const string& str)const { auto cur = root; for (auto ch : str) { auto des = cur->dict.find(ch); if (des != cur->dict.end()) { cur = cur->dict[ch]; ++cur->count; } else { return false; } } return cur->hasVal; } void erase(const string& str)const { auto cur = root; auto pre = root; for (auto ch : str) { auto des = cur->dict.find(ch); if (des != cur->dict.end()) { pre = cur; cur = cur->dict[ch]; --cur->count; } else { break; } } cur->hasVal = false; } ~Trie(); private: NodePtr root = new Node('\0'); };}
阅读全文
0 0
- Tire树(字典树、前缀树)的简介和构造
- Tire-Tree(前缀树,字典树)
- Hihocoder1014 Tire前缀树 很好的模板
- 字典树(Tire树)
- Tire字典树
- Tire字典树(zz)
- Tire-字典树
- 【Tire 求字典出现的前缀个数】hihocoder 1014 Trie树
- HDU1251_统计难题_字典树(tire)查前缀含词条数
- POJ2001 -- 字典树和字符串的前缀
- Tire树(字典树)的基本操作
- hdu 1671 字典树问题 TIRE树
- 字典树(Tire树)模板+例题
- 字典树(Tire树)模板 & 例题
- HDU-1251(字典树<Tire>入门)
- 水题不水之字典树(Tire tree)
- tire树~!
- TIRE树
- 标准C语言第七天
- 计蒜客 热爱工作的蒜蒜 spfa+dfs
- Unity3D学习笔记1-角色控制
- 参数更新
- 机器学习该怎么入门
- Tire树(字典树、前缀树)的简介和构造
- LeetCode 69: Sqrt(x)
- 用C语言实现布丰投针计算圆周率问题
- UVa10816
- 标准C语言第八天
- EasyUI动态显示后台数据库中的数据
- ubuntu16.04安装opencv3.1.0(整理)
- Git配置与使用总结
- Web应用开发出现的404,405,500错误