-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCTDL011.cpp
110 lines (96 loc) · 2.33 KB
/
SCTDL011.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @file SCTDL011.cpp
* @author long ([email protected])
* @brief Generate all binary strings of length n
* with sub-string “01” appearing exactly twice
* @version 0.1
* @date 2023-04-09
*
* @copyright Copyright (c) 2023
*
*/
#include <iostream>
#include <stdlib.h>
using namespace std;
void printBinStr(int* str, int len)
{
for (int i = 0; i < len; i++) {
cout << str[i];
}
cout << endl;
}
// This function will be called recursively
// to generate the next bit for given
// binary string according to its current state
void generateBinStr(int* str, int len, int currlen, int occur, int nextbit)
{
// Base-case: if the generated binary string
// meets the required length and the pattern "01"
// appears twice
if (currlen == len) {
// nextbit needs to be 0 because each time
// we call the function recursively,
// we call 2 times for 2 cases:
// next bit is 0 or 1
// The is to assure that the binary
// string is printed one time only
if (occur == 2 && nextbit == 0) {
printBinStr(str, len);
}
return;
}
// Generate the next bit for str
// and call recursive
if (currlen == 0) {
// Assign first bit
str[0] = nextbit;
// The next generated bit will wither be 0 or 1
generateBinStr(str, len, currlen + 1, occur, 0);
generateBinStr(str, len, currlen + 1, occur, 1);
}
else {
// If pattern "01" occurrence is < 2
if (occur < 2) {
// Set next bit
str[currlen] = nextbit;
// If pattern "01" appears then
// increase the occurrence of pattern
if (str[currlen - 1] == 0 && nextbit == 1) {
occur += 1;
}
generateBinStr(str, len, currlen + 1, occur, 0);
generateBinStr(str, len, currlen + 1, occur, 1);
// Else pattern "01" occurrence equals 2
}
else {
// If previous bit is 0 then next bit cannot be 1
if (str[currlen - 1] == 0 && nextbit == 1) {
return;
// Otherwise
}
else {
str[currlen] = nextbit;
generateBinStr(str, len, currlen + 1, occur, 0);
generateBinStr(str, len, currlen + 1, occur, 1);
}
}
}
}
int main()
{
int t;
cin >> t;
while(t--) {
int n;
cin >> n;
if (n < 4) {
cout << -1 << endl;
}
else {
int* str = new int[n];
generateBinStr(str, n, 0, 0, 1);
generateBinStr(str, n, 0, 0, 0);
}
}
return 0;
}