Advertisement
cunha1

Untitled

Jun 17th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <glib.h>
  3. #include <string.h>
  4. #include <math.h>
  5.  
  6. typedef struct pax {
  7.     char** arr;
  8. } arrpack;
  9.  
  10. char* concat(const char *s1, const char *s2)
  11. {
  12.     char *result = malloc(strlen(s1) + strlen(s2) + 1);
  13.     strcpy(result, s1);
  14.     strcat(result, s2);
  15.     return result;
  16. }
  17.  
  18. void printAllKLengthRec(arrpack* packer, char set[], char prefix[],
  19.                                     int n, int k, int* counter)
  20. {
  21.     if (k == 0)
  22.     {
  23.         int cnt = *counter;
  24.         strcpy(packer->arr[cnt], prefix);
  25.  
  26.         *counter = *counter + 1;
  27.         return;
  28.     }
  29.     for (int i = 0; i < n; i++)
  30.     {
  31.         char str2[2];
  32.         str2[0] = set[i];
  33.         str2[1] = '\0';
  34.         char* newPrefix = concat(prefix, str2);
  35.         printAllKLengthRec(packer, set, newPrefix, n, k - 1, counter);
  36.        
  37.     }
  38.  
  39. }
  40.  
  41. void printAllKLength(arrpack* packer, char set[], int k,int n)
  42. {
  43.     int counter = 0;
  44.     printAllKLengthRec(packer, set, "", n, k,&counter);
  45. }
  46.  
  47. int main(void)
  48. {
  49.     char set[52];
  50.     for(int i=0;i<52;i++) {
  51.         if(i<26)
  52.             set[i] = i+'a';
  53.         else
  54.             set[i] = i-26+'A';
  55.     }
  56.     int n=52;
  57.     int k=5;
  58.     arrpack packer;
  59.     size_t combinations = pow(n,k);
  60.     packer.arr = (char **)malloc(combinations*sizeof(char*));
  61.     for (int i=0; i<combinations; i++)
  62.          packer.arr[i] = (char *)malloc(k+1);
  63.  
  64.  
  65.     printAllKLength(&packer, set, k, n);
  66.  
  67.     for(int i=0;i<combinations;i++) {
  68.         packer.arr[i];
  69.         gchar  *sha512;
  70.         sha512 = g_compute_checksum_for_string(G_CHECKSUM_SHA512, packer.arr[i], k);
  71.  
  72.         printf("%s",packer.arr[i]);
  73.         g_free(sha512);
  74.     }
  75.    
  76.  
  77.    
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement