字典树的增、删、查、找前缀个数
来源:互联网 发布:android 仿淘宝首页 编辑:程序博客网 时间:2024/05/21 02:34
//// main.cpp// Trie 字典树的实现//// Created by zjl on 16/9/24.// Copyright © 2016年 zjl. All rights reserved.//#include <iostream>#include <string>#include <vector>using namespace std;const int number = 26;struct TrieNode{ int path;//多少个单词经过这个词 int end;//以此节点为结尾的单词有多少 其实最多就一个 vector<TrieNode*>num; TrieNode(){ path = 0; end = 0; num.reserve(number); }};void inserts(TrieNode* root, string word){ if(word.empty()) return; TrieNode* p = root; for(int i = 0; i < word.size(); i++){ int index = word[i] - 'a'; if(p->num[index] == NULL) p->num[index] = new TrieNode(); p = p->num[index]; p->path++; } p->end++;}void deletes(TrieNode* root, string word){ if(word.empty()) return; for(int i = 0; i < word.size(); i++){ int index = word[i] - 'a'; root->num[index]->path --; if( root->num[index]->path == 0){ root->num[index] = NULL; return; } root = root->num[index]; } root->end--;}bool search(TrieNode* root, string word){ if(word.empty()) return false; for(int i = 0; i < word.size(); i++){ int index = word[i] - 'a'; if(root->num[index] == NULL) return false; root = root->num[index]; } if(root->end > 0) return true; else return false;}//返回以prefix为前缀的单词的个数int prefixnumber(TrieNode* root, string word){ if(word.empty()) return 0; for(int i = 0; i < word.size(); i++){ int index = word[i] - 'a'; if(root->num[index] == NULL){ return 0; } root = root->num[index]; } return root->path;}int main(int argc, const char * argv[]) { // insert code here... return 0;}
0 0
- 字典树的增、删、查、找前缀个数
- poj 2001 Shortest Prefixes 字典树找前缀
- 【Tire 求字典出现的前缀个数】hihocoder 1014 Trie树
- HDU 1251 统计难题(字典树,统计前缀个数)
- HDU 1251 统计难题-字典树-统计前缀个数
- TrieTree(字典树,前缀树) 的实现 (增删改查)
- SQLite数据库的增,删,查,找
- 链表的增、删、查、找
- 字典树(前缀树)的应用
- 字典树(前缀树)的应用
- POJ2001 -- 字典树和字符串的前缀
- Trie树计算单词前缀的个数
- poj 2001 Shortest Prefixes 【字典树】【找每一个字符串在字符串集里面的 最短且可唯一标识 的前缀】
- 为什么使用了哈希算法的字典查集合找速度比全局遍历字典查集合速度快?
- HDU1251_统计难题_字典树(tire)查前缀含词条数
- 找0的个数
- android中SQLite数据库的增、删、查、找
- 动态顺序表的增、删、查、找
- Annotation 注解原理 @Butter Knife黄油刀进阶
- bzoj 2733 永无乡 Splay 启发式合并
- 我的vim配置
- win7 下安装 redhat6.4双系统
- Android线程前奏
- 字典树的增、删、查、找前缀个数
- iconv
- Qcom LK阶段如何使用ADC介绍
- (三) 总线_设备_驱动注册
- c++ 模板类 声明和定义都放在.h文件的原因
- 首次参加工作,第一期迭代任务
- Linux安装1T磁盘划分
- Java中禁止的包名(Prohibited package name)
- 中南大学急诊医学专业2008-2016年考研报录比【汇总】