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.

Approach #1: DFS. [Java]

class Solution {
public String crackSafe(int n, int k) {
String strPwd = String.join("", Collections.nCopies(n, "0"));
StringBuilder sbPwd = new StringBuilder(strPwd);
int total = (int)Math.pow(k, n);
Set<String> seen = new HashSet<>();
seen.add(strPwd); crackSafeAfter(sbPwd, total, seen, n, k); return sbPwd.toString();
} private boolean crackSafeAfter(StringBuilder pwd, int total, Set<String> seen, int n, int k) {
if (seen.size() == total) return true; String lastDigits = pwd.substring(pwd.length()-n+1);
for (char ch = '0'; ch < '0' + k; ch++) {
String newComb = lastDigits + ch;
if (!seen.contains(newComb)) {
seen.add(newComb);
pwd.append(ch);
if (crackSafeAfter(pwd, total, seen, n, k)) return true;
seen.remove(newComb);
pwd.deleteCharAt(pwd.length() - 1);
}
} return false;
}
}

  

Analysis:

In order to guarantee to open the box at last, the input password ought to contain all length-n combinations on digits [0...k-1] - there should be k^n combinations in total.

To make the input password as short as possible, we'd better make each possible length-n combination on digits [0...k-1] occurs exactly once as a substring of the password. The existence of such a password is proved by DeBruijin sequence:

A De Bruijn sequence of order n on a size-k alphabet A is a cyclic sequence in which every possible length-n string on A occurs exactly once as a substring. It has length k^n, which is also the number of distinct substrings of length n on a size-k alphabet; De Bruijn sequences are therefore optimally short.

We reuse last n-1 digits of the input-so-far password as below:

e.g. n = 2, k = 2

all 2-length combinations on [0, 1]:

00 ('00'110)

01 (0'01'10)

11 (00'11'0)

   10 (001'10')

The password is 00110

We can utilize DFS to find the password:

goal: to find the shortest input password such that each possible n-length combination of digits [0..k-1] occurs exactly once as a substring.

node: current input password

edge: if the last n - 1 digits of node1 can be transformed to node2 by appending a digit from 0..k-1, there will be an edge between node1 and node2

start node: n repeated 0's
end node: all n-length combinations among digits 0..k-1 are visited

visitedComb: all combinations that have been visited

Reference:

https://leetcode.com/problems/cracking-the-safe/discuss/153039/DFS-with-Explanations

753. Cracking the Safe的更多相关文章

  1. [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 th ...

  2. 【LeetCode】753. Cracking the Safe 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/cracking ...

  3. 暴力枚举 + 24点 --- hnu : Cracking the Safe

    Cracking the Safe Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit u ...

  4. [LeetCode] Cracking the Safe 破解密码

    There is a box protected by a password. The password is n digits, where each letter can be one of th ...

  5. HNU 12886 Cracking the Safe(暴力枚举)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12886&courseid=274 解题报告:输入4个数 ...

  6. [Swift]LeetCode753. 破解保险箱 | Cracking the Safe

    There is a box protected by a password. The password is n digits, where each letter can be one of th ...

  7. HNU 12886 Cracking the Safe 二十四点的判断

    经典的一个题,今天竟然写跪了…… 题意: 给你4个数字,让你判断是否能通过四则运算和括号,凑成24点. 思路: 暴力枚举运算顺序和运算符. 代码: #include <iostream> ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. 算法与数据结构基础 - 深度优先搜索(DFS)

    DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以 ...

随机推荐

  1. 一文帮你搞懂 Android 文件描述符

    介绍文件描述符的概念以及工作原理,并通过源码了解 Android 中常见的 FD 泄漏. 一.什么是文件描述符? 文件描述符是在 Linux 文件系统的被使用,由于Android基 于Linux 系统 ...

  2. QuickBase64 - Android 下拉通知栏快捷base64加解密工具

    Android Quick Setting Tile Base64 Encode/Decode Tool Android 下拉通知栏快捷 base64 加解密,自动将剪切板的内容进行 base64 E ...

  3. Nginx常用内核参数优化,安装,基本命令

    1.内核参数配置,默认的Linux内核参数考虑的是通用的场景,明显不符合用于支持高并发访问web服务的定义,所以需要修改Linux内核参数,使得Nginx可以拥有更高的性能.可以通过修改 /etc/s ...

  4. 剑指 Offer 25. 合并两个排序的链表

    剑指 Offer 25. 合并两个排序的链表 Offer 25 该问题的原型就是多项式的合并. 实现较简单,没有特殊需要注意的问题. package com.walegarrett.offer; /* ...

  5. CNN结构演变总结(三)设计原则

    CNN结构演变总结(一)经典模型 CNN结构演变总结(二)轻量化模型 前言: 前两篇对一些经典模型和轻量化模型关于结构设计方面的一些创新进行了总结,在本文将对前面的一些结构设计的原则,作用进行总结. ...

  6. 03-Spring默认标签解析

    默认标签的解析 上一篇分析了整体的 xml 文件解析,形成 BeanDefinition 并注册到 IOC 容器中,但并没有详细的说明具体的解析,这一篇主要说一下 默认标签的解析,下一篇主要说自定义标 ...

  7. springboot整合mybatis。mapper.xml资源文件放置到resources文件夹下的配置&别名使用配置

  8. Go语言学习 学习资料汇总

    从进入实验室以来,一直听小溪师兄说Go语言,但是第一学期的课很多,一直没有时间学习,现在终于空出来时间学习,按照我的学习习惯,我一般分为三步走 学习一门语言首先要知道学会了能干什么, 然后再把网上的资 ...

  9. BZOJ_2844 albus就是要第一个出场 【线性基】

    一.题目 albus就是要第一个出场 二.分析 非常有助于理解线性基的一题. 构造线性基$B$后,如果$|A| > |B|$,那么就意味着有些数可以由$B$中的数异或出来,而多的数可以取或者不取 ...

  10. BZOJ_4034 [HAOI2015]树上操作 【树链剖分dfs序+线段树】

    一 题目 [HAOI2015]树上操作 二 分析 树链剖分的题,这里主要用到了$dfs$序,这题比较简单的就是不用求$lca$. 1.和树链剖分一样,先用邻接链表建双向图. 2.跑两遍$dfs$,其实 ...