views:

86

answers:

5
+2  Q: 

Strings Generation

Hi,I would like to know how can I create various string from some given characters eg:

given characters: a, b

I would like to generate the following strings:

aa
ab
ba
bb

What I have thought of is having (for 2 inputs only) two for-loops one inside another, and then loop each to the number of inputs which in this case is 2 and the output strings will be 2*2 = 4 strings and as the number increases the number of output strings will increase by multiplying n*n (n-times)

A: 

I think this is a permutation question..

http://www.bearcave.com/random_hacks/permute.html

Randy
This is simpler than permutations - permutations don't allow repetition
BlueRaja - Danny Pflughoeft
+1  A: 

This is a variation of the Kleene closure (Kleene star) if that helps you in finding a solution.

Ólafur Waage
+1  A: 

Your approach (as much as I understood of it) sounds good as a first attempt, although the proof of the pudding is eating it, so write the code and test it :-)

Note that it won't scale very well, so the question is how many chars and how long strings you expect to be generated. If the answer is "not many", and performance / memory consumption is not an issue, it is fine to stick with the simplest solution which works. Otherwise, you need a more sophisticated algorithm.

I've had a vaguely similar task some time ago, where the number of possible permutations was so large that there was simply not enough memory to contain each at the same time. So we tried to model the permutations with numbers: note that any n long permutation of m characters can be defined with an m base number of n digits. So by iterating through all integer values from 0 until mn, calling a fairly simple conversion method gets you each possible string one by one. For the index value of course you might need to use a bigger integer type like long long for bigger m and n values.

Péter Török
well, the number of characters may vary from 40 to 60 and the string length is up t 16 characters ... I was able to mak eonly the first set of strings yet, I couldn`t move on to the second set of strings.
sikas
@sikas: To generate every 16-character word from an alphabet of 60 letters would require 25,657,845,139,503,479 terabytes of memory. I think you may want take another look at your requirements (If you are hoping to brute-force a 16-character password, you're pretty much out of luck - even with every computer on earth, it would take about 100,000 years)
BlueRaja - Danny Pflughoeft
So it is not possible to generate all possible combinations for high number of input characters and a big lengthed strings ...
sikas
@sikas, it is possible indeed, just you can't keep _all_ permutations in memory at the same time.
Péter Török
@Péter Török: ok, first sorry for being late n responding as I`m working on my grad project beside this one ... so I can just send the output (each permutation) to a file in append mode? I guess this will save memory.
sikas
@sikas, you would need a rather big hard disk to store that file :-) See @BlueRaja's calculations above. Big as the result seems, it appears to me that it is even off by a factor of (at least) 16, as this is only the _count_ of different permutations (i.e. 60^16), and you need at least 16 bytes to store each... not that it matters much at this scale :-)
Péter Török
@Péter Török: will it require the same memory if I just perform operations on each combination without having to store?
sikas
@sikas, no it won't, that's why I suggested it.
Péter Török
A: 

With recursion (just an idea demonstration):

void generate(std::string& s, size_t beg) {
  for (char c = 'a'; c <= 'b'; ++c) {
    s[beg] = c;
    if (beg < s.length() - 1) {
      generate (s, beg + 1);
    }
    else {
      std::cout << s << std::endl;
    }
  }
}

int main() {
  std::string s = "####";
  generate(s, 0);
  return 0;
}
bobah
A: 

You can use std::next_permutation. This will work even if you repeat letters (i.e. letters = "ababcd").

#include <algorithm>
#include <iostream>
#include <string> 


int main(int argc, char** argv) {
    std::string letters = "abcd";

    std::sort(letters.begin(), letters.end());
    do {
        std::cout << letters << "\n";
    }
    while (std::next_permutation(letters.begin(), letters.end()));
}
lmmilewski
+1 for using <algorithm>
rubenvb
-1 for giving incorrect results - this outputs two lines for OP's example, which should have four. He's not asking for permutation...
BlueRaja - Danny Pflughoeft
sorry, I don't know why i thought he was asking for permutations ;-/
lmmilewski