当前位置: 首页 > news >正文

C++自定义字典树结构

代码 
#include <iostream>
using namespace std;class TrieNode
{
public:char data;TrieNode* children[26];bool isTerminal;TrieNode(char ch){data = ch;for (int i = 0; i < 26; i++){children[i] = NULL;}isTerminal = false;}
};
class Trie
{
public:TrieNode* root;Trie(){root = new TrieNode('\0');}void insertUtil(TrieNode* root, string word){// base caseif (word.length() == 0){root->isTerminal = true;return;}// assumption , world will be CAPSint index = word[0] - 'A';TrieNode* child;// presentif (root->children[index] != NULL){child = root->children[index];}else{// absentchild = new TrieNode(word[0]);root->children[index] = child;}// RECURSIONinsertUtil(child, word.substr(1));}void insertWord(string word){insertUtil(root, word);}bool searchUtil(TrieNode* root, string word){// base caseif (word.length() == 0){return root->isTerminal;}int index = word[0] - 'A';TrieNode* child;// presentif (root->children[index] != NULL){child = root->children[index];}else{// absentreturn false;}// RECURSIONreturn searchUtil(child, word.substr(1));}bool searchWord(string word){return searchUtil(root, word);}
};#include <bitset>
#include <unordered_map>
#include <vector>
template <std::size_t N>
using Signature = std::bitset<N>;// util function to split string into parts by given delimiter.
static void split(std::string_view s, std::vector<std::string>& parts, char delimiter) {parts.emplace_back();for (auto ch : s) ch == delimiter ? parts.push_back("") : parts.back().push_back(ch);
};// A MyTrie structures ids by names into a tree.
template <std::size_t N>
class MyTrie {
private:// Signature of all signals under this tree.// e.g. the node `b` matches all "a.b.*"Signature<N> signature;// Child tries.std::unordered_map<std::string, MyTrie*> children;// If it's a end node of a signal's name, the signal id of which.size_t id = 0;char m_delimiter;public:MyTrie(char delimiter = '.') : m_delimiter(delimiter){}~MyTrie() {  // free every child recursivelyfor (auto p : children) delete p.second;}// Puts a signal id onto this tree by signal name.void Put(std::string_view name, size_t id) {std::vector<std::string> parts;split(name, parts, m_delimiter);auto t = this;  // t is the node walked throughfor (const auto& p : parts) {// Creates a node if not exist.if (auto [it, inserted] = t->children.try_emplace(p, nullptr); inserted) it->second = new MyTrie();// Mark this signal id to its signature.t->signature[id] = 1;t = t->children[p];}// The last node.t->id = id;}// Match signals by given pattern, returns a signature of matched signal ids.Signature<N> Match(std::string_view pattern) const {Signature<N> sig;std::vector<std::string> parts;split(pattern, parts, m_delimiter);auto t = this;for (const auto& p : parts) {// matches all under the subtreeif (p == "*")return t->signature;else {  // match by exact name// match failure, returns empty signatureif (t->children.find(p) == t->children.end()) return sig;t = t->children.at(p);}}// The last node, matches a single signal.sig[t->id] = 1;return sig;}
};int test()
{// 基础添加查找,单个字符为一组,进行字符串词典构建Trie* t = new Trie();t->insertWord("ARM");t->insertWord("DO");t->insertWord("TIME");cout << "Present or Not " << t->searchWord("TIM") << endl;  // Present or Not 0cout << "Present or Not " << t->searchWord("TIME") << endl; // Present or Not 1// 扩展模式匹配,单个字符串为一组,进行字符串词典构建MyTrie<1024> trie;trie.Put("ab.cd.ef", 1);trie.Put("ab.cd.kk", 2);trie.Put("ab.xy.zz", 3);trie.Put("tt.xx", 4);trie.Put("ab.cd", 5);auto m1 = trie.Match("ab.cd.ef");   // m1.count() == 1auto m2 = trie.Match("ab.cd.*");   // m2.count() == 2// 字体查找MyTrie<1024> fontlist(' ');std::vector<std::string> familys = {"Noto Sans SC", "Noto Sans ", "Noto Sans Regular", "Noto Sans Bold", "Noto Sans Italic"};int i = 0;for (const auto& family : familys) {fontlist.Put(family, i++);}auto findList = fontlist.Match("Noto Sans *");for (int i = 0; i < familys.size(); i++) {if (findList[i]) {std::cout << familys[i] << std::endl;}}return 0;
}
输出

Present or Not 0
Present or Not 1
Noto Sans SC
Noto Sans
Noto Sans Regular
Noto Sans Bold
Noto Sans Italic


创作不易,小小的支持一下吧!

http://www.lryc.cn/news/406030.html

相关文章:

  • dockerfile部署wordpress
  • CSS(二)——CSS 背景
  • 开机出现grub无法进入系统_电脑开机出现grub解决方法
  • uboot 设置bootargs配置内核网络挂载根文件系统
  • Vue3+.NET6前后端分离式管理后台实战(三十一)
  • 22集 如何minimax密钥和groupid-《MCU嵌入式AI开发笔记》
  • 决策树的概念
  • C++《类和对象》(中)
  • SpringBoot中JSR303校验
  • 图像数据增强方法概述
  • 【学习笔记】无人机系统(UAS)的连接、识别和跟踪(五)-无人机跟踪
  • 分享从零开始学习网络设备配置--任务6.1 实现计算机的安全接入
  • 双向链表(C语言版)
  • 【算法/学习】前缀和差分
  • idea Project 不显示文件和目录
  • Linux--Socket编程预备
  • 100个python的基本语法知识【下】
  • Git如何将一个分支上的修改转移到另一个分支
  • jvm-证明cpu指令是乱序执行的案例
  • 《流程引擎原理与实践》开源电子书
  • 谷粒商城实战笔记-52~53-商品服务-API-三级分类-新增-修改
  • uni-app 影视类小程序开发从零到一 | 开源项目分享
  • Python使用正则替换字符串
  • 每日一练,java03
  • 【机器学习】深入理解损失函数(Loss Functions)
  • python实现特征检测算法3
  • 软件更新的双刃剑:从”微软蓝屏”事件看网络安全的挑战与对策
  • Redis 主从搭建
  • LeetCode 129, 133, 136
  • macOS 环境Qt Creator 快捷键