#include <iostream>
#include <iterator>
#include <algorithm>
#include <string>
char letters[] = {'a', 'b', 'c'};
template <typename T, std::size_t N>
std::size_t array_size(T (&)[N])
{ return N; }
bool next(std::string& in)
{
for(std::size_t i = 0; i < in.size(); ++i)
if(in[i] == letters[array_size(letters) - 1]){ //if current letter is last in set ...
in[i] = letters[0]; //Then we assign first letter and let loop continue
} else {
std::size_t pos = std::distance(std::begin(letters),
std::find(std::begin(letters), std::end(letters), in[i]));
in[i] = letters[++pos]; //Else we assign next letter in set and return true
return true; //As indication that it is not the last possible string
}
return false; //If loop finished, we exhausted all strings
}
void print_all()
{
constexpr std::size_t max = 4;
for(std::size_t size = 1; size <= max; ++size) {//Generate sequences of all possible length
//Create string of correspomding length. Note that we CANNOT use space as initializer
//Or nothing would work at all.
std::string guess(size, letters[0]);
bool again = false;
do { //Inner loop, generates all possible variation of given length
again = next(guess); //Generate next string
std::cout << guess << '\n';
} while( again );
std::cout << '\n';
}
}
int main ()
{
print_all();
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aXRlcmF0b3I+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxzdHJpbmc+CgpjaGFyIGxldHRlcnNbXSA9IHsnYScsICdiJywgJ2MnfTsKCnRlbXBsYXRlIDx0eXBlbmFtZSBULCBzdGQ6OnNpemVfdCBOPgpzdGQ6OnNpemVfdCBhcnJheV9zaXplKFQgKCYpW05dKQp7IHJldHVybiBOOyB9Cgpib29sIG5leHQoc3RkOjpzdHJpbmcmIGluKQp7CiAgICBmb3Ioc3RkOjpzaXplX3QgaSA9IDA7IGkgPCBpbi5zaXplKCk7ICsraSkKICAgICAgICBpZihpbltpXSA9PSBsZXR0ZXJzW2FycmF5X3NpemUobGV0dGVycykgLSAxXSl7IC8vaWYgY3VycmVudCBsZXR0ZXIgaXMgbGFzdCBpbiBzZXQgLi4uCiAgICAgICAgICAgIGluW2ldID0gbGV0dGVyc1swXTsgLy9UaGVuIHdlIG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