0

I am trying to make a brute-force algorithm in C++, to solve problems. I have previously made a brute-force algorithm in Python but this used a 3rd party library, which means I can't convert it to C++. I quite like this design, that I have found;

#include <iostream>

using namespace std;

int main() {
    string characters = "abcde";

    int length = 5;
    string word = "";
    for(int i = word.length(); i <= length; i++) {
        for(int l = 0; l < characters.length(); l++) {
            word += characters[l];
            cout << word << "\n";
        }
    }
    return 0;
}

, but due to some bug(s), its output is:

abcdeabcde
abcdeabcdea
abcdeabcdeab
abcdeabcdeabc
abcdeabcdeabcd
abcdeabcdeabcde

and so on... The result, that I need is:

a
b
c
d
e
aa
ab
ac
ad
ae
ba
bb
bc
...

Thanks In Advance :)

Any help is appreciated :)

4
  • 1
    that's not the output of your code Commented Jul 31, 2017 at 19:14
  • you want to make all possible permutations of that string and something more ... Commented Jul 31, 2017 at 19:15
  • Your not iterating through the string each time. I would use an array of chars for this. Commented Jul 31, 2017 at 19:16
  • A much better phrasing for this question is: "I want to generate all words in a language given an alphabet" en.wikipedia.org/wiki/Formal_language (although as I worded it it is a bit confusing) Commented Jul 31, 2017 at 19:19

1 Answer 1

7

Your approach to generating all permutations is fundamentally flawed. Even if the bugs in your code were fixed, it would not behave the way you want.

Simply put, with a 2-level loop, you'll never hit the "aaa" permutation.

I would personally recommend a recursive approach, here's a rough starting point you can work around:

#include <iostream>
#include <string>

void visit(std::string const& chars, size_t max_len, std::string const& cur) {
    if(cur.length() == max_len) {
      return;
    }
    else {
      for(auto c : chars) {
        std::string next = cur + c;
        std::cout << next << std::endl;

        visit(chars, max_len, next);
      }
    }
}

int main() {
  visit("abcde", 5, "");
  return 0;
}
Sign up to request clarification or add additional context in comments.

19 Comments

me very much like this recursive approach :)
I am not the type of person whom copies someone's work and claims it as my own, I love programming and learning by myself but I was absolutely clueless. Thanks mate, this really helps! I dont use stack overflow much, how do I give you reputation points?
Just mark my answer as accepted (stackoverflow.com/help/someone-answers). Glad to be of help.
That depends on the version (msdn.microsoft.com/en-us/library/hh567368.aspx). The feature I'm using is called "Range-based for-loop". According to that matrix, it was introduced in MSVC 2012.
Changing my code to use a traditional for loop is pretty trivial. I'm sure you can take it from here.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.