字典树的一些使用
来源:互联网 发布:ifunbox中文版 mac 编辑:程序博客网 时间:2024/06/04 23:27
字典树的一些简单使用
字典树又被叫做tire树,又被叫做前缀树,顾名思义就是可以用来求字符串前缀的树形结构,与二叉树不同,字典树是多叉树,字符串不是直接存放在节点中,而是每一个节点存放一个字符,一个节点的孩子是具有相同的字符串前缀的。字典树的根节点通常是取空的。
字典树的特点是
1.所有相同的前缀只存储一次
2.查找一个字符串是否存在只需该字符串长度次查找
如图
创建一个字典树的节点方法为
查找匹配前缀
就只实现了字典树的这三个简单的功能。
字典树的特点是
1.所有相同的前缀只存储一次
2.查找一个字符串是否存在只需该字符串长度次查找
如图
其基本性质可以归纳为:
1. 根节点不包含字符,除根节点外每一个节点都只包含一个字符。
2. 从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。
3. 每个节点的所有子节点包含的字符都不相同。
搜索字典项目的方法为:
(1) 从根结点开始一次搜索;
(2) 取得要查找关键词的第一个字母,并根据该字母选择对应的子树并转到该子树继续进行检索;
(3) 在相应的子树上,取得要查找关键词的第二个字母,并进一步选择对应的子树进行检索。
(4) 迭代过程……
(5) 在某个结点处,关键词的所有字母已被取出,则读取附在该结点上的信息,即完成查找。
字典树的存储结构
struct node{int num; node*next[Max];};
创建一个字典树的节点方法为
node *createnode(){node *p = new node;for (int i = 0; i < Max; i++){p->next[i] = NULL;}p->num = 0;return p;}插入一个字符串的操作
void insertstr(char *str, node *root){int len = strlen(str);node *t, *p = root;for (int i = 0; i < len; i++){int id = str[i] - 'a';if (p->next[id] == NULL){t = createnode();p->next[id] = t;p->num++;p = p->next[id];}else{p = p->next[id];}}}
查找匹配前缀
int searchtire(char *str, node *root){node *p = root;int len = strlen(str);int count = 0;for (int i = 0; i < len; i++){int id = str[i] - 'a';if (p->next[id] == NULL){count = 0;return 0;}else{p = p->next[id];count = p->num;}}return count;}
就只实现了字典树的这三个简单的功能。
下面有一些字典树的简单题
http://acm.hdu.edu.cn/showproblem.php?pid=1251
AC代码
#include<iostream>using namespace std;struct node{ int num; node *next[26];};node *createnode(){ node *p = new node(); for (int i = 0; i < 26; i++) { p->next[i] = NULL; } p->num = 0; return p;}void insertnode(char *str, node *root){ node *p; p = root; int len = strlen(str); for (int i = 0; i < len; i++) { int id = str[i] - 'a'; if (p->next[id] == NULL) { p->next[id] = createnode(); p = p->next[id]; p->num++; } else { p = p->next[id]; p->num++; } }}int search(char *str, node *root){ node *p; p = root; int len = strlen(str); for (int i = 0; i < len; i++) { int id = str[i] - 'a'; if (p->next[id] == NULL) { return 0; } else { p = p->next[id]; } } return p->num;}int main(){ char str[20]; node *root = createnode(); while (gets_s( str)) { if (str[0] == 0) break; insertnode(str, root); } while (cin >> str) { cout << search(str, root) << endl; } return 0;}http://acm.hdu.edu.cn/showproblem.php?pid=1305
AC代码
#include<iostream>using namespace std;#define Max 2typedef struct node{ int num; int sz; node *next [Max];};node *createbode(){ node *p = new node; for (int i = 0; i < Max; i++) { p->next[i] = NULL; } p->num = 0; p->sz = 0; return p;}void insertnode(char *str, node *root){ node *t, *p = root; int len = strlen(str); for (int i = 0; i < len; i++) { int id = str[i] - '0'; if (p->next[id] == NULL) { t = createbode(); p->next[id] = t; p = p->next[id]; p->num++; } else { p = p->next[id]; } } p->sz = 1;}int search(char *str, node *root){ node *p = root; int len = strlen(str); for (int i = 0; i < len; i++) { int id = str[i] - '0'; if (p->next[id] == NULL) { return 0; } else { if (p->next[id]->sz == 1) return 1; if (p->next[id]->num == 0) return 0; p = p->next[id]; } } return 1;}int main(){ char str[100]; int key = 0; while(cin >> str) { key++; node *root = createbode(); int flag = 0; int count = 0; insertnode(str, root); while (cin >> str) { if (strcmp(str, "9") == 0) { if (count == 0) { flag = 0; } break; } count++; flag = search(str, root); if (flag == 1) { while (cin >> str) { if (strcmp(str, "9") == 0) break; } break; } insertnode(str, root); } if (flag) { cout << "Set " << key << " is not immediately decodable" << endl; } else { cout << "Set " << key << " is immediately decodable" << endl; }} return 0;}
0 0
- 字典树的一些使用
- 字典树的使用
- 关于字典树的一些常用知识
- 字典的一些基础知识
- 字典与数组嵌套使用时遇到的一些问题
- 一些字典树模板
- 字典树的使用 poj1451(字典树+BFS+剪枝)
- 网络字典的一些想法
- 数据字典的一些内容
- Python字典的一些遍历
- 字典的一些基础用法
- 数据结构:字典树的基本使用
- 数据库-字典的使用
- Swift字典的使用
- 字典的使用
- NSDictionary字典的使用
- 字典的使用
- Python 字典的使用
- 自定义 input type="file" 文件上传样式
- 文件系统与磁盘格式
- 深圳办理港澳通行证及续签
- ListView绑定数据后界面显示一条数据原因
- iOS开发辅助工具,沙盒定位软件和数据库可视化软件
- 字典树的一些使用
- The 10 Best Open Source Projects You Should Be Volunteering To Help With
- android横向滑动选择的view
- iOS中socket的使用
- 笔记
- Google 2015 APAC Round A Problem C - Addition
- struts2使用action方式实现文件下载(支持中文文件名)
- TQ2440 学习笔记—— 10、嵌入式编程基础知识【arm-linux-ld 选项】
- 项目做完了,总结一下