Adding one digit (0-9) to the sequence/string creates new 4 digits number

前端 未结 3 1320
再見小時候
再見小時候 2021-01-14 15:31

I\'m trying to find an algorithm which \"breaks the safe\" by typing the keys 0-9. The code is 4 digits long. The safe will be open where it identifies the code as substring

3条回答
  •  清歌不尽
    2021-01-14 16:06

    I found a reduction to your problem:

    Lets define directed graph G = (V,E) in the following way:

    V = {all possible combinations of the code}.

    E = {< u,v > | v can be obtained from u by adding 1 digit (at the end), and delete the first digit}.

    |V| = 10^4.

    Din and Dout of every vertex equal to 10 => |E| = 10^5.

    You need to prove that there is Hamilton cycle in G - if you do, you can prove the existence of a solution.

    EDIT1:

    The algorithm:

    1. Construct directed graph G as mentioned above.

    2. Calculate Hamilton cycle - {v1,v2,..,vn-1,v1}.

    3. Press every number in v1.

    4. X <- v1.

    5. while the safe isn't open:

      5.1 X <- next vertex in the Hamilton path after X.

      5.2 press the last digit in X.

    We can see that because we use Hamilton cycle, we never repeat the same substring. (The last 4 presses).

    EDIT2:

    Of course Hamilton path is sufficient.

提交回复
热议问题