字典树 trie树 学习
一字典树
从根节点到某一节点,路径上经过的字符串连接起来,为该节点对应的字符串;
每个节点的所有子节点包含的字符都不相同。


/*
字典树学习: 输入n个单词 举出一个单词出现的次数。 10 apple
ppppp
hello
hello
need
bee
bee
none
you
apple need
==1
==Program ended with exit code: 0 */
//
// main.cpp
// CPlusDemo
//
// Created by HF on 2018/5/15.
// Copyright © 2018年 HF-Liqun. All rights reserved.
// #include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std; #define MAX 26 //the total number of alphabet is 26, a...z struct DicTrie{
bool isTerminal;//是否是单词结束标志
int count; //当前字符串出现次数
struct DicTrie *next[MAX ]; //每个节点 最多 有 MAX 个孩子节点 结构体嵌套
} ; DicTrie *root = NULL; // 根节点是一个空节点 特此声明并赋值 int init() //初始化链表,有对空间要求的 可选用该方法进行初始化
{
root = new DicTrie;
root->count = 0;
for (int i = 0; i < MAX; i ++) {
root->next[i] = NULL; // 空节点
root->isTerminal = false; //
}
return 0;
} bool isFindTrie(char *targetString) //判断是否能在字典树中查到目标串
{
int len = strlen(targetString);
DicTrie *head = root;
for (int i = 0; i < len; i ++) {
int res = (int)(targetString[i] - 'a');//当前小写字母对应数字
if (head->next[res] == NULL) { //如果是空节点 则为否 结束查找
return false;
} else {//不为空 更新头指针 以便继续往下查找
head = head->next[res];
}
}
if (head->count > 0) {
return true;
}
return false;
} int findTrieCount(char *targetString) //判断是否能在字典树中查到目标串
{
int len = strlen(targetString);
DicTrie *head = root;
for (int i = 0; i < len; i ++) {
int res = (int)(targetString[i] - 'a');//当前小写字母对应数字
if (head->next[res] == NULL) { //如果是空节点 则为否 结束查找
return false;
} else {//不为空 更新头指针 以便继续往下查找
head = head->next[res];
}
}
return head->count;
} int insertTrie(char *targetString)
{
int len = strlen(targetString);
DicTrie *head = root;
for (int i = 0; i < len; i ++) {
int res = (int)(targetString[i] - 'a');//当前小写字母对应数字
if (head->next[res] == NULL) { //如果是空节点
head->next[res] = new DicTrie;//则插入新节点元素
head = head->next[res]; //更新头指针 并初始化
head->count = 0; //
for (int j = 0; j < MAX; j ++) {
head->next[j] = NULL;
head->isTerminal = false;
}
} else {
head = head->next[res];
}
}
head->count ++;//每次插入一个,响应计数都增加1
head->isTerminal = true;
return head->count;
} int deleteTrie(char *targetString)
{
int len = strlen(targetString);
DicTrie *head = root;
for (int i = 0; i < len; i ++) {
int res = (int)(targetString[i] - 'a');//当前小写字母对应数字
if (head->next[res] == NULL) { //如果是空节点 表示删除的字符串不在字典中
return 0;
} else { //继续查找
head = head->next[res];
}
}
head->count --;//每次删除一个,响应计数都-1
if (head->count <= 0) {
head->count = 0;
head->isTerminal = false;
}
return 0;
} int main(int argc, const char * argv[]) {
// insert code here...
int n;
char targetString[20]; init(); scanf("%d",&n); //n 组数据
for (int i = 0; i < n; i ++) {
scanf("%s",targetString);
//字符串插入字典树
insertTrie(targetString);//插入方法
}
scanf("%s",targetString);
printf("==%d\n",findTrieCount(targetString));//查找方法 return 0;
}
参考
字典树 trie树 学习的更多相关文章
- 字典树(Trie树)的实现及应用
>>字典树的概念 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.与二叉查找树不同,Trie树的 ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结
Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...
- 洛谷$P4585\ [FJOI2015]$火星商店问题 线段树+$trie$树
正解:线段树+$trie$树 解题报告: 传送门$QwQ$ $umm$题目有点儿长我先写下题目大意趴$QwQ$,就说有$n$个初始均为空的集合和$m$次操作,每次操作为向某个集合内加入一个数$x$,或 ...
- luoguP6623 [省选联考 2020 A 卷] 树(trie树)
luoguP6623 [省选联考 2020 A 卷] 树(trie树) Luogu 题外话: ...想不出来啥好说的了. 我认识的人基本都切这道题了. 就我只会10分暴力. 我是傻逼. 题解时间 先不 ...
- 字典树trie的学习与练习题
博客详解: http://www.cnblogs.com/huangxincheng/archive/2012/11/25/2788268.html http://eriol.iteye.com/bl ...
- [转载]字典树(trie树)、后缀树
(1)字典树(Trie树) Trie是个简单但实用的数据结构,通常用于实现字典查询.我们做即时响应用户输入的AJAX搜索框时,就是Trie开始.本质上,Trie是一颗存储多个字符串的树.相邻节点间的边 ...
- Luogu P2922 [USACO08DEC]秘密消息Secret Message 字典树 Trie树
本来想找\(01Trie\)的结果找到了一堆字典树水题...算了算了当水个提交量好了. 直接插入模式串,维护一个\(Trie\)树的子树\(sum\)大小,求解每一个文本串匹配时走过的链上匹配数和终点 ...
- 【字符串算法】字典树(Trie树)
什么是字典树 基本概念 字典树,又称为单词查找树或Tire树,是一种树形结构,它是一种哈希树的变种,用于存储字符串及其相关信息. 基本性质 1.根节点不包含字符,除根节点外的每一个子节点都包含一个字符 ...
随机推荐
- UI-7-UIScrollView
#import "ViewController.h" @interface ViewController ()<UIScrollViewDelegate> { UIIm ...
- Trailing Zeroes (III) 假设n!后面有x个0.现在要求的是,给定x,要求最小的n; 判断一个n!后面有多少个0,通过n/5+n/25+n/125+...
/** 题目:Trailing Zeroes (III) 链接:https://vjudge.net/contest/154246#problem/N 题意:假设n!后面有x个0.现在要求的是,给定x ...
- virtualBox centos 6.x不能联网
sudo vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 TYPE=Ethernet UUID=3323da63---89bb-92 ...
- cocos2d-2.0-x-2.0.4生成vs2010项目模板的解决方法
cocos2d教学书籍还有网上好多博主都说仅仅要执行一下install-templates-msvc.bat这个批处理文件即可了.但是我按了半天vs2010就是不出现令小伙伴惊喜的cocos2d wi ...
- CentOS 6.5 MySQL5.6.26源码安装
一.源码安装cmake工具 从mysql5.5起,mysql源码安装开始使用cmake wget http://cmake.org/files/v3.2/cmake-3.2.3.tar.gztar z ...
- CentOS 6.5 Ruby源码安装
清除旧版Ruby,若存在 yum remove ruby 若为源码,使用如下命令 cd <your-ruby-source-path> make uninstall 下面开始安装Ruby ...
- 微信 oauth授权2
2.前面请求成功后 会在跳转url后得到 ?code=00b788e3b42043c8459a57a8d8ab5d9f&state=1 3.之后 使用code换取access_token 换取 ...
- Struts2上传文件(1)
使用Struts框架后, Struts2框架不会处理multipart/form-data的请求,它需要调用其他的上传文件框架来解析二进制数据.但是Struts在原有的上传解析器基础上做了很多的封装, ...
- SQL Server 日期格式化输出
转自:http://hi.baidu.com/%BC%D1%C0%D6%B1%C8%BA%A3/blog/item/fdaf6c9525adfa0f7af480ec.html T-SQL Script ...
- 性能百万/s:腾讯轻量级全局流控方案详解【转自Wetest】
阿里用的方案是在nginx中配置限流(限流功能模块是自己开发的),流量统计线上是有监控打通的,具体的限流值是通过线上流量表现+线下性能测试(模拟线上场景)测试得出的. 全新的全局流控实现方案,既解决了 ...