{"id":618,"date":"2024-06-28T09:00:00","date_gmt":"2024-06-28T01:00:00","guid":{"rendered":"https:\/\/seanxd.com\/?p=618"},"modified":"2024-06-28T11:59:11","modified_gmt":"2024-06-28T03:59:11","slug":"zerojudge-i646","status":"publish","type":"post","link":"https:\/\/seanxd.com\/en\/zerojudge-i646\/","title":{"rendered":"ZeroJudge I646: Permutations and Sorting"},"content":{"rendered":"\n\n\n<p>\u9ad8\u4e2d\u6578\u5b78\u6709\u6559\u5230\u6392\u5217\u7d44\u5408\uff0c\u6709\u6642\u5019\u6703\u5c07<strong>\u6240\u6709\u7684\u300c\u6392\u5217\u300d\u65b9\u6cd5\u5217\u51fa\u4f86<\/strong>\u3002\u73fe\u5728\u5c31\u9ebb\u7169\u4f60\u5e6b\u8001\u5e2b\u505a\u9019\u4e00\u4ef6\u4e8b\u5427\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7bc4\u4f8b\u6e2c\u8cc7<\/h2>\n\n\n\n<figure class=\"wp-block-table nfd-wb-animate nfd-wb-fade-in-bottom-short\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7bc4\u4f8b\u8f38\u5165<\/th><th>\u7bc4\u4f8b\u8f38\u51fa<\/th><\/tr><\/thead><tbody><tr><td>\u8f38\u5165\u542b\u6709\u591a\u7b46\u6e2c\u8cc7\uff0c\u6bcf\u7b46\u6e2c\u8cc7\u4e00\u5217\uff0c\u542b\u67091\u500b\u6b63\u6574\u6578 N (1 &lt;= N &lt;= 7) \u4ee3\u8868\u6709\u591a\u5c11\u500b\u6771\u897f\u8981\u6392\u5217\u3002<strong>N \u4ef6\u6771\u897f\u7684\u4ee3\u865f\u4ee5 a\u3001b\u3001c\u3001\u2026 \u4ee3\u8868\u3002<\/strong><br>\u7576 N = 0 \u6642\u4ee3\u8868\u8f38\u5165\u7d50\u675f (\u9019\u7b46\u4e0d\u7528\u8f38\u51fa)\u3002<\/td><td>\u5c0d\u6bcf\u7b46\u6e2c\u8cc7\u8f38\u51fa\u6240\u6709\u7684\u6392\u5217\uff0c\u6bcf\u500b\u6392\u5217\u4e00\u5217\uff0c\u4e0d\u540c\u7684\u6392\u5217\u65b9\u5f0f\u6309\u5b57\u5178\u9806\u5e8f\u8f38\u51fa\u3002<\/td><\/tr><tr><td>2<br>3<br>0<\/td><td>ab<br>ba<br>abc<br>acb<br>bac<br>bca<br>cab<br>cba<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u89e3\u984c\u601d\u8def<\/h2>\n\n\n\n<p>\u4f7f\u7528 DFS \u4e26\u4f7f\u7528\u5b57\u4e32\u7684\u65b9\u5f0f\u5c07\u7b54\u6848\u52a0\u51fa\u4f86\uff0c\u8dd1\u4e00\u500b For\u8ff4\u5708 \u4e26\u5ba3\u500b\u4e00\u500b Map \u4f86\u5b58\u76ee\u524d\u4ee5\u7d93\u5b58\u653e\u904e\u7684\u5b57\u6bcd\uff0c\u5982\u679c\u76ee\u524d\u8dd1\u5230\u7684\u5b57\u6bcd\u6c92\u6709\u88ab\u653e\u904e\u5c31\u5c07 Map[\u5b57\u6bcd]++ \u548c \u5b57\u4e32+\u5b57\u6bcd \u4e26\u518d\u4e00\u6b21\u547c\u53eb DFS\uff0c\u547c\u53eb\u5b8c\u8a18\u5f97\u8981\u5c07 Map \u9084\u6709\u5b57\u4e32\u5fa9\u539f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff0d<a href=\"https:\/\/zerojudge.tw\/ShowProblem?problemid=i646\" target=\"_blank\" rel=\"noreferrer noopener\">ZeroJudge I646: \u6392\u5217\u7d44\u5408-\u6392\u5217<\/a><\/h3>\n\n\n\n<div class=\"hcb_wrap nfd-wb-animate nfd-wb-reveal-right nfd-delay-50\"><pre class=\"prism line-numbers lang-cpp\" data-lang=\"C++\"><code>#include &lt;iostream&gt;\n#include &lt;map&gt;\n#include &lt;vector&gt;\nusing namespace std;\n\nvector&lt;string&gt;ans;\nint N;\n\nvoid DFS(string str, map&lt;char, int&gt;MAP) {\n    if (str.length() == N) {\n        ans.push_back(str);\n        return;\n    }\n    for (int i = 0; i&lt;N; i++) {\n        char ch = i + &#39;a&#39;;\n        if (MAP[ch] == 0) {\n            MAP[ch]++;\n            DFS(str+ch, MAP);\n            MAP[ch]--;\n        }\n    }\n}\n\nint main() {\n    cin.sync_with_stdio(0);\n    cin.tie(0);\n    while (cin &gt;&gt; N && N != 0) {\n        ans.clear();\n        map&lt;char, int&gt;MAP;\n        DFS(&quot;&quot;, MAP);\n        for (int i = 0; i&lt;ans.size(); i++) {\n            cout &lt;&lt; ans[i] &lt;&lt; &quot;\\n&quot;;\n        }\n    }\n}\n\n\/\/ZeroJudge I646\n\/\/Dr. SeanXD<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9ad8\u4e2d\u6578\u5b78\u6709\u6559\u5230\u6392\u5217\u7d44\u5408\uff0c\u6709\u6642\u5019\u6703\u5c07\u6240\u6709\u7684\u300c\u6392\u5217\u300d\u65b9\u6cd5\u5217\u51fa\u4f86\u3002\u73fe\u5728\u5c31\u9ebb\u7169\u4f60\u5e6b\u8001\u5e2b\u505a\u9019\u4e00\u4ef6\u4e8b\u5427\uff01 \u7bc4\u4f8b\u6e2c\u8cc7 \u7bc4\u4f8b\u8f38 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","footnotes":""},"categories":[6],"tags":[21,14,20,8,11,9],"class_list":["post-618","post","type-post","status-publish","format-standard","hentry","category-6","tag-dfs","tag-map","tag-20","tag-8","tag-11","tag-9"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/618","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/comments?post=618"}],"version-history":[{"count":2,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/618\/revisions"}],"predecessor-version":[{"id":1025,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/posts\/618\/revisions\/1025"}],"wp:attachment":[{"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/media?parent=618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/categories?post=618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/seanxd.com\/en\/wp-json\/wp\/v2\/tags?post=618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}