Project Euler:problem Passcode Derivation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.