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

前端 未结 3 1288
再見小時候
再見小時候 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 15:59

    Here in summary is the problem I think you are trying to solve and some explanation on how i might approach solving it. http://www.cs.swan.ac.uk/~csharold/cpp/SPAEcpp.pdf

    You have to do some finessing to make it fit into the chinese post man problem however... Imagine solving this problem for the binary digits, three digits strings. Assume you have the first two digits, and ask your self what are my options to move to? (In regards to the next two digit string?) You are left with a Directed Graph.

     /-\
    /   V    
    \-  00 ----> 01
          ^  /   ^|
           \/    ||
           /\    ||
          V  \   |V
     /-- 11 ---> 10 
     \   ^         
      \-/
    

    Solve the Chinese Postman, you will have all combinations and will form one string The question is now, is the Chinese postman solvable? There are algorithms which determine weather or not a DAG is solvable for the CPP, but i don't know if this particular graph is necessarily solvable based on the problem alone. That would be a good thing to determine. You do however know you could find out algorithmically weather it is solvable and if it is you could solve it using algorithms available in that paper (I think) and online.

    Every vertex here has 2 incoming edges and 2 outgoing edges. There are 4 (2^2) vertexes.

    In the full sized problem there are 19683( 3 ^ 9 ) vertexs and every vertex has 512 ( 2 ^ 9 ) out going and incoming vertexes. There would be a total of

    19683( 3 ^ 9 ) x 512 (2 ^ 9) = 10077696 edges in your graph. 
    

    Approach to solution:

    1.) Create list of all 3 digit numbers 000 to 999.
    2.) Create edges for all numbers such that last two digits of first number match first
    two digits of next number. 
    
    ie 123 -> 238 (valid edge) 123 -> 128 (invalid edge)
    
    3.) use Chinese Postman solving algorithmic approaches to discover if solvable and
    solve
    
    0 讨论(0)
  • 2021-01-14 16:02

    I would create an array of subsequences which needs to be updates upon any insertion of a new digit. So in your example, it will start as:

    array = {1}
    

    then

    array = {1,2,12}
    

    then

    array = {1,2,12,3,13,23,123}
    

    then

    array = {1,2,12,3,13,23,123,4,14,24,124,34,134,234,1234}
    

    and when you have a sequence that is already at the length=4 you don't need to continue the concatenation, just remove the 1st digit of the sequence and insert the new digit at the end, for example, use the last item 1234, when we add 5 it will become 2345 as follows:

    array = {1,2,12,3,13,23,123,4,14,24,124,34,134,234,1234,5,15,25,125,35,135,235,1235,45,145,245,1245,345,1345,2345,2345}
    

    I believe that this is not a very complicated way of going over all the sub-sequences of a given sequence.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题