samedi 15 juin 2019

Avoid "doubles" in permutation list

i have no idea how to avoid "doubles".

Currently i get all permutations like: abcdef a ab abc

But also permutations like: aabbcc aaaaaf

I need an output without such doubles cause the list is currently 55986 lines long :/

#include <string>
#include <vector>
#include <iostream>
#include<fstream>
using namespace std;

vector<string> make_permutations(int length) {
    if (length == 0) {
        return vector<string>{};
    }
    vector<string> results;
    vector<string> children = make_permutations(length - 1);
    for (char letter = 'a'; letter <= 'f'; letter++) {
        results.emplace_back(1, letter);
        for (auto child : children) {
            results.push_back(std::string(1, letter) + child);
        }
    }
    return results;

}

int main()
{
    ofstream f("passwords.dat");
    auto results = make_permutations(6);
    for (auto s : results) f << s << endl;


}

Aucun commentaire:

Enregistrer un commentaire