{"id":1349,"date":"2024-02-05T04:12:19","date_gmt":"2024-02-04T20:12:19","guid":{"rendered":"http:\/\/www.forillusion.com\/?p=1349"},"modified":"2025-02-14T11:39:10","modified_gmt":"2025-02-14T03:39:10","slug":"trie-tree","status":"publish","type":"post","link":"https:\/\/www.forillusion.com\/index.php\/trie-tree\/","title":{"rendered":"\u5b57\u5178\u6811Trie Tree"},"content":{"rendered":"\n<p><div class=\"has-toc have-toc\"><\/div><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6a21\u7248<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>struct trie\n{\n    int next&#91;26];   \/\/\u6307\u5411\u5b50\u8282\u70b9\u8282\u70b9\u7684\u4e0b\u6807\uff0c\u9ed8\u8ba4\u503c\u4e3a0\u5219\u8868\u793a\u6ca1\u6709\u5b50\u8282\u70b9\uff0c\u5e76\u75280-25\u4ee3\u8868'a'-'z'\n    int isEnd;   \/\/\u662f\u5426\u4e3a\u4e00\u4e2a\u5355\u8bcd\u7684\u7ed3\u5c3e\n}tree&#91;500005];\nint inc;   \/\/\u6700\u540e\u4e00\u4e2a\u5206\u914d\u7684\u5b57\u5178\u6811\u4e0b\u6807\uff0c\u7528\u4e8e\u5206\u914d\u65b0\u7684\u8282\u70b9\u4e0b\u6807\n\nint insert(string s)   \/\/\u63d2\u5165\u5355\u8bcd\n{\n    int p=0;   \/\/\u4e34\u65f6\u6307\u9488\uff0c\u6307\u5411\u5b50\u8282\u70b9\uff0c\u521d\u59cb\u6307\u54110\uff0c\u5373\u6811\u7684\u6839\n    for (int i=0;i&lt;s.size();i++)   \/\/\u904d\u5386\u5355\u8bcd\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\n    {\n        if (tree&#91;p].next&#91;s&#91;i]-97]) p=tree&#91;p].next&#91;s&#91;i]-97];    \/\/\u5982\u679c\u5f53\u524d\u8282\u70b9\u5b58\u5728\u5b57\u6bcd\u4e3as&#91;i]\u7684\u5b50\u8282\u70b9\uff0c\u5219\u5c06p\u6307\u5411\u8be5\u5b50\u8282\u70b9\n        else   \/\/\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u8be5\u8282\u70b9\n        {\n            inc++;   \/\/\u5206\u914d\u65b0\u7684\u4e0b\u6807\n            tree&#91;p].next&#91;s&#91;i]-97]=inc;   \/\/\u5c06\u5f53\u524d\u8282\u70b9\u5b57\u6bcd\u4e3as&#91;i]\u7684\u5b50\u8282\u70b9\u6307\u5411\u65b0\u8282\u70b9\n            p=inc;   \/\/\u5c06\u4e34\u65f6\u6307\u9488\u6307\u5411\u65b0\u8282\u70b9\n        }\n    }\n    if (tree&#91;p].isEnd) return 1;   \/\/\u904d\u5386\u5b8c\u5355\u8bcd\u540e\uff0c\u6307\u9488p\u5c06\u6307\u5411\u5355\u8bcd\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u6240\u5728\u7684\u8282\u70b9\uff0c\u5224\u65ad\u8be5\u8282\u70b9\u7684\u5355\u8bcd\u672b\u5c3e\u6807\u8bb0\u662f\u5426\u4e3a1\uff0c\u5982\u679c\u662f\u5219\u8bf4\u660e\u4e4b\u524d\u6dfb\u52a0\u8fc7\u8be5\u5355\u8bcd\u4e86\uff0c\u8fd4\u56de1\u3002\n    else tree&#91;p].isEnd=1;   \/\/\u5426\u5219\u8bf4\u660e\u4e4b\u524d\u6ca1\u6dfb\u52a0\u8fc7\u8be5\u5355\u8bcd\uff0c\u5c06\u5176\u7684\u5355\u8bcd\u672b\u5c3e\u6807\u8bb0\u7f6e\u4e3a1\uff0c\u7136\u540e\u8fd4\u56de0\u3002\n    return 0;\n}\n\nint check(string s)   \/\/\u68c0\u67e5\u5355\u8bcd\u662f\u5426\u5b58\u5728\n{\n    int p=0;   \/\/\u4e34\u65f6\u6307\u9488\uff0c\u6307\u5411\u5b50\u8282\u70b9\uff0c\u521d\u59cb\u6307\u54110\uff0c\u5373\u6811\u7684\u6839\n    for (int i=0;i&lt;s.size();i++)   \/\/\u904d\u5386\u5355\u8bcd\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\n    {\n        if (!tree&#91;p].next&#91;s&#91;i]-97]) return 0;   \/\/\u5982\u679c\u5f53\u524d\u8282\u70b9\u4e0d\u5b58\u5728\u5b57\u6bcd\u4e3as&#91;i]\u7684\u5b50\u8282\u70b9\uff0c\u5219\u8868\u793a\u8be5\u5355\u8bcd\u4e0d\u5b58\u5728\uff0c\u8fd4\u56de0\n        p=tree&#91;p].next&#91;s&#91;i]-97];   \/\/\u5c06\u4e34\u65f6\u6307\u9488\u6307\u5411\u5b57\u6bcd\u4e3as&#91;i]\u7684\u5b50\u8282\u70b9\n    }\n    if (tree&#91;p].isEnd) return 1;   \/\/\u904d\u5386\u5b8c\u5355\u8bcd\u540e\uff0c\u6307\u9488p\u5c06\u6307\u5411\u5355\u8bcd\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u6240\u5728\u7684\u8282\u70b9\uff0c\u5224\u65ad\u8be5\u8282\u70b9\u7684\u5355\u8bcd\u672b\u5c3e\u6807\u8bb0\u662f\u5426\u4e3a1\uff0c\u5982\u679c\u662f\u5219\u8bf4\u660e\u8be5\u5355\u8bcd\u5b58\u5728\uff0c\u8fd4\u56de1\uff0c\u5426\u5219\u8fd4\u56de0\u3002\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u91ca<\/h2>\n\n\n\n<p>\u5b57\u5178\u6811\uff0c\u662f\u4e00\u79cd<strong>\u7a7a\u95f4\u6362\u65f6\u95f4<\/strong>\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53c8\u79f0Trie\u6811\u3001\u524d\u7f00\u6811\uff0c\u662f\u4e00\u79cd\u6811\u5f62\u7ed3\u6784\uff0c\u5178\u578b\u7528\u4e8e\u7edf\u8ba1\u3001\u6392\u5e8f\u3001\u548c\u4fdd\u5b58\u5927\u91cf\u5b57\u7b26\u4e32\u3002\u5229\u7528\u5b57\u7b26\u4e32\u7684\u516c\u5171\u524d\u7f00\u6765\u51cf\u5c11\u67e5\u8be2\u65f6\u95f4\uff0c\u6700\u5927\u9650\u5ea6\u5730\u51cf\u5c11\u4e0d\u9700\u8981\u7684\u5b57\u7b26\u4e32\u6bd4\u8f83\uff0c\u67e5\u8be2\u6548\u7387\u6bd4\u54c8\u5e0c\u6811\u9ad8\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/pic1.zhimg.com\/80\/v2-c6c00f279fb8a978ba6a7b08820e92e8_720w.webp\" src=\"https:\/\/cdn.forillusion.com\/moezx\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  alt=\"\" width=\"479\" height=\"332\"\/><\/figure >\n<noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/pic1.zhimg.com\/80\/v2-c6c00f279fb8a978ba6a7b08820e92e8_720w.webp\" alt=\"\" width=\"479\" height=\"332\"\/><\/figure><\/noscript>\n\n\n\n<p>\u6839\u8282\u70b9\u4e0d\u5305\u542b\u5b57\u7b26\uff0c\u9664\u6839\u8282\u70b9\u5916\u6bcf\u4e00\u4e2a\u8282\u70b9\u90fd\u53ea\u5305\u542b\u4e00\u4e2a\u5b57\u7b26\u3002<br>\u4ece\u6839\u8282\u70b9\u5230\u67d0\u4e00\u8282\u70b9\uff0c\u8def\u5f84\u4e0a\u7ecf\u8fc7\u7684\u5b57\u7b26\u8fde\u63a5\u8d77\u6765\uff0c\u4e3a\u8be5\u8282\u70b9\u5bf9\u5e94\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>\u5b9a\u4e49\u7ed3\u6784\u4f53\uff1a\u8be5\u7ed3\u6784\u4f53\u7528\u6765\u5b58\u50a8\u6811\u7684\u6bcf\u4e00\u4e2a\u8282\u70b9\uff0c\u6bcf\u4e2a\u8282\u70b9\u90fd\u5305\u62ec\u4e86\u6307\u5411\u5b50\u8282\u70b9\u7684\u6307\u9488\uff0c\u4ee5\u53ca\u5f53\u524d\u8282\u70b9\u662f\u5426\u4e3a\u5355\u8bcd\u7684\u7ed3\u5c3e\u3002<br>\u7528\u6570\u7ec4\u7ebf\u6027\u5b58\u50a8\u6811\uff0c\u6240\u4ee5\u6307\u9488\u8bb0\u5f55\u7684\u662f\u5b50\u8282\u70b9\u7684\u6570\u7ec4\u4e0b\u6807\u503c\u3002<br>inc\u8bb0\u5f55\u7684\u662f\u5f53\u524d\u7684\u6570\u7ec4\u4e0b\u6807\u5df2\u7ecf\u5206\u914d\u5230\u591a\u5c11\u4e86\uff0c\u7528\u4e8e\u5206\u914d\u65b0\u7684\u8282\u70b9\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>struct trie\n{\n    int next&#91;26];   \/\/\u6307\u5411\u5b50\u8282\u70b9\u8282\u70b9\u7684\u4e0b\u6807\uff0c\u9ed8\u8ba4\u503c\u4e3a0\u5219\u8868\u793a\u6ca1\u6709\u5b50\u8282\u70b9\uff0c\u5e76\u75280-25\u4ee3\u8868'a'-'z'\n    int isEnd;   \/\/\u662f\u5426\u4e3a\u4e00\u4e2a\u5355\u8bcd\u7684\u7ed3\u5c3e\n}tree&#91;500005];\nint inc;   \/\/\u6700\u540e\u4e00\u4e2a\u5206\u914d\u7684\u5b57\u5178\u6811\u4e0b\u6807\uff0c\u7528\u4e8e\u5206\u914d\u65b0\u7684\u8282\u70b9\u4e0b\u6807<\/code><\/pre>\n\n\n\n<p>\u63d2\u5165\u5355\u8bcd\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"cpp\">int insert(string s)   \/\/\u63d2\u5165\u5355\u8bcd\n{\n    int p=0;   \/\/\u4e34\u65f6\u6307\u9488\uff0c\u6307\u5411\u5b50\u8282\u70b9\uff0c\u521d\u59cb\u6307\u54110\uff0c\u5373\u6811\u7684\u6839\n    for (int i=0;i&lt;s.size();i++)   \/\/\u904d\u5386\u5355\u8bcd\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\n    {\n        if (tree&#91;p].next&#91;s&#91;i]-97]) p=tree&#91;p].next&#91;s&#91;i]-97];    \/\/\u5982\u679c\u5f53\u524d\u8282\u70b9\u5b58\u5728\u5b57\u6bcd\u4e3as&#91;i]\u7684\u5b50\u8282\u70b9\uff0c\u5219\u5c06p\u6307\u5411\u8be5\u5b50\u8282\u70b9\n        else   \/\/\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u521b\u5efa\u8be5\u8282\u70b9\n        {\n            inc++;   \/\/\u5206\u914d\u65b0\u7684\u4e0b\u6807\n            tree&#91;p].next&#91;s&#91;i]-97]=inc;   \/\/\u5c06\u5f53\u524d\u8282\u70b9\u5b57\u6bcd\u4e3as&#91;i]\u7684\u5b50\u8282\u70b9\u6307\u5411\u65b0\u8282\u70b9\n            p=inc;   \/\/\u5c06\u4e34\u65f6\u6307\u9488\u6307\u5411\u65b0\u8282\u70b9\n        }\n    }\n    if (tree&#91;p].isEnd) return 1;   \/\/\u904d\u5386\u5b8c\u5355\u8bcd\u540e\uff0c\u6307\u9488p\u5c06\u6307\u5411\u5355\u8bcd\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u6240\u5728\u7684\u8282\u70b9\uff0c\u5224\u65ad\u8be5\u8282\u70b9\u7684\u5355\u8bcd\u672b\u5c3e\u6807\u8bb0\u662f\u5426\u4e3a1\uff0c\u5982\u679c\u662f\u5219\u8bf4\u660e\u4e4b\u524d\u6dfb\u52a0\u8fc7\u8be5\u5355\u8bcd\u4e86\uff0c\u8fd4\u56de1\u3002\n    else tree&#91;p].isEnd=1;   \/\/\u5426\u5219\u8bf4\u660e\u4e4b\u524d\u6ca1\u6dfb\u52a0\u8fc7\u8be5\u5355\u8bcd\uff0c\u5c06\u5176\u7684\u5355\u8bcd\u672b\u5c3e\u6807\u8bb0\u7f6e\u4e3a1\uff0c\u7136\u540e\u8fd4\u56de0\u3002\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u68c0\u67e5\u5355\u8bcd\u662f\u5426\u5b58\u5728\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"cpp\">int check(string s)   \/\/\u68c0\u67e5\u5355\u8bcd\u662f\u5426\u5b58\u5728\n{\n    int p=0;   \/\/\u4e34\u65f6\u6307\u9488\uff0c\u6307\u5411\u5b50\u8282\u70b9\uff0c\u521d\u59cb\u6307\u54110\uff0c\u5373\u6811\u7684\u6839\n    for (int i=0;i&lt;s.size();i++)   \/\/\u904d\u5386\u5355\u8bcd\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\n    {\n        if (!tree&#91;p].next&#91;s&#91;i]-97]) return 0;   \/\/\u5982\u679c\u5f53\u524d\u8282\u70b9\u4e0d\u5b58\u5728\u5b57\u6bcd\u4e3as&#91;i]\u7684\u5b50\u8282\u70b9\uff0c\u5219\u8868\u793a\u8be5\u5355\u8bcd\u4e0d\u5b58\u5728\uff0c\u8fd4\u56de0\n        p=tree&#91;p].next&#91;s&#91;i]-97];   \/\/\u5c06\u4e34\u65f6\u6307\u9488\u6307\u5411\u5b57\u6bcd\u4e3as&#91;i]\u7684\u5b50\u8282\u70b9\n    }\n    if (tree&#91;p].isEnd) return 1;   \/\/\u904d\u5386\u5b8c\u5355\u8bcd\u540e\uff0c\u6307\u9488p\u5c06\u6307\u5411\u5355\u8bcd\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u6240\u5728\u7684\u8282\u70b9\uff0c\u5224\u65ad\u8be5\u8282\u70b9\u7684\u5355\u8bcd\u672b\u5c3e\u6807\u8bb0\u662f\u5426\u4e3a1\uff0c\u5982\u679c\u662f\u5219\u8bf4\u660e\u8be5\u5355\u8bcd\u5b58\u5728\uff0c\u8fd4\u56de1\uff0c\u5426\u5219\u8fd4\u56de0\u3002\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6a21\u7248 \u89e3\u91ca \u5b57\u5178\u6811\uff0c\u662f\u4e00\u79cd\u7a7a\u95f4\u6362\u65f6\u95f4\u7684\u6570\u636e\u7ed3\u6784\uff0c\u53c8\u79f0Trie\u6811\u3001\u524d\u7f00\u6811\uff0c\u662f\u4e00\u79cd\u6811\u5f62\u7ed3\u6784\uff0c\u5178\u578b\u7528\u4e8e\u7edf\u8ba1\u3001\u6392\u5e8f\u3001\u548c\u4fdd\u5b58\u5927\u91cf\u5b57\u7b26\u4e32\u3002\u5229\u7528 &#8230;<\/p>","protected":false},"author":1,"featured_media":1351,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,35],"tags":[43,12,34,22],"class_list":["post-1349","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-3","category-35","tag-43","tag-12","tag-34","tag-22"],"_links":{"self":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1349","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/comments?post=1349"}],"version-history":[{"count":1,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1349\/revisions"}],"predecessor-version":[{"id":1733,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1349\/revisions\/1733"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media\/1351"}],"wp:attachment":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media?parent=1349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/categories?post=1349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/tags?post=1349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}