Project Euler 79: Passcode derivation

A security measure frequently used online banking is to ask three random characters in their password to the user. For example, if the password is 531278, the bank may ask the user password in the second, third and fifth character is the number of correct responses should be 317. Text file keylog.txt contains fifty successful login attempts, always assuming that one of the three characters in order of appearance, by analyzing this file deduce the shortest possible length of the password is unknown.

Analysis: This question can be used topological sorting algorithm in graph theory is relatively easy to solve. The so-called topological sorting (Topological Sorting) refers to a directed acyclic graph (DAG, Directed Acyclic Graph) linear sequence of all vertices. The sequence and must satisfy the following two conditions: (1) each vertex appears once and only once; (2) if there is a path from a vertex A to a vertex B, then A appears in front of the apex B of the apex in the sequence. Obviously, only directed acyclic topological sorting chart only, non-topological sort does not exist a directed acyclic graph. By analyzing the meaning of the questions, questions asked shortest possible password, so the password can not necessarily duplicate numbers, otherwise it can not be the shortest, which satisfies the first condition topological sorting. Moreover, the text file are given in the order of fifty to three digits after the same taken out of the former password, thus also satisfies the second condition topological sort. Thus, the subject of the request shortest possible passwords actually required topological sort of the FIG.

After analysis, we found that these fifty numbers only thirty three are not repeated, so we can use the collection to store these numbers to avoid duplication. For any of a number of them, in fact, may be formed to have two edges of the graph, such as 319 to indicate the presence of \ (3 \ rightarrow9 \) and \ (1 \ rightarrow9 \) the two sides. We used these data to import database networkx side to the figures, through all thirty-three numbers, FIG finally formed as follows:

From the above figures we can clearly see that this number does not exist as point 7 on its side, and therefore must be the first digit of the password; In addition, no other starting point 0 from the digital side of this figure, it is inevitable 0 the password is the last digit. With the above in the graph model, we can use Kahn algorithm or depth-first search topological sorting obtained this figure. We will not repeat here-create the wheel, the direct use of existing networkx library algorithms. Been tried, this figure there is only a topological sort, it will be spliced into a number that is the subject of the request.

# time cost = 367 µs ± 2.94 µs

import networkx as nx

def get_data_from_file(file_name="data/ep79.txt"):
    data = set()
    with open(file_name) as f:
        for line in f.readlines():
            data.add(line)
    return data

def main():
    data = get_data_from_file()
    G = nx.DiGraph()
    for i in data:
        G.add_edges_from([(i[0],i[1]),(i[1],i[2])])
    ans = list(nx.all_topological_sorts(G))[0]
    return int(''.join(ans))

Guess you like

Origin www.cnblogs.com/metaquant/p/12073073.html