<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Euler path Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/euler-path/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/euler-path/</link>
	<description></description>
	<lastBuildDate>Tue, 04 Sep 2018 04:51:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>Euler path Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/euler-path/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 753. Cracking the Safe</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-753-cracking-the-safe/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-753-cracking-the-safe/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 31 Dec 2017 17:31:01 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[Euler path]]></category>
		<category><![CDATA[hard]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1373</guid>

					<description><![CDATA[<p>题目大意：让你构建一个最短字符串包含所有可能的密码。 There is a box protected by a password. The password is n digits, where each letter can be one of the first kdigits 0, 1, ..., k-1. You&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-753-cracking-the-safe/">花花酱 LeetCode 753. Cracking the Safe</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/kRdlLahVZDc?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p>题目大意：让你构建一个最短字符串包含所有可能的密码。</p>
<p>There is a box protected by a password. The password is <code>n</code> digits, where each letter can be one of the first <code>k</code>digits <code>0, 1, ..., k-1</code>.</p>
<p>You can keep inputting the password, the password will automatically be matched against the last <code>n</code> digits entered.</p>
<p>For example, assuming the password is <code>"345"</code>, I can open it when I type <code>"012345"</code>, but I enter a total of 6 digits.</p>
<p>Please return any string of minimum length that is guaranteed to open the box after the entire string is inputted.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: n = 1, k = 2
Output: "01"
Note: "10" will be accepted too.</pre><p><b>Example 2:</b></p><pre class="crayon-plain-tag">Input: n = 2, k = 2
Output: "00110"
Note: "01100", "10011", "11001" will be accepted too.</pre><p><b>Note:</b></p>
<ol>
<li><code>n</code> will be in the range <code>[1, 4]</code>.</li>
<li><code>k</code> will be in the range <code>[1, 10]</code>.</li>
<li><code>k^n</code> will be at most <code>4096</code>.</li>
</ol>
<p><ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-2404451723245401"
     data-ad-slot="7983117522"><br />
</ins></p>
<h1><strong>Idea: Search</strong></h1>
<p><img 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="(max-width: 960px) 100vw, 960px" /></p>
<h1><strong>Solution 1: DFS w/ backtracking</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C ++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 9 ms
class Solution {
public:
    string crackSafe(int n, int k) {
        const int total_len = pow(k, n) + n - 1;
        string ans(n, '0');
        unordered_set&lt;string&gt; visited{ans};
        if (dfs(ans, total_len, n, k, visited))
            return ans;
        return "";
    }
private:
    bool dfs(string&amp; ans, const int total_len, const int n, const int k, unordered_set&lt;string&gt;&amp; visited) {
        if (ans.length() == total_len)
            return true;
        
        string node = ans.substr(ans.length() - n + 1, n - 1);
        for (char c = '0'; c &lt; '0' + k; ++c) {
            node.push_back(c);
            if (!visited.count(node)) {
                ans.push_back(c);
                visited.insert(node);
                if (dfs(ans, total_len, n, k, visited)) return true;
                visited.erase(node);
                ans.pop_back();
            }
            node.pop_back();
        }
        
        return false;
    }
};</pre><p></div></div></p>
<h1><strong>Solution 2: DFS w/o backtracking</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 13 ms
class Solution {
public:
    string crackSafe(int n, int k) {
        const int total_len = pow(k, n) + n - 1;        
        string node(n - 1, '0');
        string ans;
        unordered_set&lt;string&gt; visited;
        dfs(node, k, visited, ans);
        return ans + node;
    }
private:
    void dfs(const string&amp; node, const int k, unordered_set&lt;string&gt;&amp; visited, string&amp; ans) {        
        for (char c = '0'; c &lt; '0' + k; ++c) {
            string next = node + c;            
            if (visited.count(next)) continue;            
            visited.insert(next);                 
            dfs(next.substr(1), k, visited, ans);
            ans.push_back(c);
        }
    }
};</pre><p></div></div></p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-753-cracking-the-safe/">花花酱 LeetCode 753. Cracking the Safe</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/graph/leetcode-753-cracking-the-safe/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
