leetcode 753. Cracking the Safe

本文介绍了一种算法,用于找到能打开由n位k进制密码保护的密码箱的最短序列。通过递归地尝试所有可能的组合,并使用哈希集合记录已尝试的密码,确保最终序列覆盖所有可能的密码组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

While entering a password, the last ndigits entered will automatically be matched against the correct password.

For example, assuming the correct password is "345", if you type "012345", the box will open because the correct password matches the suffix of the entered password.

Return any password of minimum lengththat is guaranteed to open the box at some point of entering it.

 

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.
class Solution {
public:
    string crackSafe(int n, int k) 
    {
        string res = "" ;
        unordered_set<string> password ;
        return res = crackSafe(res , n , k , password) ;
    }
    string crackSafe(string& s , int n , int k , unordered_set<string> &password)
    {
        if(s.size() < n) 
        {
            for(int i = 0 ; i < k ; ++i) 
            {
                char c = '0' + i ;
                s += c ;
                return crackSafe(s , n , k , password) ;
            }
        }
        else if(s.size() == n) password.insert(s) ;
    
        
        for(int i = 0 ; i < k ; ++i)
        {
            char c = '0' + i ;
            string pw = s.substr(s.size() - n + 1) + c ;
            
            if(!password.count(pw)) 
            {
                string res = "" ;
                password.insert(pw) ;
                s += c ;
                res = crackSafe(s , n , k , password);
                if( res.size() == pow(k , n) + n - 1) return res ;
                else
                {
                    s = s.substr(0 , s.size() - 1) ;
                    password.erase(pw) ;
                }
            }
        }
        
        return s ;
    }
};
class Solution {
public:
    string crackSafe(int n, int k) 
    {
        string res = "" ;
        unordered_set<string> password ;
        return res = crackSafe(res , n , k , password) ;
    }
    string crackSafe(string& s , int n , int k , unordered_set<string> &password)
    {
        if(s.size() < n) 
        {
            s += '0' ; // 与上一种不一样的地方,可以发现总有一串最短字符串是以string(n , '0')开头的
            return crackSafe(s , n , k , password) ; 
        }
        else if(s.size() == n) password.insert(s) ;
    
        
        for(int i = 0 ; i < k ; ++i)
        {
            char c = '0' + i ;
            string pw = s.substr(s.size() - n + 1) + c ;
            
            if(!password.count(pw)) 
            {
                string res = "" ;
                password.insert(pw) ;
                s += c ;
                res = crackSafe(s , n , k , password);
                if( res.size() == pow(k , n) + n - 1) return res ;
                else
                {
                    s = s.substr(0 , s.size() - 1) ;
                    password.erase(pw) ;
                }
            }
        }
        
        return s ;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值