Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma warning (disable: 4996) //visual studio bug fix, no need on code
- #include <stdio.h> //printf
- #include <stdlib.h> //malloc
- int isValid(const char* argument, int length) {
- for (int i = 0; i < length; i++) {
- if (argument[i] == '?' ||
- i > 0 && i < length - 1 && argument[i - 1] == argument[i] && argument[i] == argument[i + 1])
- return 0; //0 == false
- }
- return 1; //1 == true
- }
- void printAllBinaryStrings(char* input, int length, const char* toPrint, const int lengthConst) {
- for (int i = 0; i < length; i++) {
- if (input[i] == '?') {
- input[i] = '0';
- printAllBinaryStrings(input + i, length - i, toPrint, lengthConst);
- input[i] = '1';
- printAllBinaryStrings(input + i, length - i, toPrint, lengthConst);
- input[i] = '?'; //important!
- }
- if (isValid(toPrint, lengthConst) == 1)
- printf("%s\n", toPrint);
- }
- }
- int main() {
- int length = 0;
- scanf("%d", &length);
- ++length; //add '\0'
- char* input = (char*)malloc(sizeof(char) * length);
- scanf("%s", input);
- printAllBinaryStrings(input, --length, input, length);
- free(input);
- }
Add Comment
Please, Sign In to add comment