zoukankan
html css js c++ java
用指针传递的字典树
#include <string> using namespace std; #define NUM 26 #define TYPE 'A' class dictree { public: dictree *child[NUM]; string *value;//节点所存的数据,根据题目而使用不同的类型或数据 dictree(){memset(child,0,sizeof(child));value=NULL;} ~dictree(); bool insert(string s, string s2); string search(string s); }; //把信息s2插入到结点s处 bool dictree::insert(string s,string s2) { int len,i,j; dictree *current,*newnode; len=s.length(); if(len==0)return 0; current=this; for(i=0;i<len;i++) { if(current->child[s[i]-TYPE]!=0) current=current->child[s[i]-TYPE]; else { newnode=(dictree *)malloc(sizeof(dictree)); for(j=0;j<26;j++) newnode->child[j]=0; current->child[s[i]-TYPE]=newnode; current=newnode; } if(i == len - 1)current->value=new string(s2); } return 0; } //搜索s结点的信息 string dictree::search(string s) { int len,i; string ret = ""; dictree *current; len=s.length(); if(len==0)return ret; current=this; for(i=0;i<len;i++) { if(current->child[s[i]-TYPE]!=0) current=current->child[s[i]-TYPE]; else return ret; if(i == len - 1)ret = *current->value; } return ret; } dictree::~dictree() { if(this == NULL) return; for(int i = 0; i < 26; i++) delete this->child[i]; delete value; }
查看全文
相关阅读:
day-14 模块的使用,循环导入,模块导入路径优先级,项目的目录结构
day13-三元表达式,生成式,递归函数,匿名函数
day12-无参装饰器,迭代器和生成器
day11-函数对象,函数嵌套,名称空间与作用域,闭包函数,以及装饰器的前言
一周总结(4)
大道至简读后感
一周总结(3)
一周总结(2)
一周总结(1)
关于教室派app的使用体验与建议
原文地址:https://www.cnblogs.com/windmissing/p/2559822.html
最新文章
华华听月月唱歌(贪心)
成为王的痛苦(前缀最大优化dp)
距集训结束还有2天,好想回家
博客新手,多多关照
分块查找
折半查找
顺序查找
查找
关键路径
拓扑排序
热门文章
有向无环图——描述表达式
最短路径——Floyd算法
最短路径——Dijkstra算法
最短路径——BFS算法
day20-继承于抽象,继承的应用,继承的实现原理,派生与方法重用,组合
day19-python中一切皆对象,类中的内置装饰器应用,绑定方法和非绑定方法,继承
day18-面向对象编程介绍,对象和类的基本使用
day17-常用模块之hashlib模块,subprocess模块,os模块和sys模块以及configparter模块
day16-常用模块之re,json,pickle,time,datetime和random模块
day15-包和logging模块的使用
Copyright © 2011-2022 走看看