Algorithm to print all combination of letters of the given string in lexicographical order

后端 未结 2 978
心在旅途
心在旅途 2021-01-15 19:17

I tried to create the code to generate all possible combination of the given string in the lexicographical order:

The code that I wrote is:

void get(         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-15 19:50

    Following may help:

    bool increase(std::vector& bs)
    {
        for (std::size_t i = 0; i != bs.size(); ++i) {
            bs[i] = !bs[i];
            if (bs[i] == true) {
                return true;
            }
        }
        return false; // overflow
    }
    
    template 
    void PowerSet(const std::vector& v)
    {
        std::vector bitset(v.size());
    
        do {
            for (std::size_t i = 0; i != v.size(); ++i) {
                if (bitset[i]) {
                    std::cout << v[i] << " ";
                }
            }
            std::cout << std::endl;
        } while (increase(bitset));
    }
    

    Live example

提交回复
热议问题