V
V
Vladislav Orlov2014-04-12 23:07:46
C++ / C#
Vladislav Orlov, 2014-04-12 23:07:46

Generation using combinations of an array of a certain length from elements with a character array type?

Good afternoon!
To investigate the algorithm's worst-case performance, I need to pre-generate the words in a certain sequence: from "zzzz" to "aaaa", depending on the number of words required.
Now it is generated like this:

int count_words = 200000;
char** words = malloc(count_words * sizeof(char*));
// Generate words in worst-case
int index = 0;
for (int a = 0; a < 'z' - 'a'; a++) {
  for (int b = 0; b < 'z' - 'a'; b++) {
    for (int c = 0; c < 'z' - 'a'; c++) {
      for (int d = 0; d < 'z' - 'a'; d++) {
        if (index < 200000) {
          words[index] = malloc(5 * sizeof(char));
          words[index][0] = 'z' - a;
          words[index][1] = 'z' - b;
          words[index][2] = 'z' - c;
          words[index][3] = 'z' - d;
          words[index][4] = '\000';
          index++;
        }
      }
    }
  }
}

And it takes something like this:
int count_words = 200000;
char** words = malloc(count_words * sizeof(char*));
// Generate words in worst-case
for (int i = 0; i < count_words; i++) {
  words[i] = calloc(STR_LEN + 1, sizeof(char));
  for (int k = 0; k < STR_LEN; k++) {
    words[i][k] = 'z' - SOME_ARITHMETICS;
  }
  words[STR_LEN] = '\000';
}

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tsarevfs, 2014-04-13
@tsarevfs

It is possible so .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question