[LeetCode 753] Cracking the Safe

There is a box protected by a password. The password is n digits, where each letter can be one of the first k digits 0, 1, ..., k-1.

You can keep inputting the password, the password will automatically be matched against the last n digits entered.

For example, assuming the password is "345", I can open it when I type "012345", but I enter a total of 6 digits.

Please return any string of minimum length that is guaranteed to open the box after the entire string is inputted.

Example 1:

Input: n = 1, k = 2
Output: "01"
Note: "10" will be accepted too.

Example 2:

Input: n = 2, k = 2
Output: "00110"
Note: "01100", "10011", "11001" will be accepted too.

Note:

  1. n will be in the range [1, 4].
  2. k will be in the range [1, 10].
  3. k^n will be at most 4096.

 

分析

这道题我刚开始看的时候尝试做字符串匹配,但是想来想去也没有想到比较好的解法,同时hint中给出的欧拉图的提示我也没有想明白怎么去解。后来看到别人的解法,才明白这一题用DFS就能暴力的破解得到答案。

DFS在搜索的一个原则在于一定要使得当前的字符串的最后(n-1)位使待搜索的下一个答案的前(n-1)位。例如n=4, k=3,那么如果我们以0000为起点,那么我们下一个尝试的字符串分别是0001和0002,挨个尝试,直到包含了所有的字符串。怎么确定我们包含了所有可能的字符串呢?使用set记录已经包含的字符串,当set.size() == pow(k, n)时即可。

但是我们上述的解法存在了一个前提条件:所有的字符串都可以使用(n-1)个重复的位依次拼接字符串,并能够依次遍历所有的字符串并且不重复。如果不是的话,那么在深搜的时候很有可能出现找不到解的情况。那么这个题的前提条件就是:所有可能字符串可以看做一个节点,取一个字符串s1的后(n-1)位与另一个字符串s2的前(n-1)位重复,那么就组成了s1->s2的一条边,那么所有pow(k,n)个节点组成的图必须是哈密顿图才能保证我们的深搜一定是有解的。而哈密顿图的证明我在众多答案中没有找到,本人的几何知识也不是很好,也无力证明。只能以这个为前提给出答案了。

Code

DFS

class Solution {
public:
    string crackSafe(int n, int k) {
        set<string> s;
        string res(n, '0');
        s.insert(res);
        int sum = pow(k, n);
        dfs(s, n, k, res, sum);
        return res;
    }
    
    bool dfs(set<string>& s, int n, int k, string& res, int sum)
    {
        if (s.size() == sum)
            return true;
        
        for (int i = 0; i < k; i ++)
        {
            string tmp = res.substr(res.size() -n + 1);
            tmp.push_back(i+'0');
            if (s.find(tmp) != s.end())
            {
                continue;   
            }
            s.insert(tmp);
            res.push_back(i+'0');
            if (dfs(s, n, k, res, sum))
                return true;
            res.pop_back();
            s.erase(tmp);
        }
        return false;
    }
};

贪心解法

这个解法建立在DFS的一个特殊场景下,n=3, k=2为例,以000为开始,我们遍历00 + i for i in k-1 to 0,一定能够遍历所有的点。但是如果反过来,遍历 00 + i for i in 0 to k-1,就找不到相应的解。所以这道题的一个理论依据也是没有看到证明,只能说最后的结果证明是work的。

class Solution {
public:
    string crackSafe(int n, int k) {
        set<string> s;
        string res(n, '0');
        s.insert(res);
        int sum = pow(k, n);
        
        cout << sum << " " << s.size() << endl;
        while (s.size() < sum)
        {
            string nexts = res.substr(res.size() - n + 1);
            for (int i = k-1; i >= 0; i --)
            {
                nexts.push_back(i + '0');
                cout << nexts << endl;
                if (s.find(nexts) == s.end())
                {
                    s.insert(nexts);
                    res.push_back(i + '0');
                    break;
                }
                nexts.pop_back();
            }
        }
        return res;
    }
};

运行效率

DFS解法

Runtime: 32 ms, faster than 32.25% of C++ online submissions for Cracking the Safe.

Memory Usage: 15.7 MB, less than 22.86% of C++ online submissions for Cracking the Safe.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值