数据结构与算法——字典(前缀)树的实现

参考视频:左程云--算法讲解044【必备】前缀树原理和代码详解

类实现:

class Trie {private:class TrieNode {public:int pass;int end;vector<TrieNode*> nexts;TrieNode(): pass(0), end(0), nexts(26, nullptr) {}};TrieNode* root;  // 根指针public:Trie() {root = new TrieNode();}void insert(const string& word) {TrieNode* node = root;node->pass++;int path;for(char c : word){path = c - 'a';if (node->nexts[path] == nullptr) {node->nexts[path] = new TrieNode();}node = node->nexts[path];node->pass++;}node->end++;}void mydelete(const string& word) {if (countWordsEqualTo(word) > 0) {TrieNode* node = root;node->pass--;int path;for (char c : word) {path = c - 'a';if (--node->nexts[path]->pass == 0) {funcDelete(node, path);return;}node = node->nexts[path];}node->end--;}}void funcDelete(TrieNode* node, int path) {TrieNode* target = node->nexts[path];node->nexts[path] = nullptr;delete (target);}bool search(const string& word) {return countWordsEqualTo(word) > 0 ? true : false;}int countWordsEqualTo(const string& word) {TrieNode* node = root;int path;for (char c : word) {path = c - 'a';if (node->nexts[path] == nullptr) {return 0;}node = node->nexts[path];}return node->end;}int prefixNumber(const string& pre) {TrieNode* node = root;int path;for (char c : pre) {path = c - 'a';if (node->nexts[path] == nullptr) {return 0;}node = node->nexts[path];}return node->pass;}
};

类实现基础上进行哈希表的优化:

class Trie {
private:class TrieNode {public:int pass;int end;unordered_map<int, TrieNode*> nexts;TrieNode() : pass(0), end(0) {}};TrieNode* root;  // 根指针public:Trie() {root = new TrieNode();}void insert(const string& word) {TrieNode* node = root;node->pass++;int path;for (char c : word) {path = c - 'a';if (node->nexts.find(path) == node->nexts.end()) {node->nexts.insert({ path, new TrieNode() });}node = node->nexts[path];node->pass++;}node->end++;}void mydelete(const string& word) {if (countWordsEqualTo(word) > 0) {TrieNode* node = root;node->pass--;int path;for (char c : word) {path = c - 'a';if (--node->nexts[path]->pass == 0) {node->nexts.erase(path);return;}node = node->nexts[path];}node->end--;}}bool search(const string& word) {return countWordsEqualTo(word) > 0 ? true : false;}int countWordsEqualTo(const string& word) {TrieNode* node = root;int path;for (char c : word) {path = c - 'a';if (node->nexts.find(path) == node->nexts.end()) {return 0;}node = node->nexts[path];}return node->end;}int prefixNumber(const string& pre) {TrieNode* node = root;int path;for (char c : pre) {path = c - 'a';if (node->nexts.find(path) == node->nexts.end()) {return 0;}node = node->nexts[path];}return node->pass;}
};

静态数组实现 + 牛客测试

测试链接:牛客题霸--字典树的实现

