-
Notifications
You must be signed in to change notification settings - Fork 0
/
SCTDL002.cpp
88 lines (79 loc) · 1.79 KB
/
SCTDL002.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
/**
* @file SCTDL002.cpp
* @author long ([email protected])
* @brief C++ program generates valid binary strings
* Input: 1??0?101
* Output:
* 10000101
* 10001101
* 10100101
* 10101101
* 11000101
* 11001101
* 11100101
* 11101101
* @version 0.1
* @date 2023-03-04
*
* @copyright Copyright (c) 2023
*
*/
#include <iostream>
int arr[10000];
void generateBinString(std::string bin, int len, int index);
void printBinString(std::string bin, int len);
int main()
{
int t;
std::cin >> t;
while(t--)
{
std::string bin;
std::cin >> bin;
int countQM{0};
// Count the question marks
for(int i = 0; i < bin.length(); i++)
{
if (bin[i] == '?')
{
countQM++;
}
}
// Generate the bin strings with length equal to number of question marks
generateBinString(bin, countQM, 0);
}
}
// Function to print the output
void printBinString(std::string bin, int len)
{
int index = 0;
for (int i = 0; i < bin.size(); i++)
{
if(bin[i] != '?')
{
std::cout << bin[i];
} else
{
std::cout << arr[index++];
}
}
std::cout << std::endl;
}
// Function to generate all binary strings
void generateBinString(std::string bin, int len, int index)
{
if (index == len) {
printBinString(bin, len);
return;
}
// First assign "0" at ith position
// and try for all other permutations
// for remaining positions
arr[index] = 0;
generateBinString(bin, len, index+1);
// And then assign "1" at ith position
// and try for all other permutations
// for remaining positions
arr[index] = 1;
generateBinString(bin, len, index+1);
}