{"id":1373,"date":"2017-12-31T09:31:01","date_gmt":"2017-12-31T17:31:01","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1373"},"modified":"2018-09-03T21:51:59","modified_gmt":"2018-09-04T04:51:59","slug":"leetcode-753-cracking-the-safe","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-753-cracking-the-safe\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 753. Cracking the Safe"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 753. Cracking the Safe  - \u5237\u9898\u627e\u5de5\u4f5c EP144\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/kRdlLahVZDc?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>\u9898\u76ee\u5927\u610f\uff1a\u8ba9\u4f60\u6784\u5efa\u4e00\u4e2a\u6700\u77ed\u5b57\u7b26\u4e32\u5305\u542b\u6240\u6709\u53ef\u80fd\u7684\u5bc6\u7801\u3002<\/p>\n<p>There is a box protected by a password. The password is\u00a0<code>n<\/code>\u00a0digits, where each letter can be one of the first\u00a0<code>k<\/code>digits\u00a0<code>0, 1, ..., k-1<\/code>.<\/p>\n<p>You can keep inputting the password, the password will automatically be matched against the last\u00a0<code>n<\/code>\u00a0digits entered.<\/p>\n<p>For example, assuming the password is\u00a0<code>\"345\"<\/code>, I can open it when I type\u00a0<code>\"012345\"<\/code>, but I enter a total of 6 digits.<\/p>\n<p>Please return any string of minimum length that is guaranteed to open the box after the entire string is inputted.<\/p>\n<p><b>Example 1:<\/b><\/p>\n<pre class=\"lang:default highlight:0 decode:true\">Input: n = 1, k = 2\r\nOutput: \"01\"\r\nNote: \"10\" will be accepted too.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"lang:default highlight:0 decode:true\">Input: n = 2, k = 2\r\nOutput: \"00110\"\r\nNote: \"01100\", \"10011\", \"11001\" will be accepted too.\r\n<\/pre>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li><code>n<\/code>\u00a0will be in the range\u00a0<code>[1, 4]<\/code>.<\/li>\n<li><code>k<\/code>\u00a0will be in the range\u00a0<code>[1, 10]<\/code>.<\/li>\n<li><code>k^n<\/code>\u00a0will be at most\u00a0<code>4096<\/code>.<\/li>\n<\/ol>\n<p><ins class=\"adsbygoogle\"\n     style=\"display:block; text-align:center;\"\n     data-ad-layout=\"in-article\"\n     data-ad-format=\"fluid\"\n     data-ad-client=\"ca-pub-2404451723245401\"\n     data-ad-slot=\"7983117522\"><br \/>\n<\/ins><\/p>\n<h1><strong>Idea:\u00a0Search<\/strong><\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1379\" src=\"http:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/754-ep144.png\" alt=\"\" width=\"960\" height=\"540\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/754-ep144.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/754-ep144-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2017\/12\/754-ep144-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/p>\n<h1><strong>Solution 1: DFS\u00a0w\/ backtracking<\/strong><\/h1>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C ++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Runtime: 9 ms\r\nclass Solution {\r\npublic:\r\n    string crackSafe(int n, int k) {\r\n        const int total_len = pow(k, n) + n - 1;\r\n        string ans(n, '0');\r\n        unordered_set&lt;string&gt; visited{ans};\r\n        if (dfs(ans, total_len, n, k, visited))\r\n            return ans;\r\n        return \"\";\r\n    }\r\nprivate:\r\n    bool dfs(string&amp; ans, const int total_len, const int n, const int k, unordered_set&lt;string&gt;&amp; visited) {\r\n        if (ans.length() == total_len)\r\n            return true;\r\n        \r\n        string node = ans.substr(ans.length() - n + 1, n - 1);\r\n        for (char c = '0'; c &lt; '0' + k; ++c) {\r\n            node.push_back(c);\r\n            if (!visited.count(node)) {\r\n                ans.push_back(c);\r\n                visited.insert(node);\r\n                if (dfs(ans, total_len, n, k, visited)) return true;\r\n                visited.erase(node);\r\n                ans.pop_back();\r\n            }\r\n            node.pop_back();\r\n        }\r\n        \r\n        return false;\r\n    }\r\n};<\/pre>\n<\/div><\/div>\n<h1><strong>Solution 2: DFS\u00a0w\/o backtracking<\/strong><\/h1>\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 13 ms\r\nclass Solution {\r\npublic:\r\n    string crackSafe(int n, int k) {\r\n        const int total_len = pow(k, n) + n - 1;        \r\n        string node(n - 1, '0');\r\n        string ans;\r\n        unordered_set&lt;string&gt; visited;\r\n        dfs(node, k, visited, ans);\r\n        return ans + node;\r\n    }\r\nprivate:\r\n    void dfs(const string&amp; node, const int k, unordered_set&lt;string&gt;&amp; visited, string&amp; ans) {        \r\n        for (char c = '0'; c &lt; '0' + k; ++c) {\r\n            string next = node + c;            \r\n            if (visited.count(next)) continue;            \r\n            visited.insert(next);                 \r\n            dfs(next.substr(1), k, visited, ans);\r\n            ans.push_back(c);\r\n        }\r\n    }\r\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u8ba9\u4f60\u6784\u5efa\u4e00\u4e2a\u6700\u77ed\u5b57\u7b26\u4e32\u5305\u542b\u6240\u6709\u53ef\u80fd\u7684\u5bc6\u7801\u3002 There is a box protected by a password. The password is\u00a0n\u00a0digits, where each letter can be one of the first\u00a0kdigits\u00a00, 1, &#8230;, k-1. You&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[33,194,217],"class_list":["post-1373","post","type-post","status-publish","format-standard","hentry","category-graph","tag-dfs","tag-euler-path","tag-hard","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1373"}],"version-history":[{"count":10,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1373\/revisions"}],"predecessor-version":[{"id":3838,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1373\/revisions\/3838"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}