#include <bits/stdc++.h>
using namespace std;
const static int N = 1e6 + 5;
int trieArr[N][26];
int passArr[N];
int endArr[N];
int cnt;
void helpFunc(int op, string& word);
class trie{
public:void static buildTrie(){cnt = 1;}void static insert(const string& word){int cur = 1;passArr[cur]++;int path;for(char c : word){path = c - 'a';if(trieArr[cur][path] == 0){trieArr[cur][path] = ++cnt;}cur = trieArr[cur][path];passArr[cur]++;}endArr[cur]++;}int static searchCount(const string& word){int cur = 1, path;for(char c : word){path = c - 'a';if(trieArr[cur][path] == 0){return 0;}cur = trieArr[cur][path];}return endArr[cur];}bool static search(const string& word){return searchCount(word) > 0 ? true : false;}int static prefixCount(const string& pre){int cur = 1, path;for(char c : pre){path = c - 'a';if(trieArr[cur][path] == 0){return 0;}cur = trieArr[cur][path];}return passArr[cur];}void static deleteWord(const string& word){if(searchCount(word) > 0){int cur = 1, path;passArr[cur]--;for(char c : word){path = c - 'a';if(--passArr[trieArr[cur][path]] == 0){trieArr[cur][path] = 0;return;}cur = trieArr[cur][path];}endArr[cur]--;}}void static rebuildTrie(){for(int i = 1; i <= cnt; i++){fill(&trieArr[i][0], &trieArr[i][0] + 26, 0);passArr[i] = 0;endArr[i] = 0;}}
};int main() {int m;cin >> m;trie::buildTrie();while(m--){int op;string word;cin >> op >> word;helpFunc(op, word);}trie::rebuildTrie();return 0;
}void helpFunc(int op, string& word){switch(op){case 1 :trie::insert(word);break;case 2 :trie::deleteWord(word);break;case 3 :if(trie::search(word)){cout << "YES" << endl;}else{cout << "NO" << endl;}break;case 4 :cout << trie::prefixCount(word) <<endl;break;default:break;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/diannao/93943.shtml
繁体地址,请注明出处:http://hk.pswp.cn/diannao/93943.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

STORM代码阅读笔记

默认的 分辨率是 [160,240] &#xff0c;基于 Transformer 的方法不能做高分辨率。 Dataloader 输入是 带有 pose 信息的 RGB 图像 eval datasets ## 采样帧数目 20 num_max_future_frames int(self.timespan * fps) ## 每次间隔多少个时间 timesteps 取一个context image n…

2025电赛G题-发挥部分-参数自适应FIR滤波器

&#xff08;1&#xff09;测评现场提供由RLC元件&#xff08;各1个&#xff09;组成的“未知模型电路”。 按照图3所示&#xff0c;探究装置连接该电路的输入和输出端口&#xff0c;对该电路进行 自主学习、建模&#xff08;不可借助外部测试设备&#xff09;&#xff0c;2分钟…

Linux基础 -- 内核快速向用户态共享内核变量方案之ctl_table

系统化、可直接上手的 /proc/sys sysctl 接口使用文档。内容涵盖&#xff1a;机制原理、适用场景、ctl_table 字段详解、常用解析器&#xff08;proc_handler&#xff09;完整清单与选型、最小样例到进阶&#xff08;范围校验、毫秒→jiffies、字符串、数组、每网络命名空间&a…

【RH124知识点问答题】第3章 从命令行管理文件

1. 怎么理解“Linux中一切皆文件”&#xff1f;Linux是如何组织文件的&#xff1f;&#xff08;1&#xff09;“Linux中一切皆文件”的理解和文件组织&#xff1a;在Linux中&#xff0c;“一切皆文件”指的是Linux将各种设备、目录、文件等都视为文件对象进行管理。这种统一的文…

练习javaweb+mysql+jsp

只是简单的使用mysql、简单的练习。 有很多待完善的地方&#xff0c;比如list的servlet页面&#xff0c;应该判断有没有用户的。 比如list.jsp 应该循环list而不是写死 index.jsp 样式可以再优化一下的。比如按钮就特丑。 本文展示了一个简单的MySQL数据库操作练习项目&#x…

使用Nginx部署前端项目

使用Nginx部署前端项目 一、总述二、具体步骤 2.1解压2.2将原来的html文件夹的文件删除&#xff0c;将自己的静态资源文件放进去&#xff0c;点击nginx.exe文件启动项目2.3查看进程中是否有ngix的两个进程在浏览器中输入“localhost:端口号”即可访问。 2.4端口被占用情况处理 …

【论文学习】KAG论文翻译

文章目录KAG: Boosting LLMs in Professional Domains via Knowledge Augmented Generation摘要1 引言2 方法论2.1 LLM友好型知识表示2.2 互索引机制2.2.1 语义分块2.2.2 带丰富语境的的信息抽取2.2.3 领域知识注入与约束2.2.4 文本块向量与知识结构的相互索引2.3 逻辑形式求解…

24黑马SpringCloud安装MybatisPlus插件相关问题解决

目录 一、前言 二、菜单栏没有Other 三、Config Database里的dburl需要加上时区等配置 一、前言 在学习24黑马SpringCloud的MybatisPlus-12.拓展功能-代码生成器课程时&#xff0c;发现由于IDEA版本不同以及MybatisPlus版本更新会出现与视频不一致的相关问题&#xff0c;本博…

人工智能赋能聚合物及复合材料模型应用与实践

近年来&#xff0c;生成式人工智能&#xff08;包括大语言模型、分子生成模型等&#xff09;在聚合物及复合材料领域掀起革命性浪潮&#xff0c;其依托数据驱动与机理协同&#xff0c;从海量数据中挖掘构效关系、通过分子结构表示&#xff08;如 SMILES、BigSMILES&#xff09;…

MyBatis-Plus3

一、条件构造器和常用接口 1.wapper介绍 MyBatis-Plus 提供了一套强大的条件构造器&#xff08;Wrapper&#xff09;&#xff0c;用于构建复杂的数据库查询条件。Wrapper 类允许开发者以链式调用的方式构造查询条件&#xff0c;无需编写繁琐的 SQL 语句&#xff0c;从而提高开…

GXP6040K压力传感器可应用于医疗/汽车/家电

GXP6040K 系列压力传感器是一种超小型&#xff0c;为设备小型化做出贡献的高精度半导体压力传感器&#xff0c;适用于生物医学、汽车电子、白色家电等领域。采用标准的SOP6 和 DIP6 封装形式&#xff0c;方便用户进行多种安装方式。 内部核心芯片是利用 MEMS&#xff08;微机械…

Android ConstraintLayout 使用详解

什么是 ConstraintLayoutConstraintLayout&#xff08;约束布局&#xff09;是 Android Studio 2.2 引入的一种新型布局&#xff0c;现已成为 Android 开发中最强大、最灵活的布局管理器之一。它结合了 RelativeLayout 的相对定位和 LinearLayout 的线性布局优势&#xff0c;能…

Unity3D数学第三篇:坐标系与变换矩阵(空间转换篇)

Unity3D数学第一篇&#xff1a;向量与点、线、面&#xff08;基础篇&#xff09; Unity3D数学第二篇&#xff1a;旋转与欧拉角、四元数&#xff08;核心变换篇&#xff09; Unity3D数学第三篇&#xff1a;坐标系与变换矩阵&#xff08;空间转换篇&#xff09; Unity3D数学第…

UV安装并设置国内源

文章目录一、UV下载1.官方一键安装2.github下载安装二、更换国内镜像源&#xff08;加速下载&#xff09;方法1&#xff1a;临时环境变量&#xff08;单次生效&#xff09;方法2&#xff1a;永久配置&#xff08;推荐&#xff09;方法3&#xff1a;命令行直接指定源三、验证镜像…

1 前言:什么是 CICD 为什么要学 CICD

什么是 CI/CD 我的资源库网站&#xff1a;https://www.byteooo.cn 在开发阶段&#xff0c;许多编译工具会将我们的源码编译可使用的文件。例如 vue-cli 的项目会被 webpack 打包编译为浏览器的文件&#xff0c;Java 项目会被编译为 .class/jar 文件以供服务器使用。 但是&am…

GitHub 趋势日报 (2025年07月30日)

&#x1f4ca; 由 TrendForge 系统生成 | &#x1f310; https://trendforge.devlive.org/ &#x1f310; 本日报中的项目描述已自动翻译为中文 &#x1f4c8; 今日获星趋势图 今日获星趋势图3579copyparty752supervision664500-AI-Agents-Projects483awesome403prompt-optim…

“非参数化”大语言模型与RAG的关系?

这个问题触及了一个关键的技术细节&#xff0c;两者关系密切&#xff0c;但层面不同&#xff1a; “非参数化”大语言模型是一个更广泛的概念或类别&#xff0c;而RAG&#xff08;Retrieval-Augmented Generation&#xff09;是实现这一概念最主流、最具体的一种技术框架。 您可…

LeetCode Hot 100:15. 三数之和

题目给你一个整数数组 nums &#xff0c;判断是否存在三元组 [nums[i], nums[j], nums[k]] 满足 i ! j、i ! k 且 j ! k &#xff0c;同时还满足 nums[i] nums[j] nums[k] 0 。请你返回所有和为 0 且不重复的三元组。注意&#xff1a;答案中不可以包含重复的三元组。示例 1&…

银行回单识别应用场景剖析

银行回单OCR识别技术通过自动化处理纸质或电子回单中的关键信息&#xff0c;显著提升了金融、企业及个人场景下的数据管理效率。以下是其核心应用场景及价值的详细剖析&#xff1a;一、企业财务场景自动化账务处理对账与记账&#xff1a;OCR自动提取交易日期、金额、账号等信息…

React的介绍和特点

1. React是什么&#xff1f; 1.1. React&#xff1a; 用于构建用户界面的JavaScript库1.2. React的官网文档&#xff1a;https://zh-hans.reactjs.org/ 2. React的特点2.1. 声明式编程&#xff1a; 目前整个大前端开发的模式&#xff1a;Vue、React、Flutter、SwiftUI只需要维护…