{"id":47218,"date":"2024-12-02T15:28:50","date_gmt":"2024-12-02T07:28:50","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47218\/"},"modified":"2024-12-02T15:28:50","modified_gmt":"2024-12-02T07:28:50","slug":"php-%e5%87%bd%e6%95%b0%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e9%80%92%e5%bd%92%e6%9d%a5%e5%ae%9e%e7%8e%b0-trie-%e6%a0%91%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47218\/","title":{"rendered":"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0 Trie \u6811\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0 Trie \u6811\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aPHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0 Trie \u6811\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p>\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0 Trie \u6811\uff1a\u521b\u5efa Trie \u6811\uff1a\u9012\u5f52\u521b\u5efa\u5b50\u8282\u70b9\uff0c\u76f4\u81f3\u5355\u8bcd\u672b\u5c3e\u3002\u641c\u7d22 Trie \u6811\uff1a\u9012\u5f52\u904d\u5386\u8282\u70b9\uff0c\u76f4\u81f3\u5355\u8bcd\u672b\u5c3e\u6216\u8282\u70b9\u4e0d\u5b58\u5728\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729905206671c42361f497.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0 Trie \u6811\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0 Trie \u6811\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728 PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0 Trie \u6811<\/strong><\/p>\n<p><strong>\u4ecb\u7ecd<\/strong><\/p>\n<p>Trie \u6811\uff0c\u4e5f\u79f0\u4e3a\u524d\u7f00\u6811\u6216\u5355\u8bcd\u67e5\u627e\u6811\uff0c\u662f\u4e00\u79cd\u7528\u4e8e\u9ad8\u6548\u5b58\u50a8\u548c\u67e5\u627e\u5b57\u7b26\u4e32\u7684\u6811\u5f62\u6570\u636e\u7ed3\u6784\u3002\u5b83\u7279\u522b\u9002\u7528\u4e8e\u5339\u914d\u67e5\u627e\uff0c\u5373\u67e5\u627e\u5305\u542b\u7279\u5b9a\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u672c\u6587\u5c06\u5411\u60a8\u5c55\u793a\u5982\u4f55\u5728 PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0 Trie \u6811\u3002<\/p>\n<p><strong>\u9012\u5f52\u7b97\u6cd5<\/strong><\/p>\n<p>\u9012\u5f52\u662f\u4e00\u79cd\u89e3\u51b3\u95ee\u9898\u7684\u6280\u672f\uff0c\u5176\u4e2d\u51fd\u6570\u4f1a\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u66f4\u7b80\u5355\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230\u8fbe\u5230\u57fa\u672c\u60c5\u51b5\u3002\u5bf9\u4e8e Trie \u6811\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u6765\u521b\u5efa\u548c\u641c\u7d22\u6811\u5f62\u7ed3\u6784\u3002<\/p>\n<p><strong>\u521b\u5efa Trie \u6811<\/strong><\/p>\n<p>\u521b\u5efa Trie \u6811\u7684\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<pre>function createTrie(array $words): TrieNode\n{\n    $root = new TrieNode(''); \/\/ \u521b\u5efa\u6839\u8282\u70b9\n\n    foreach ($words as $word) {\n        $node = $root;\n        for ($i = 0; $i &lt; strlen($word); $i++) {\n            $letter = $word[$i];\n            if (!isset($node-&gt;children[$letter])) {\n                $node-&gt;children[$letter] = new TrieNode($letter);\n            }\n            $node = $node-&gt;children[$letter];\n        }\n        $node-&gt;endOfWord = true;\n    }\n\n    return $root;\n}<\/pre>\n<p><strong>\u641c\u7d22 Trie \u6811<\/strong><\/p>\n<p>\u641c\u7d22 Trie \u6811\u7684\u8fc7\u7a0b\u5982\u4e0b\uff1a<\/p>\n<pre>function searchTrie(TrieNode $root, string $word): bool\n{\n    $node = $root;\n    for ($i = 0; $i &lt; strlen($word); $i++) {\n        $letter = $word[$i];\n        if (!isset($node-&gt;children[$letter])) {\n            return false;\n        }\n        $node = $node-&gt;children[$letter];\n    }\n\n    return $node-&gt;endOfWord;\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4ee5\u4e0b\u5355\u8bcd\u5217\u8868\uff1a<\/p>\n<pre>$words = ['apple', 'banana', 'cherry', 'dog', 'fish'];<\/pre>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>createTrie<\/code> \u51fd\u6570\u521b\u5efa\u4e00\u4e2a Trie \u6811\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>$root = createTrie($words);<\/pre>\n<p>\u7136\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>searchTrie<\/code> \u51fd\u6570\u641c\u7d22\u5355\u8bcd\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>if (searchTrie($root, 'apple')) {\n    echo \"\u627e\u5230\u4e86 'apple'\\n\";\n} else {\n    echo \"\u672a\u627e\u5230 'apple'\\n\";\n}<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>\u627e\u5230\u4e86 'apple'<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0 Trie \u6811\uff1f \u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300aPHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0 Trie \u6811\uff1f\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~ \u4f7f\u7528\u9012\u5f52\u5b9e\u73b0 Trie \u6811\uff1a\u521b\u5efa Trie \u6811\uff1a\u9012\u5f52\u521b\u5efa\u5b50\u8282\u70b9\uff0c\u76f4\u81f3\u5355\u8bcd\u672b\u5c3e\u3002\u641c\u7d22 Trie \u6811\uff1a\u9012\u5f52\u904d\u5386\u8282\u70b9\uff0c\u76f4\u81f3\u5355\u8bcd\u672b\u5c3e\u6216\u8282\u70b9\u4e0d\u5b58\u5728\u3002 \u5728 PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u9012\u5f52\u5b9e\u73b0 Trie \u6811 \u4ecb\u7ecd Trie \u6811\uff0c\u4e5f\u79f0\u4e3a\u524d\u7f00\u6811\u6216\u5355\u8bcd\u67e5\u627e\u6811\uff0c\u662f\u4e00\u79cd\u7528\u4e8e\u9ad8\u6548\u5b58\u50a8\u548c\u67e5\u627e\u5b57\u7b26\u4e32\u7684\u6811\u5f62\u6570\u636e\u7ed3\u6784\u3002\u5b83\u7279\u522b\u9002\u7528\u4e8e\u5339\u914d\u67e5\u627e\uff0c\u5373\u67e5\u627e\u5305\u542b\u7279\u5b9a\u6a21\u5f0f\u7684\u5b57\u7b26\u4e32\u3002\u672c\u6587\u5c06\u5411\u60a8\u5c55\u793a\u5982\u4f55\u5728 PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u9012\u5f52\u6765\u5b9e\u73b0 Trie \u6811\u3002 \u9012\u5f52\u7b97\u6cd5 \u9012\u5f52\u662f\u4e00\u79cd\u89e3\u51b3\u95ee\u9898\u7684\u6280\u672f\uff0c\u5176\u4e2d\u51fd\u6570\u4f1a\u8c03\u7528\u81ea\u8eab\u6765\u89e3\u51b3\u66f4\u7b80\u5355\u7684\u5b50\u95ee\u9898\uff0c\u76f4\u5230\u8fbe\u5230\u57fa\u672c\u60c5\u51b5\u3002\u5bf9\u4e8e Trie \u6811\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u6765\u521b\u5efa\u548c\u641c\u7d22\u6811\u5f62\u7ed3\u6784\u3002 \u521b\u5efa Trie \u6811 \u521b\u5efa Trie \u6811\u7684\u8fc7\u7a0b\u5982\u4e0b\uff1a function createTrie(array $words): TrieNode { $root = new TrieNode(&#8221;); \/\/ \u521b\u5efa\u6839\u8282\u70b9 foreach ($words as $word) { $node = [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47218","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47218","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47218"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47218\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47218"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47218"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47218"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}