{"id":449,"date":"2021-03-11T16:10:16","date_gmt":"2021-03-11T08:10:16","guid":{"rendered":"\/?p=449"},"modified":"2021-03-11T16:10:16","modified_gmt":"2021-03-11T08:10:16","slug":"6-1%e9%9c%8d%e5%a4%ab%e6%9b%bc%e5%8e%8b%e7%bc%a9","status":"publish","type":"post","link":"http:\/\/xinblog.ltd\/?p=449","title":{"rendered":"6.1\u970d\u592b\u66fc\u538b\u7f29"},"content":{"rendered":"<p>\u653e\u5f03\u539f\u6709\u7684\u4e8c\u8fdb\u5236\u6765\u8868\u793a\u4e00\u4e2a\u5b57\u7b26,\u800c\u662f\u91c7\u7528\u4e86\u8f83\u5c11\u7684\u6bd4\u7279\u6765\u8868\u793a\u51fa\u73b0\u9891\u7387\u9ad8\u7684\u5b57\u7b26,\u7528\u8f83\u591a\u7684\u6bd4\u7279\u8868\u793a\u51fa\u73b0\u9891\u7387\u4f4e\u7684\u5b57\u7b26<\/p>\n<p>\u5982\u679c\u9700\u8981\u5bf9 ABRACADABRA!\u6765\u7f16\u7801\u7684\u8bdd,\u7b80\u5355\u4e14\u76f4\u63a5\u7684\u8fd0\u7b97\u5c31\u662f\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236,\u4f46\u662f\u8fd9\u6837\u51fa\u73b0\u9891\u7387\u6700\u9ad8\u7684A\u548c\u53ea\u51fa\u73b0\u4e00\u6b21\u7684!\u6240\u5360\u7684\u7a7a\u95f4\u76f8\u540c,\u51fa\u73b0\u65e0\u6cd5\u8bc6\u522b\u7684\u95ee\u9898<\/p>\n<p>\u4e0d\u5982,\u5c06A\u7f16\u7801\u4e3a0,B\u7f16\u7801\u4e3a1,R\u4e3a00,C\u4e3a01,D\u4e3a10,!\u4e3a11.<\/p>\n<p>\u867d\u7136\u4e0d\u5b8c\u6574,\u4f46\u662f\u53ef\u4ee5\u521d\u6b65\u7684\u4f53\u73b0\u6211\u4eec\u7684\u538b\u7f29\u601d\u60f3<\/p>\n<p>\u4e3a\u4e86\u5b8c\u5584\u8fd9\u4e2a\u601d\u60f3,\u5f15\u5165\u4e86\u524d\u7f00\u7801\u8fd9\u4e2a\u6982\u5ff5<\/p>\n<p>\u4f46\u662f,0\u53ef\u4ee5\u8bef\u89e3\u4e3aR,C,D<\/p>\n<p>\u4f46\u5982\u679c\u6211\u4eec\u5c06A\u7f16\u7801\u4e3a0,B\u4e3a1111,C\u4e3a110,D\u4e3a100 ,R\u4e3a1110 ,!\u4e3a101<\/p>\n<p>\u90a3\u4e48\u5c31\u4e0d\u4f1a\u51fa\u73b0\u8bef\u89e3\u8fd9\u4e2a\u53ef\u80fd\u6027\u4e86<\/p>\n<p>\u524d\u7f00\u7801\u53ef\u4ee5\u7528\u6765\u5f62\u6210\u4e00\u4e2a\u5355\u8bcd\u67e5\u627e\u6811<\/p>\n<p>\u53ea\u5229\u7528\u53f6\u5b50\u8282\u70b9\u6765\u8868\u793a\u5b57\u7b26,\u4f46\u4e2d\u95f4\u8282\u70b9\u5e76\u4e0d\u662f\u6ca1\u6709\u542b\u4e49<\/p>\n<p>\u7b80\u5355\u6765\u8bf4,\u5c31\u662f\u5f62\u6210\u4e00\u4e2a\u4ece\u6839\u8282\u70b9\u5230\u8be5\u8282\u70b9\u7684\u8def\u5f84\u8868\u793a\u7684\u6bd4\u7279\u5b57\u7b26\u4e32,\u5de6\u4e3a0,\u53f3\u4e3a1\u7684\u6811<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"490\" height=\"762\" src=\"\/wp-content\/uploads\/2021\/03\/unnamed-file-207.png\" class=\"wp-image-450\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-207.png 490w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-207-193x300.png 193w\" sizes=\"(max-width: 490px) 100vw, 490px\" \/><\/p>\n<p>\u90a3\u4e48,\u6700\u540e\u7684\u6700\u4f18\u7684\u4e14\u6700\u7ecf\u5178\u7684\u6811\u6784\u5efa\u65b9\u6cd5\u88ab\u79f0\u4e3a\u970d\u592b\u66fc\u7f16\u7801<\/p>\n<p>\u5229\u7528\u524d\u7f00\u7801\u8fdb\u884c\u6570\u636e\u538b\u7f29\u9700\u8981\u7ecf\u8fc75\u4e2a\u6b65\u9aa4<\/p>\n<p>\u6784\u9020\u4e00\u9897\u7f16\u7801\u5355\u8bcd\u67e5\u627e\u6811<\/p>\n<p>\u5c06\u8be5\u6811\u4ee5\u5b57\u8282\u6d41\u7684\u5f62\u5f0f\u5199\u5165\u8f93\u5165\u4ee5\u4f9b\u5c55\u5f00\u4f7f\u7528<\/p>\n<p>\u4f7f\u7528\u8be5\u6811\u5c06\u5b57\u8282\u6d41\u7f16\u7801\u4e3a\u6bd4\u7279\u6d41<\/p>\n<p>\u8bfb\u53d6\u5355\u8bcd\u67e5\u627e\u6811<\/p>\n<p>\u4f7f\u7528\u8be5\u6811\u5c06\u6bd4\u7279\u6d41\u89e3\u7801<\/p>\n<p>\u6811\u7684\u7ed3\u70b9\u5e76\u4e0d\u96be,\u53ea\u662f\u7b80\u5355\u7684\u4f7f\u7528\u4e86\u7c7b\u4f3c\u94fe\u8868\u7684\u5185\u90e8\u7c7b\u7ed3\u6784,\u542b\u6709\u5206\u522b\u6307\u5411\u5de6\u548c\u53f3\u4e24\u4e2a\u7ed3\u70b9\u7684Node<\/p>\n<p>private static class Node implements Comparable&lt;Node&gt;<\/p>\n<p>{<\/p>\n<p>private char ch;<\/p>\n<p>private int freq;<\/p>\n<p>private final Node left,right;<\/p>\n<p>}<\/p>\n<p>\u5bf9\u5e94\u7684,\u5982\u679c\u9700\u8981\u5c55\u5f00\u6574\u4e2a\u6811,\u5219\u9700\u8981<\/p>\n<p>\u8c03\u7528\u4e00\u4e2a\u4ece\u6807\u51c6\u8f93\u5165\u83b7\u53d6\u6570\u5b57\u7684\u7684\u65b9\u6cd5,\u7136\u540e\u6839\u636e\u6bd4\u7279\u6d41\u7684\u8f93\u5165\u4ece\u6839\u8282\u70b9\u5f00\u59cb\u79fb\u52a8,\u5982\u679c\u4e3a0\u79fb\u52a8\u5411\u5de6\u5b50\u8282\u70b9,\u5982\u679c\u4e3a1\u79fb\u52a8\u5411\u53f3\u5b50\u8282\u70b9.\u76f4\u5230\u5bfb\u5230\u4e00\u4e2a\u6839\u8282\u70b9<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"384\" height=\"314\" src=\"\/wp-content\/uploads\/2021\/03\/unnamed-file-208.png\" class=\"wp-image-451\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-208.png 384w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-208-300x245.png 300w\" sizes=\"(max-width: 384px) 100vw, 384px\" \/><\/p>\n<p>\u5982\u679c\u9700\u8981\u5bf9\u524d\u7f00\u7801\u8fdb\u884c\u538b\u7f29<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"808\" height=\"390\" src=\"\/wp-content\/uploads\/2021\/03\/unnamed-file-209.png\" class=\"wp-image-452\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-209.png 808w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-209-300x145.png 300w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-209-768x371.png 768w\" sizes=\"(max-width: 808px) 100vw, 808px\" \/><\/p>\n<p>\u7ef4\u62a4\u4e00\u4e2aString\u6570\u7ec4st,\u7136\u540ebuildCode\u904d\u5386\u67e5\u627e\u6811\u4e2d\u5bf9\u5e94\u5b57\u8282<\/p>\n<p>\u5bf9\u4e8e\u4efb\u610f\u7684\u5355\u8bcd\u67e5\u627e\u6811,\u90fd\u80fd\u591f\u4ea7\u751f\u4e00\u4e2a\u5c06\u6811\u4e2d\u7684\u5b57\u7b26\u548c\u6bd4\u7279\u5b57\u7b26\u4e32\u76f8\u5bf9\u5e94\u7684\u7f16\u8bd1\u8868<\/p>\n<p>\u90a3\u4e48\u56f0\u96be\u5c31\u662f,\u5bf9\u4e8e\u5355\u8bcd\u67e5\u627e\u6811\u7684\u521d\u6b21\u6784\u9020<\/p>\n<p>\u6211\u4eec\u5148\u7ef4\u62a4\u521b\u5efa\u4e00\u4e2a\u7531\u8bb8\u591a\u53ea\u6709\u4e00\u4e2a\u7ed3\u70b9\u7684\u6811\u7ec4\u6210\u7684\u68ee\u6797,<\/p>\n<p>\u6bcf\u4e2a\u7ed3\u70b9\u7684Node\u4e2d\u6211\u4eec\u7ef4\u62a4\u4e86\u4e00\u4e2afreq\u7684\u5b9e\u4f8b\u53d8\u91cf\u6765\u8868\u793a\u51fa\u73b0\u7684\u9891\u7387<\/p>\n<p>\u4e3a\u4e86\u5f97\u5230\u6b63\u786e\u7684\u9891\u7387,\u9700\u8981\u8bfb\u53d6\u6574\u4e2a\u8f93\u5165\u6d41,\u6545\u970d\u592b\u66fc\u7b97\u6cd5\u662f\u4e00\u4e2a\u4e24\u8f6e\u7b97\u6cd5<\/p>\n<p>\u4e0d\u65ad\u7684\u5bfb\u627e\u4e24\u4e2a\u9891\u7387\u6700\u5c0f\u7684\u7ed3\u70b9,\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u4e8c\u8005\u4e3a\u5b50\u8282\u70b9\u7684\u65b0\u8282\u70b9(\u65b0\u8282\u70b9\u7684\u9891\u7387\u4e3a\u4e24\u4e2a\u5b50\u8282\u70b9\u7684\u9891\u7387\u4e4b\u548c)<\/p>\n<p>private static Node buildTrie(int[] freq) {\/\/\u83b7\u53d6\u9891\u7387<\/p>\n<p>MinPQ&lt;Node&gt; pq = new MinPQ&lt;Node&gt;();<\/p>\n<p>for (char c = 0; c &lt; R; c++) {<\/p>\n<p>if (freq[c] &gt; 0){<\/p>\n<p>pq.insert(new Node(c, freq[c], null, null));\/\/\u904d\u5386\u63d2\u5165<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>while ((pq.size()) &gt; 1) {<\/p>\n<p>Node x = pq.delMin();<\/p>\n<p>Node y = pq.delMin();<\/p>\n<p>Node parent = new Node(&#8216;\\0&#8217;, x.freq + y.freq, x, y);<\/p>\n<p>pq.insert(parent);<\/p>\n<p>}<\/p>\n<p>return pq.delMin();<\/p>\n<p>}<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"1184\" height=\"535\" src=\"\/wp-content\/uploads\/2021\/03\/unnamed-file-210.png\" class=\"wp-image-453\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-210.png 1184w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-210-300x136.png 300w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-210-1024x463.png 1024w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-210-768x347.png 768w\" sizes=\"(max-width: 1184px) 100vw, 1184px\" \/><\/p>\n<p>\u6784\u6210\u4e86\u4e00\u4e2a\u5b57\u5178\u67e5\u627e\u6811<\/p>\n<p>\u8fd9\u6837\u914d\u5408\u4e0a\u9762\u7684\u5199\u5165,\u5c31\u53ef\u4ee5\u8fdb\u884c\u5199\u5165\u5230\u8f93\u51fa\u6d41\u4e86<\/p>\n<p>\u4e0b\u4e00\u6b65\u5c31\u662f\u538b\u7f29<\/p>\n<p>\u6211\u4eec\u5bf9\u4e8e\u5355\u8bcd\u6811\u7684\u7ea6\u5b9a\u538b\u7f29\u548c\u89e3\u6790\u683c\u5f0f<\/p>\n<p>\u5bf9\u6574\u4e2a\u5355\u8bcd\u67e5\u627e\u6811\u8fdb\u884c\u524d\u5e8f\u904d\u5386,<\/p>\n<p>\u5982\u679c\u627e\u5230\u4e86\u4e00\u4e2a\u5185\u90e8\u8282\u70b9,\u5219\u5199\u5165\u4e00\u4e2a0,\u5982\u679c\u662f\u4e00\u4e2a\u53f6\u5b50\u7ed3\u70b9,\u5219\u5199\u5165\u4e00\u4e2a1,\u7136\u540e\u5199\u5165\u5bf9\u5e94\u5b57\u7b26\u76848\u4f4dASCII\u7801<\/p>\n<p>\u5176\u904d\u5386\u65b9\u5f0f\u6309\u7167\u5982\u6b64,\u8bfb\u53d6\u4e00\u4e2a\u6bd4\u7279\u6765\u83b7\u53d6\u5f53\u524d\u8282\u70b9\u7684\u7c7b\u578b,\u5982\u679c\u662f0\u5219\u4e3a\u5185\u90e8\u8282\u70b9\u5e76\u7ee7\u7eed\u6784\u9020\u4ed6\u7684\u5de6\u53f3\u5b50\u6811<\/p>\n<p>\u5982\u679c\u662f1\u5219\u6784\u9020\u4e00\u4e2a\u53f6\u5b50\u7ed3\u70b9\u5c31\u8bfb\u53d6\u5b57\u7b26\u7f16\u7801\u5e76\u4e14\u6784\u9020\u4e00\u4e2a\u53f6\u5b50\u7ed3\u70b9<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"700\" height=\"369\" src=\"\/wp-content\/uploads\/2021\/03\/unnamed-file-211.png\" class=\"wp-image-454\" alt=\"\u56fe\u7247\" srcset=\"http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-211.png 700w, http:\/\/xinblog.ltd\/wp-content\/uploads\/2021\/03\/unnamed-file-211-300x158.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>private static void writeTrie(Node x) {<\/p>\n<p>if (x.isLeaf) {<\/p>\n<p>BinaryStdOut.write(true);<\/p>\n<p>BinaryStdOut.write(x.ch);\/\/\u5199\u5165\u5b57\u7b26\u4e32<\/p>\n<p>return;<\/p>\n<p>}<\/p>\n<p>BinaryStdOut.write(false)<\/p>\n<p>writeTrie(x.left);<\/p>\n<p>writeTrie(x.right);<\/p>\n<p>}<\/p>\n<p>\u603b\u7ed3\u4e0b\u6765:<\/p>\n<p>\u538b\u7f29\u7684\u5b9e\u73b0\u987a\u5e8f\u4e3a<\/p>\n<p>\u8bfb\u53d6\u8f93\u5165;<\/p>\n<p>\u5c06\u8f93\u5165\u7684\u6bcf\u4e2achar\u503c\u7684\u51fa\u73b0\u9891\u7387\u5236\u4f5c\u6210\u8868\u683c<\/p>\n<p>\u6839\u636e\u9891\u7387\u6784\u9020\u76f8\u5bf9\u5e94\u7684\u970d\u592b\u66fc\u7f16\u7801\u6811<\/p>\n<p>\u6784\u9020\u7f16\u8bd1\u8868,\u5c06\u8f93\u5165\u7684\u6bcf\u4e00\u4e2achar\u503c\u548c\u4e00\u4e2a\u6bd4\u7279\u5b57\u7b26\u4e32\u76f8\u5173\u8054<\/p>\n<p>\u5c06\u5355\u8bcd\u67e5\u627e\u6811\u7f16\u7801\u4e3a\u6bd4\u7279\u5b57\u7b26\u4e32\u5e76\u5199\u5165\u8f93\u51fa\u6d41<\/p>\n<p>\u5c06\u5355\u8bcd\u603b\u6570\u7f16\u7801\u4e3a\u6bd4\u7279\u5b57\u7b26\u4e32\u5e76\u5199\u5165\u8f93\u51fa\u6d41<\/p>\n<p>\u4f7f\u7528\u7f16\u8bd1\u8868\u6765\u7f16\u8bd1\u6bcf\u4e00\u4e2a\u5b57\u7b26,\u5e76\u5199\u5165\u8f93\u51fa\u6d41<\/p>\n<p>\u5c55\u5f00\u987a\u5e8f\u4e3a:&#8230;<\/p>\n<p>\u8bfb\u53d6\u5355\u8bcd\u67e5\u627e\u6811<\/p>\n<p>\u8bfb\u53d6\u8981\u89e3\u7801\u7684\u5b57\u7b26\u6570\u91cf<\/p>\n<p>\u4f7f\u7528\u5355\u8bcd\u67e5\u627e\u6811\u5c06\u6bd4\u7279\u6d41\u8fdb\u884c\u89e3\u7801<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u653e\u5f03\u539f\u6709\u7684\u4e8c\u8fdb\u5236\u6765\u8868\u793a\u4e00\u4e2a\u5b57\u7b26 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"_links":{"self":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/449"}],"collection":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=449"}],"version-history":[{"count":0,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=\/wp\/v2\/posts\/449\/revisions"}],"wp:attachment":[{"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xinblog.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}