c++ - List of 10 digit strings where every 4 digit substring is unique -
c++ - List of 10 digit strings where every 4 digit substring is unique -
in past competition, asked generate 10 digit strings using digits 0 9 1 time each. four-digit substring used in string cannot used again.
what highest number of unique strings can generate using these rules? list them.
example:
if utilize string 0243697518 in list, can not generate strings contain 0243, 2436, 4369, 3697, 6975, 9751 , 7518
to solve problem, have written c++ program, scans permutation of "0123456789" , add together them solution list if 4 digit sub-strings of code has not been used before. problem of algorithm size of solution list varies depending on starting point add together list first. if start adding list "0123456789", list ends 504 entries not maximum asked. wonder how solve question, help highly appreciated. i'm open hear mathematical solution or algorithm suggestions generate list asked.
#include <iostream> #include <cstdint> #include <vector> #include <set> #include <algorithm> using namespace std; void main(void) { set<string> substring_list; // holds list of used 4 digit sub-strings set<string> solution_list; string code = "0123456789"; { vector<string> subs; (int = 0; < 7; i++) { // adds 4 digits sub-strings used in code subs.push_back(code.substr(i, 4)); } if ((substring_list.find(subs[0]) == substring_list.end()) && (substring_list.find(subs[1]) == substring_list.end()) && (substring_list.find(subs[2]) == substring_list.end()) && (substring_list.find(subs[3]) == substring_list.end()) && (substring_list.find(subs[4]) == substring_list.end()) && (substring_list.find(subs[5]) == substring_list.end()) && (substring_list.find(subs[6]) == substring_list.end())) { // if substrings unique , not used before // add together code solution list solution_list.insert(code); // add together newly added code's substrings substring list. (auto s: subs) { substring_list.insert(s); } } } while (next_permutation(code.begin(), code.end())); cout << solution_list.size() << endl; }
c++ algorithm math graph-theory
Comments
Post a Comment