Created
March 6, 2018 13:35
-
-
Save zxmarcos/6ed638036de2835dff0979b49aa4e268 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// generator.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <experimental\generator> | |
#include <string> | |
#include <numeric> | |
#include <vector> | |
#include <cstdint> | |
#include <sstream> | |
#include <iostream> | |
#include <utility> | |
#include <stack> | |
using namespace std; | |
using generator = experimental::generator<string>; | |
const string alphabet = "abcdefghijklmnopqrstuvwxyz"; | |
generator all_strings() | |
{ | |
size_t set_size = 1; | |
for (;; ++set_size) { | |
stack<pair<string, size_t>> stack; | |
stack.push(make_pair(string(""), set_size)); | |
do { | |
const pair<string, size_t> p = stack.top(); | |
stack.pop(); | |
if (p.second == 0) { | |
co_yield p.first; | |
} | |
else { | |
for (size_t i = 0; i < alphabet.size(); ++i) { | |
string new_prefix = p.first + alphabet[i]; | |
stack.push(make_pair(new_prefix, p.second - 1)); | |
} | |
} | |
} while (!stack.empty()); | |
} | |
} | |
int main() | |
{ | |
for (auto p : all_strings()) { | |
cout << p << endl; | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment