Skip to content

Instantly share code, notes, and snippets.

@warm-ice0x00
Created July 8, 2023 16:19
Show Gist options
  • Save warm-ice0x00/ea7a5d4b1f0cc15b7de429e2e54e6b3b to your computer and use it in GitHub Desktop.
Save warm-ice0x00/ea7a5d4b1f0cc15b7de429e2e54e6b3b to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iterator>
#include <stack>
#include <string>
#include <vector>
class BruteForceIterator
: public std::iterator<std::input_iterator_tag, std::string> {
public:
BruteForceIterator() : length(0), done(true) {}
BruteForceIterator(int length, const std::vector<std::string>& strings)
: length(length), strings(strings), done(false) {
for (std::vector<std::string>::const_reverse_iterator it = strings.rbegin();
it != strings.rend(); ++it) {
stack.push(*it);
}
++*this;
}
BruteForceIterator& operator++() {
if (!done) {
if (!stack.empty()) {
current_string = stack.top();
stack.pop();
if (current_string.size() < length) {
for (std::vector<std::string>::const_reverse_iterator it =
strings.rbegin();
it != strings.rend(); ++it) {
stack.push(current_string + *it);
}
}
} else {
done = true;
}
}
return *this;
}
std::string operator*() const { return current_string; }
bool operator==(const BruteForceIterator& other) const {
return done == other.done;
}
bool operator!=(const BruteForceIterator& other) const {
return !(*this == other);
}
private:
size_t length;
std::vector<std::string> strings;
std::stack<std::string> stack;
std::string current_string;
bool done;
};
class BruteForce {
public:
BruteForce(int length, const std::vector<std::string>& strings)
: length(length), strings(strings) {}
BruteForceIterator begin() { return BruteForceIterator(length, strings); }
BruteForceIterator end() { return BruteForceIterator(); }
private:
int length;
std::vector<std::string> strings;
};
int main() {
std::vector<std::string> strings;
strings.push_back("a");
strings.push_back("b");
strings.push_back("c");
BruteForce brute_force(3, strings);
for (BruteForceIterator it = brute_force.begin(); it != brute_force.end();
++it) {
std::cout << *it << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment