A common security method used for online banking are to ask the user for three random characters from a passcode. For example, if the passcode is 531278, they may ask for the 2nd, 3rd, and 5th characters; The expected reply would be:317.
The text file, Keylog.txt, contains fifty successful login attempts.
Given that the three characters is always asked for on order, analyse the file so as to determine the shortest possible s Ecret passcode of unknown length.
First, each number that appears before and after each number is sorted out.
#include <iostream> #include <string> #include <vector> #include <set> #include <fstream> Using namespace Std;int Main () {Ifstream input;input.open ("Keylog.txt"); vector<string>s;string line;while (! Input.eof ()) {input >> line;s.push_back (line);} set<char> a[10][2];for (int i = 0; i < s.size (); i++) {a[s[i][0]-' 0 '][1].insert (s[i][1]); a[s[i][0]-' 0 '][1].ins ERT (s[i][2]); a[s[i][1]-' 0 '][1].insert (s[i][2]); a[s[i][1]-' 0 '][0].insert (s[i][0]); a[s[i][2]-' 0 '][0].insert (s[i] [0]); A[S[I][2]-' 0 '][0].insert (s[i][1]);} for (int i = 0, I <= 9; i++) {for (int j = 0; J < 2; J + +) {if (j = = 0) {cout << "appears before" << I << "Number:" ;} Elsecout << "appears after" << I << "number:"; for (Set<char>::iterator P = a[i][j].cbegin (); p! = A[i][j].cend (); ++p) cout << *p << ""; cout << Endl;}} System ("pause"); return 0;}
The results are as follows:
It is obvious that the password does not appear in 4 and 5, and 7 is preceded by no other number, which means that 7 is the first number, then the number appearing in the three front is only 7, which determines the second number is 3
Then according to the number 1 appears before 3, 7, to determine the third digit is 1, followed by the number of 6,2,8,9,0 before, and finally get the password is 73162890
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Project Euler:problem Passcode Derivation