samedi 6 octobre 2018

How to do Next_permutation from the end?

I'm implementing a program and I need a function that generates the permutation from the longest to the smallest. For example, if I have the letters "a, b, c, d, e, f, g" with the standard "Next Permutation", the results will be:

a->ab->abc etc

What I need, instead of it, would be something like:

abcdefg->abcdef->abcde-> abcd ecc

An option would also be all the permutation of the maximun size (in this case 7), then all the permutation of size-1 (6) and so on.

Here is the standard "Next permutation" that I don't know how to modify it

 std::string letters{"abcdefg"};
    std::sort(letters.begin(), letters.end());
    do {
        for (std::size_t i=letters.length; i <= letters.length(); ++i) {
            std::cout << letters.substr(0, i) << std::endl;
        }
    } while (std::next_permutation(letters.begin(), letters.end()));

Aucun commentaire:

Enregistrer un commentaire