数据结构 - trie
#include <cstring>
#include <iostream>
#include <map>
#include <cstdio>
using namespace std;
class Trie{
private :
map<char,Trie *> * root;
pair<bool,int> info;
inline Trie * makeNext(char c){
if(root == NULL){
root = new map<char ,Trie * >;
}
map<char, Trie *>::iterator it = root->find(c);
Trie * in ;
if( it == root->end()){
in = new Trie();
root->insert( pair<char , Trie * >(c,in));
}else{
in = it->second;
}
info.second ++ ;
return in;
}
inline void makeEnd(){
info.second ++ ;
info.first = true;
}
inline Trie * getChild(char c){
//该节点后面什么都没有了
if( root == NULL ){
return NULL;
}
map<char, Trie *>::iterator it = root->find(c);
if(it == root->end()){
//有兄弟节点,但是没有这个后续
return NULL;
}
return it->second;
}
void destory(){
if(root != NULL){
for(map<char ,Trie *> :: iterator it = root->begin() ; it!=root->end() ; ++ it ){
it->second->destory();
}
delete root;
}
}
public :
static pair<bool,int> None ;
Trie(){
root = NULL;
info = pair<bool,int>(false,);
}
~Trie(){
destory();
}
void addStr(char * str){
int len = strlen(str);
Trie * nowRoot = this;
while( (*str)!='\0'){
nowRoot = nowRoot->makeNext(*str);
++str;
}
nowRoot->makeEnd();
} pair<bool,int> findStr(char * str){
Trie * nowRoot = this;
while((*str)!='\0'){
nowRoot = nowRoot->getChild(*str);
if( nowRoot == NULL){
return None;
}
str++;
}
return nowRoot -> info;
}
};
pair<bool,int> Trie :: None = pair<bool,int>(false,-);
int main(){
Trie * a = new Trie();
a->addStr("abcdfg");
a->addStr("abcd");
a->addStr("abc");
a->addStr("ab");
cout<<"stop "<<endl;
pair <bool,int> ans = a->findStr("abc");
cout<< ans.first << " " << ans.second<<endl;
delete a;
return ;
}
数据结构 - trie的更多相关文章
- 数据结构—— Trie (前缀树)
实现一个 Trie (前缀树),包含 插入, 查询, 和 查询前缀这三个操作. Trie trie = new Trie(); trie.insert("apple"); trie ...
- 数据结构~trie树(字典树)
1.概述 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树. 我理解字典树是看了这位大佬博客.还不了解字典树的 ...
- 数据结构 -- Trie字典树
简介 字典树:又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种. 优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高. 性质: 1. 根节 ...
- hiho149周 - 数据结构 trie树
题目链接 坑点:accept和deny的ip可能相同,需加个判断 #include <cstdio> #include <cstdlib> #include <vecto ...
- 【数据结构】Trie树
数据结构--Trie树 概念 Trie树,又称字典树.前缀树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计 ...
- 数据结构 | 30行代码,手把手带你实现Trie树
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法和数据结构专题的第28篇文章,我们一起来聊聊一个经典的字符串处理数据结构--Trie. 在之前的4篇文章当中我们介绍了关于博弈论的 ...
- 基于trie树的具有联想功能的文本编辑器
之前的软件设计与开发实践课程中,自己构思的大作业题目.做的具有核心功能,但是还欠缺边边角角的小功能和持久化数据结构,先放出来,有机会一点点改.github:https://github.com/chu ...
- 讲解——Trie树(字典树)
Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问一个单词,回答这个单词是否在单 ...
- Trie树(转)
原文http://www.cnblogs.com/TheRoadToTheGold/p/6290732.html 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看 ...
- 浅谈Trie树(字典树)
Trie树(字典树) 一.引入 字典是干啥的?查找字的. 字典树自然也是起查找作用的.查找的是啥?单词. 看以下几个题: 1.给出n个单词和m个询问,每次询问一个单词,回答这个单词是否在单 ...
随机推荐
- Linux程序设计学习笔记----多线程编程线程同步机制之相互排斥量(锁)与读写锁
相互排斥锁通信机制 基本原理 相互排斥锁以排他方式防止共享数据被并发訪问,相互排斥锁是一个二元变量,状态为开(0)和关(1),将某个共享资源与某个相互排斥锁逻辑上绑定之后,对该资源的訪问操作例如以下: ...
- UIButton return(textField textView)
首先设置TextField 或 TextView的 delegate /////UITextView - (BOOL)textView:(UITextView *)textView shouldCha ...
- sqlplus登录、连接命令
经常使用: sqlplus username/password 如:普通用户登录 sqlplus scott/tiger sqlplus username/password@net_service ...
- 为了树莓派IIraspberrypi安装emacs+ecb+cedet+session+color-theme+cscope+linum
类似这篇文章写的不多,为了避免以后大家转来转去而忽略了写文章的时间,这些特别加上是2014年6月28日,省的对不上一些软件的版本号(下文中有些"最新"的说法就相应这个时间).假设转 ...
- The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar
出现 The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the j ...
- poj 2513 连接火柴 字典树+欧拉通路 好题
Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Total Submissions: 27134 Accepted: 7186 ...
- VSTO学习笔记(四)从SharePoint 2010中下载文件
原文:VSTO学习笔记(四)从SharePoint 2010中下载文件 上一次我们开发了一个简单的64位COM加载项,虽然功能很简单,但是包括了开发一个64位COM加载项的大部分过程.本次我们来给CO ...
- 使用 JQueryMobile 点击超链接提示“error loading page” 错误
使用jquery mobile创建dialog时出现加载错误,“Error Loading Page”. 原因是:jquery mobile页面默认采用ajax方式进行交互,而ajax方式下是不支持f ...
- C#获取Excel中所有的Sheet名称
原文地址:http://blog.csdn.net/qq1010726055/article/details/6858849 Excel.Application myExcel = new Excel ...
- python语言学习1——初识python
Python是著名的“龟叔”Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言. 龟叔给Python的定位是“优雅”.“明确”.“简单”,所以Python ...