-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprob7-1.c
67 lines (55 loc) · 1.41 KB
/
prob7-1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* file: prob7-1.c
* author: David De Potter
* description: extra, problem 7,
* matching bitstrings
*/
#include <stdio.h>
#include <stdlib.h>
void *safeMalloc (int n) {
// checks if memory has been allocated successfully
void *p = malloc(n);
if (p == NULL) {
printf("Error: malloc(%d) failed. Out of memory?\n", n);
exit(EXIT_FAILURE);
}
return p;
}
char *readString (int n) {
// reads a string of n bits
char *bits = safeMalloc((n+1)*sizeof(char));
(void)! scanf("%s", bits);
return bits;
}
void getBitstrings (char *bits, int idx, int n, int count0, int count1) {
// prints, in lexicographical order, all bitstrings of length n
// with no more than 2 consecutive 0s or 1s
// base case
if (idx == n) {
printf("%s\n", bits);
return;
}
// recursive case
switch (bits[idx]) {
case '?':
bits[idx] = '0';
if (count0 < 2) getBitstrings(bits, idx+1, n, count0+1, 0);
bits[idx] = '1';
if (count1 < 2) getBitstrings(bits, idx+1, n, 0, count1+1);
bits[idx] = '?'; // backtrack
break;
case '0':
if (count0 < 2) getBitstrings(bits, idx+1, n, count0+1, 0);
break;
case '1':
if (count1 < 2) getBitstrings(bits, idx+1, n, 0, count1+1);
break;
}
}
int main (int argc, char *argv[]) {
int n;
(void)! scanf("%d ", &n);
char *bits = readString(n);
getBitstrings(bits, 0, n, 0, 0);
free(bits);
return 0;
}