{"id":518,"date":"2023-04-15T15:51:12","date_gmt":"2023-04-15T07:51:12","guid":{"rendered":"http:\/\/120.55.184.7\/?p=518"},"modified":"2025-04-27T17:59:54","modified_gmt":"2025-04-27T09:59:54","slug":"trie-%e5%ad%97%e5%85%b8%e6%a0%91-%e5%89%8d%e7%bc%80%e6%a0%91","status":"publish","type":"post","link":"https:\/\/beijian99.top\/?p=518","title":{"rendered":"Trie \u5b57\u5178\u6811\/\u524d\u7f00\u6811"},"content":{"rendered":"\n<p>\u524d\u7f00\u6811\uff08Trie\uff0c\u53c8\u79f0\u5b57\u5178\u6811\uff09\u662f\u4e00\u79cd\u9ad8\u6548\u5904\u7406\u5b57\u7b26\u4e32\u7684\u6811\u5f62\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e<strong>\u524d\u7f00\u5339\u914d\u3001\u81ea\u52a8\u8865\u5168\u3001\u654f\u611f\u8bcd\u8fc7\u6ee4<\/strong>\u7b49\u573a\u666f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u6838\u5fc3\u601d\u60f3<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u8282\u70b9\u7ed3\u6784<\/strong>\uff1a\u6bcf\u4e2a\u8282\u70b9\u5b58\u50a8\u5b50\u8282\u70b9\u6307\u9488\uff08\u5982\u6570\u7ec4\u6216\u54c8\u5e0c\u8868\uff09\u548c\u7ed3\u675f\u6807\u8bb0\uff08<code>isEnd<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u8def\u5f84\u5171\u4eab<\/strong>\uff1a\u76f8\u540c\u524d\u7f00\u7684\u5b57\u7b26\u4e32\u5171\u4eab\u8def\u5f84\uff0c\u8282\u7701\u7a7a\u95f4\u3002<\/li>\n\n\n\n<li><strong>\u65f6\u95f4\u590d\u6742\u5ea6<\/strong>\uff1a\u63d2\u5165\u548c\u67e5\u8be2\u5747\u4e3aO(L)\uff08L\u4e3a\u5b57\u7b26\u4e32\u957f\u5ea6\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. C++\u5b9e\u73b0\u4ee3\u7801<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u57fa\u7840\u7248\u672c\uff08\u652f\u6301\u5c0f\u5199\u5b57\u6bcd\uff09<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;vector&gt;\n#include &lt;string&gt;\nusing namespace std;\n\nclass Trie {\nprivate:\n    struct TrieNode {\n        vector&lt;TrieNode*&gt; children;\n        bool isEnd;\n        TrieNode() : children(26, nullptr), isEnd(false) {}\n    };\n    TrieNode* root;\n\npublic:\n    Trie() : root(new TrieNode()) {}\n\n    void insert(const string&amp; word) {\n        TrieNode* node = root;\n        for (char c : word) {\n            int idx = c - &#039;a&#039;;\n            if (!node-&gt;children&#x5B;idx]) {\n                node-&gt;children&#x5B;idx] = new TrieNode();\n            }\n            node = node-&gt;children&#x5B;idx];\n        }\n        node-&gt;isEnd = true;\n    }\n\n    bool search(const string&amp; word) {\n        TrieNode* node = findNode(word);\n        return node &amp;&amp; node-&gt;isEnd;\n    }\n\n    bool startsWith(const string&amp; prefix) {\n        return findNode(prefix) != nullptr;\n    }\n\nprivate:\n    TrieNode* findNode(const string&amp; s) {\n        TrieNode* node = root;\n        for (char c : s) {\n            int idx = c - &#039;a&#039;;\n            if (!node-&gt;children&#x5B;idx]) return nullptr;\n            node = node-&gt;children&#x5B;idx];\n        }\n        return node;\n    }\n};\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\"><strong>\u4f18\u5316\u7248\u672c\uff08\u652f\u6301\u4efb\u610f\u5b57\u7b26\uff0c\u4f7f\u7528\u54c8\u5e0c\u8868\uff09<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\n#include &lt;unordered_map&gt;\nclass Trie {\nprivate:\n    struct TrieNode {\n        unordered_map&lt;char, TrieNode*&gt; children;\n        bool isEnd;\n        TrieNode() : isEnd(false) {}\n    };\n    TrieNode* root;\n\npublic:\n    \/\/ \u65b9\u6cd5\u540c\u4e0a\uff0c\u4ec5\u9700\u4fee\u6539\u5b57\u7b26\u5904\u7406\u903b\u8f91\n    void insert(const string&amp; word) {\n        TrieNode* node = root;\n        for (char c : word) {\n            if (!node-&gt;children.count(c)) {\n                node-&gt;children&#x5B;c] = new TrieNode();\n            }\n            node = node-&gt;children&#x5B;c];\n        }\n        node-&gt;isEnd = true;\n    }\n    \/\/ search\u548cstartsWith\u65b9\u6cd5\u7c7b\u4f3c\u8c03\u6574\n};\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u529f\u80fd\u6269\u5c55<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u7edf\u8ba1\u524d\u7f00\u51fa\u73b0\u6b21\u6570<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nstruct TrieNode {\n    int pass; \/\/ \u7ecf\u8fc7\u8be5\u8282\u70b9\u7684\u5b57\u7b26\u4e32\u6570\n    int end;  \/\/ \u4ee5\u8be5\u8282\u70b9\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\u6570\n    \/\/ ...\u5176\u4ed6\u6210\u5458\n};\n\nvoid insert(const string&amp; word) {\n    TrieNode* node = root;\n    node-&gt;pass++;\n    for (char c : word) {\n        int idx = c - &#039;a&#039;;\n        if (!node-&gt;children&#x5B;idx]) {\n            node-&gt;children&#x5B;idx] = new TrieNode();\n        }\n        node = node-&gt;children&#x5B;idx];\n        node-&gt;pass++;\n    }\n    node-&gt;end++;\n}\n\nint countPrefix(const string&amp; prefix) {\n    TrieNode* node = findNode(prefix);\n    return node ? node-&gt;pass : 0;\n}\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5220\u9664\u64cd\u4f5c<\/strong><\/h4>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nvoid remove(const string&amp; word) {\n    if (!search(word)) return;\n    TrieNode* node = root;\n    node-&gt;pass--;\n    for (char c : word) {\n        int idx = c - &#039;a&#039;;\n        if (--node-&gt;children&#x5B;idx]-&gt;pass == 0) {\n            delete node-&gt;children&#x5B;idx];\n            node-&gt;children&#x5B;idx] = nullptr;\n            return;\n        }\n        node = node-&gt;children&#x5B;idx];\n    }\n    node-&gt;end--;\n}\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u5e94\u7528\u573a\u666f<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u81ea\u52a8\u8865\u5168<\/strong>\uff1a\u8f93\u5165\u524d\u7f00\u65f6\u5feb\u901f\u5339\u914d\u5019\u9009\u8bcd\u3002<\/li>\n\n\n\n<li><strong>\u654f\u611f\u8bcd\u8fc7\u6ee4<\/strong>\uff1a\u6784\u5efa\u654f\u611f\u8bcdTrie\u6811\uff0c\u9ad8\u6548\u68c0\u6d4b\u6587\u672c\u3002<\/li>\n\n\n\n<li><strong>\u8bcd\u9891\u7edf\u8ba1<\/strong>\uff1a\u7edf\u8ba1\u5927\u91cf\u5b57\u7b26\u4e32\u7684\u51fa\u73b0\u6b21\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5. \u590d\u6742\u5ea6\u5206\u6790<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7a7a\u95f4<\/strong>\uff1aO(N * L)$\uff08N\u4e3a\u5b57\u7b26\u4e32\u6570\u91cf\uff0cL\u4e3a\u5e73\u5747\u957f\u5ea6\uff09\u3002<\/li>\n\n\n\n<li><strong>\u65f6\u95f4<\/strong>\uff1a\u63d2\u5165\u3001\u67e5\u8be2\u3001\u5220\u9664\u5747\u4e3aO(L)\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u53c2\u8003\u5b9e\u73b0\uff1a<a href=\"https:\/\/leetcode.cn\/problems\/implement-trie-prefix-tree\/\">LeetCode 208. \u5b9e\u73b0 Trie<\/a>\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u7f00\u6811\uff08Trie\uff0c\u53c8\u79f0\u5b57\u5178\u6811\uff09\u662f\u4e00\u79cd\u9ad8\u6548\u5904\u7406\u5b57\u7b26\u4e32\u7684\u6811\u5f62\u6570\u636e\u7ed3\u6784\uff0c\u9002\u7528\u4e8e\u524d\u7f00\u5339\u914d\u3001\u81ea\u52a8\u8865\u5168\u3001\u654f\u611f\u8bcd\u8fc7\u6ee4\u7b49\u573a\u666f\u3002 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[4],"tags":[],"class_list":["post-518","post","type-post","status-publish","format-standard","hentry","category-algorithm"],"_links":{"self":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=518"}],"version-history":[{"count":2,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/518\/revisions"}],"predecessor-version":[{"id":805,"href":"https:\/\/beijian99.top\/index.php?rest_route=\/wp\/v2\/posts\/518\/revisions\/805"}],"wp:attachment":[{"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/beijian99.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}