映射关系5

明白!🚀
你要我 继续扩展,在这套 C++98 代码里加一个功能:

根据完整的5位ID,反查出对应的路径。

OK,我直接接着上面那版来,给你补充 getPathFromId 方法,并且保持整体风格统一,简洁、高效!

✨ 完整加上 getPathFromId 的 STDTMDirTree

#include
#include
#include
#include
#include

namespace STDTM {

class STDTMDirTree {
private:
struct Node {
std::map<std::string, Node*> children;
std::string idPart;
};

Node* root;
static const size_t TOTAL_DIGITS = 5;void clear(Node* node) {std::map<std::string, Node*>::iterator it;for (it = node->children.begin(); it != node->children.end(); ++it) {clear(it->second);}delete node;
}std::vector<std::string> splitPath(const std::string& path) const {std::vector<std::string> parts;std::string part;for (size_t i = 0; i < path.length(); ++i) {if (path[i] == '/') {if (!part.empty()) {parts.push_back(part);part.clear();}} else {part += path[i];}}if (!part.empty()) {parts.push_back(part);}return parts;
}void buildIdString(const std::vector<std::string>& idParts, char result[TOTAL_DIGITS + 1]) const {size_t pos = 0;for (size_t i = 0; i < idParts.size(); ++i) {const std::string& part = idParts[i];for (size_t j = 0; j < part.size() && pos < TOTAL_DIGITS; ++j) {result[pos++] = part[j];}}while (pos < TOTAL_DIGITS) {result[pos++] = '0';}result[TOTAL_DIGITS] = '\0';
}bool findPathRecursive(Node* node, const char* id, size_t pos, std::string currentPath, std::string& foundPath) const {if (pos >= TOTAL_DIGITS) {foundPath = currentPath;return true;}std::map<std::string, Node*>::const_iterator it;for (it = node->children.begin(); it != node->children.end(); ++it) {Node* child = it->second;const std::string& part = child->idPart;if (pos + part.size() > TOTAL_DIGITS) {continue; // 超出,总位数不允许}bool match = true;for (size_t i = 0; i < part.size(); ++i) {if (id[pos + i] != part[i]) {match = false;break;}}if (match) {std::string newPath = currentPath + "/" + it->first;if (findPathRecursive(child, id, pos + part.size(), newPath, foundPath)) {return true;}}}return false;
}

public:
STDTMDirTree() {
root = new Node();
}

~STDTMDirTree() {clear(root);
}void insertPathWithIds(const std::string& path, const std::vector<std::string>& idParts) {Node* current = root;std::vector<std::string> parts = splitPath(path);if (parts.size() != idParts.size()) {throw std::string("Error: Path and idParts size mismatch");}for (size_t i = 0; i < parts.size(); ++i) {const std::string& name = parts[i];if (current->children.count(name) == 0) {Node* newNode = new Node();newNode->idPart = idParts[i];current->children[name] = newNode;}current = current->children[name];}
}std::string getIdFromPath(const std::string& path) const {Node* current = root;std::vector<std::string> parts = splitPath(path);std::vector<std::string> idParts;for (size_t i = 0; i < parts.size(); ++i) {const std::string& name = parts[i];if (current->children.count(name) == 0) {return "[Path not found]";}current = current->children.find(name)->second;idParts.push_back(current->idPart);}char result[TOTAL_DIGITS + 1];buildIdString(idParts, result);return std::string(result);
}std::string getPathFromId(const std::string& id) const {if (id.size() != TOTAL_DIGITS) {return "[Invalid ID]";}std::string foundPath;if (findPathRecursive(root, id.c_str(), 0, "", foundPath)) {return foundPath;} else {return "[Path not found]";}
}

};

} // namespace STDTM

🧠 说明
• findPathRecursive :从树根出发,按ID去匹配,一层层往下走。
• 每次检查节点的idPart,必须能对应上 id 数组的相应位置。
• 如果能一路匹配到5位数字填满,就找到路径了!

📦 使用示例

STDTM::STDTMDirTree tree;
tree.insertPathWithIds(“/robot/arm1”, std::vectorstd::string{“1”, “234”});
tree.insertPathWithIds(“/station/slot3”, std::vectorstd::string{“23”, “456”});

std::cout << tree.getIdFromPath(“/robot/arm1”) << std::endl; // 12340
std::cout << tree.getIdFromPath(“/station/slot3”) << std::endl; // 23456

std::cout << tree.getPathFromId(“12340”) << std::endl; // /robot/arm1
std::cout << tree.getPathFromId(“23456”) << std::endl; // /station/slot3

要不要我顺便再教你一招,怎么加速查询?比如维护一个id到路径的哈希表,瞬间查出路径,速度还能提一档。要的话告诉我:「继续加速版本!」🚀
要不要?😎

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

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

相关文章

编译原理:由浅入深从语法树到文法类型

文法与语言基础&#xff1a;从语法树到文法类型 文法&#xff08;Grammar&#xff09;和语言&#xff08;Language&#xff09;是计算机科学和语言学中解析和理解语言结构的核心概念。无论是编程语言的编译器设计&#xff0c;还是自然语言处理&#xff08;NLP&#xff09;中的…

第十三步:vue

Vue 1、上手 1、安装 使用命令&#xff1a;npm create vuelatestvue文件后缀为.vueconst app createApp(App)&#xff1a;初始化根组件app.mount("#app")&#xff1a;挂载根组件到页面 2、文件 script标签&#xff1a;编写jstemplate标签&#xff1a;编写htmls…

Pytest-mark使用详解(跳过、标记、参数 化)

1.前言 在工作中我们经常使用pytest.mark.XXXX进行装饰器修饰&#xff0c;后面的XXX的不同&#xff0c;在pytest中有不同的作 用&#xff0c;其整体使用相对复杂&#xff0c;我们单独将其抽取出来做详细的讲解。 2.pytest.mark.skip()/skipif()跳过用例 import pytest #无条…

基于 Spring Boot 的井字棋游戏开发与实现

目录 引言 项目概述 项目搭建 1. 环境准备 2. 创建 Spring Boot 项目 3. 项目结构 代码实现 1. DemoApplication.java 2. TicTacToeController.java 3. pom.xml 电脑落子策略 - Minimax 算法 findBestMove 方法 minimax 方法 运行游戏 总结 引言 在软件开发领域&…

【算法笔记】贪心算法

一、什么是贪心算法&#xff1f; 贪心算法是一种在每一步选择中都采取当前看起来最优&#xff08;最“贪心”&#xff09;的策略&#xff0c;从而希望得到全局最优解的算法设计思想。 核心思想&#xff1a;每一步都做出局部最优选择&#xff0c;不回退。适用场景&#xff1a;…

现代c++获取linux所有的网络接口名称

现代c获取linux所有的网络接口名称 前言一、在linux中查看网络接口名称二、使用c代码获取三、验证四、完整代码如下五、总结 前言 本文介绍一种使用c获取本地所有网络接口名称的方法。 一、在linux中查看网络接口名称 在linux系统中可以使用ifconfig -a命令列举出本机所有网络…

打印及判断回文数组、打印N阶数组、蛇形矩阵

打印回文数组 1 1 1 1 1 1 2 2 2 1 1 2 3 2 1 1 2 2 2 1 1 1 1 1 1方法1&#xff1a; 对角线对称 左上和右下是对称的。 所以先考虑左上打印&#xff0c; m i n ( i 1 , j 1 ) \text min(i1,j1) min(i1,j1)&#xff0c;打印出来&#xff1a; 1 1 1 1 1 2 2 2 1 2 3 3 1 2 …

详解UnityWebRequest类

什么是UnityWebRequest类 UnityWebRequest 是 Unity 引擎中用于处理网络请求的一个强大类&#xff0c;它可以让你在 Unity 项目里方便地与网络资源进行交互&#xff0c;像发送 HTTP 请求、下载文件等操作都能实现。下面会详细介绍 UnityWebRequest 的相关内容。 UnityWebRequ…

UE5 在旋转A的基础上执行旋转B

用径向slider实现模型旋转时&#xff0c;得到的结果与ue编辑器里面的结果有很大出入。 问题应该是 两个FRotator&#xff08;0&#xff0c;10&#xff0c;0&#xff09;和&#xff08;10&#xff0c;20&#xff0c;30&#xff09;&#xff0c; 两个FRotator的加法结果为&…

4.2 Prompt工程与任务建模:高效提示词设计与任务拆解方法

提示词工程&#xff08;Prompt Engineering&#xff09;和任务建模&#xff08;Task Modeling&#xff09;已成为构建高效智能代理&#xff08;Agent&#xff09;系统的核心技术。提示词工程通过精心设计的自然语言提示词&#xff08;Prompts&#xff09;&#xff0c;引导大型语…

MySQL 索引的最左前缀匹配原则是什么?

MySQL 索引的最左前缀匹配原则详解 最左前缀匹配原则&#xff08;Leftmost Prefix Principle&#xff09;是 MySQL 复合索引&#xff08;联合索引&#xff09;查询优化中的核心规则&#xff0c;理解这一原则对于高效使用索引至关重要。 核心概念 定义&#xff1a;当查询条件…

SQL命令

一、控制台中查询命令 默认端口号&#xff1a;3306 查看服务器版本: mysql –version 启动MySQL服务&#xff1a;net start mysql 登录数据库&#xff1a;mysql -u root -p 查看当前系统下的数据库&#xff1a;show databases&#xff1b; 创建数据库&#xff1a;create…

新增 29 个专业,科技成为关键赛道!

近日&#xff0c;教育部正式发布《普通高等学校本科专业目录&#xff08;2025年&#xff09;》&#xff0c;新增 29 个本科专业&#xff0c;包括区域国别学、碳中和科学与工程、海洋科学与技术、健康与医疗保障、智能分子工程、医疗器械与装备工程、时空信息工程、国际邮轮管理…

零基础上手Python数据分析 (23):NumPy 数值计算基础 - 数据分析的加速“引擎”

写在前面 —— 超越原生 Python 列表,解锁高性能数值计算,深入理解 Pandas 的底层依赖 在前面一系列关于 Pandas 的学习中,我们已经领略了其在数据处理和分析方面的强大威力。我们学会了使用 DataFrame 和 Series 来高效地操作表格数据。但是,你是否好奇,Pandas 为何能够…

Android 13.0 MTK Camera2 设置默认拍照尺寸功能实现

Android 13.0 MTK Camera2 设置默认拍照尺寸功能实现 文章目录 需求&#xff1a;参考资料架构图了解Camera相关专栏零散知识了解部分相机源码参考&#xff0c;学习API使用&#xff0c;梳理流程&#xff0c;偏应用层Camera2 系统相关 修改文件-修改方案修改文件&#xff1a;修改…

HarmonyOS 框架基础知识

参考文档&#xff1a;HarmonyOS开发者文档 第三方库&#xff1a;OpenHarmony三方库中心仓 基础特性 Entry&#xff1a;关键装饰器 Components&#xff1a;组件 特性EntryComponent​​作用范围仅用于页面入口可定义任意可复用组件​​数量限制​​每个页面有且仅有一个无数量…

前端分页与瀑布流最佳实践笔记 - React Antd 版

前端分页与瀑布流最佳实践笔记 - React Antd 版 1. 分页与瀑布流对比 分页&#xff08;Pagination&#xff09;瀑布流&#xff08;Infinite Scroll&#xff09;展示方式按页分批加载&#xff0c;有明确页码控件滚动到底部时自动加载更多内容&#xff0c;无明显分页用户控制用…

Linux网络编程:TCP多进程/多线程并发服务器详解

Linux网络编程&#xff1a;TCP多进程/多线程并发服务器详解 TCP并发服务器概述 在Linux网络编程中&#xff0c;TCP服务器主要有三种并发模型&#xff1a; 多进程模型&#xff1a;为每个客户端连接创建新进程多线程模型&#xff1a;为每个客户端连接创建新线程I/O多路复用&am…

详解springcloudalibaba采用prometheus+grafana实现服务监控

文章目录 1.官网下载安装 prometheus和grafana1.promethus2.grafana 2. 搭建springcloudalibaba集成prometheus、grafana1. 引入依赖,springboot3.2之后引入如下2. 在yml文件配置监控端点暴露配置3. 在当前启动的应用代码中添加&#xff0c;在prometheus显示的时候附加当前应用…

数据分析1

一、常用数据处理模块Numpy Numpy常用于高性能计算&#xff0c;在机器学习常常作为传递数据的容器。提供了两种基本对象&#xff1a;ndarray、ufunc。 ndarray具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组。 ufunc提供了对数组快速运算的标准数学函数。 ndar…