Skip to content

Instantly share code, notes, and snippets.

@zxmarcos
Created March 6, 2018 13:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zxmarcos/6ed638036de2835dff0979b49aa4e268 to your computer and use it in GitHub Desktop.
Save zxmarcos/6ed638036de2835dff0979b49aa4e268 to your computer and use it in GitHub Desktop.
// 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