Stoycho_KK

recursive

Oct 20th, 2021 (edited)
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #pragma warning (disable: 4996) //visual studio bug fix, no need on code
  2.  
  3. #include <stdio.h> //printf
  4. #include <stdlib.h> //malloc
  5.  
  6. int isValid(const char* argument, int length) {
  7.     for (int i = 0; i < length; i++) {
  8.         if (argument[i] == '?' ||
  9.             i > 0 && i < length - 1 && argument[i - 1] == argument[i] && argument[i] == argument[i + 1])
  10.             return 0; //0 == false
  11.     }
  12.     return 1; //1 == true
  13. }
  14.  
  15. void printAllBinaryStrings(char* input, int length, const char* toPrint, const int lengthConst) {
  16.     for (int i = 0; i < length; i++) {
  17.         if (input[i] == '?') {
  18.             input[i] = '0';
  19.             printAllBinaryStrings(input + i, length - i, toPrint, lengthConst);
  20.             input[i] = '1';
  21.             printAllBinaryStrings(input + i, length - i, toPrint, lengthConst);
  22.             input[i] = '?'; //important!
  23.         }
  24.  
  25.         if (isValid(toPrint, lengthConst) == 1)  
  26.             printf("%s\n", toPrint);
  27.     }
  28. }
  29.  
  30. int main() {
  31.     int length = 0;
  32.  
  33.     scanf("%d", &length);
  34.  
  35.     ++length; //add '\0'
  36.  
  37.     char* input = (char*)malloc(sizeof(char) * length);
  38.  
  39.     scanf("%s", input);
  40.  
  41.     printAllBinaryStrings(input, --length, input, length);
  42.  
  43.     free(input);
  44. }
  45.  
Add Comment
Please, Sign In to add comment