Re: brute force

Discussion in 'C++' started by David List, Aug 22, 2003.

  1. David List

    David List Guest

    On Fri, 22 Aug 2003 14:40:51 +0300,
    rosty <>
    wrote in <bi4vce$591i2$-berlin.de>:
    > does anybody know how a brute force algorithm looks like? the result should
    > be something like this a, b, c; etc then aa, ab, ac; etc and so on....
    > i imagine there should be a vector of char holding the chars that are going
    > to participate in the brute forcing,
    > vector<char> vc(3);
    > vc[0]='a';
    > vc[1]='b';
    > vc[2]='c';//only three char for simplicity
    >
    > then a string that is going to be the pass itself. then an iteration
    > through the vector and adding those chars to the pass string, and an int
    > holding the actual size of the pass (untill the pass reaches its maximum)
    > and then... i just cannot put it all together, is it so difficult, or am i
    > just so stupid?
    > any code example?


    I think some sort of permutation algorithm could help you out
    here. Luckily there is a built-in function for that in C++. Try
    looking into the next_permutation() function in <algorithm>. If you
    have Stroustrup, 3rd edition, he mentions it in section 18.10.

    --
    Med venlig hilsen / Best regards
    David List
     
    David List, Aug 22, 2003
    #1
    1. Advertising

  2. David List

    rosty Guest

    thanks, but next_permutaion() and prev_permutaion() are usefull ( imho) only
    if you have a fixed length string and wish to have all possible combinations
    of the chars within that string.
    like it says in the book:
    abcd abdc acbd acdb etc....

    the pass string for the brute force is:
    1) of an alterable length
    2) is made from a series of chars usually larger than the resulting string

    i'm still puzzled, and btw, i made of course a google search and the only
    thing i found was people claiming it is simple, but never saw a piece of
    code proving that, maybe it is simple but i'm really puzzled


    "David List" <> wrote in message
    news:...
    > On Fri, 22 Aug 2003 14:40:51 +0300,
    > rosty <>
    > wrote in <bi4vce$591i2$-berlin.de>:
    > > does anybody know how a brute force algorithm looks like? the result

    should
    > > be something like this a, b, c; etc then aa, ab, ac; etc and so on....
    > > i imagine there should be a vector of char holding the chars that are

    going
    > > to participate in the brute forcing,
    > > vector<char> vc(3);
    > > vc[0]='a';
    > > vc[1]='b';
    > > vc[2]='c';//only three char for simplicity
    > >
    > > then a string that is going to be the pass itself. then an iteration
    > > through the vector and adding those chars to the pass string, and an int
    > > holding the actual size of the pass (untill the pass reaches its

    maximum)
    > > and then... i just cannot put it all together, is it so difficult, or am

    i
    > > just so stupid?
    > > any code example?

    >
    > I think some sort of permutation algorithm could help you out
    > here. Luckily there is a built-in function for that in C++. Try
    > looking into the next_permutation() function in <algorithm>. If you
    > have Stroustrup, 3rd edition, he mentions it in section 18.10.
    >
    > --
    > Med venlig hilsen / Best regards
    > David List
     
    rosty, Aug 22, 2003
    #2
    1. Advertising

  3. > thanks, but next_permutaion() and prev_permutaion() are usefull ( imho)
    only
    > if you have a fixed length string and wish to have all possible

    combinations
    > of the chars within that string.


    But that is good for you, because you are dealing with a fixed-length set
    for each iteration. Look:

    set width = 1) a, b, c...
    set width = 2) aa, ab, ac, ad
    set width = 3) aaa, aab, aac

    You see?

    Or are you looking for the BruteForce object? Or perhaps the
    BruteForceForNT object? Well you won't find them. You can't avoid writing
    some code for your self.

    ~
    Jeremy
     
    Jeremy Cowles, Aug 22, 2003
    #3
  4. If you know VB, I have a class that does what you want. Given a specific
    character set, and a result width, it produces every possible combonation of
    the character set in the specified width, similar to next/prev_permutation.
    Let me know if you want me to post it.


    ~
    Jeremy
     
    Jeremy Cowles, Aug 22, 2003
    #4
  5. rosty wrote:
    >
    > thanks, but next_permutaion() and prev_permutaion() are usefull ( imho) only
    > if you have a fixed length string and wish to have all possible combinations
    > of the chars within that string.
    > like it says in the book:
    > abcd abdc acbd acdb etc....
    >
    > the pass string for the brute force is:
    > 1) of an alterable length
    > 2) is made from a series of chars usually larger than the resulting string
    >
    > i'm still puzzled, and btw, i made of course a google search and the only
    > thing i found was people claiming it is simple, but never saw a piece of
    > code proving that, maybe it is simple but i'm really puzzled


    If I understood your problem correctly, then it is indeed simple. It
    is a recursive function.

    What do you want:

    You have a string (initially empty)
    From this string you create new strings
    by adding a character. This will give a
    new string where you have to do the same procedure

    void AddTo( std::string orig )
    {
    std::string new_a = orig + 'a';
    std::cout << new_a << "\n";

    AddTo( new_a );
    }

    The above will create the sequence
    "a" "aa" "aaa" "aaaa" etc.

    Exercises left for the reader:
    replace the 'fixed' set of letters to add (in the above case
    'a') with a variable set (your vector, you will need a loop)

    Add a termination criterium, such that the whole thing stops
    when a specific amount of letters have been added (Hint:
    pass the nesting level as an additional parameter and increment
    it by each recursive function call).

    Don't output the created strings, but store them in a vector.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 22, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Apps
    Replies:
    3
    Views:
    7,860
    Alvin Bruney [MVP - ASP.NET]
    May 26, 2005
  2. Stuart Golodetz

    Re: brute force

    Stuart Golodetz, Aug 22, 2003, in forum: C++
    Replies:
    2
    Views:
    506
    Stuart Golodetz
    Aug 23, 2003
  3. Bas

    Brute force sudoku cracker

    Bas, Sep 16, 2005, in forum: Python
    Replies:
    21
    Views:
    3,276
    Dennis Lee Bieber
    Sep 23, 2005
  4. ago
    Replies:
    11
    Views:
    710
    Anton Vredegoor
    Jan 20, 2006
  5. Replies:
    8
    Views:
    482
Loading...

Share This Page