-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1996 S3.cpp
91 lines (59 loc) · 2.48 KB
/
1996 S3.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
/*
1996 S3 - Pattern Generator
Difficulty: Easy
For this problem, there's 2 main ways of solving it.
You either make use of the trick or you solve it via recursion. This file contains the trick, the other file contains the recursion solution.
The trick is simple, to generate all bit patterns.
Find the last occurence of the substring "10", swap them and the for the right side of the string from the substring "10", reverse it.
For example, imagine you start with 1001.
Last occurence of "10" is at the start. So you swap them.
Now you have 0101, this isn't completely correct yet, since now we need to reverse the right side after the "10" we had swapped.
Reversing the right side, we get 0110, which is correct.
*/
#include <iostream>
#include <string>
int main(){
int numOfTests;
std::cin >> numOfTests;
for (int i = 0; i < numOfTests; i++){
std::string pattern = ""; //Start with blank
int n, k;
std::cin >> n >> k;
//Initialize the beginning string, we will use the trick afterward to obtain all other strings
for (int j = 0; j < k; j++){
pattern += '1';
}
for (int j = k; j < n; j++){
pattern += '0';
}
std::cout << "The bit patterns are" << '\n' << pattern << '\n'; //Print first pattern
while (true){
std::size_t onezero = pattern.rfind("10"); //Position of substring "10"
//If no occurence of "10"
if (onezero == std::string::npos){
break;
}
//Swap characters
char backup = pattern[onezero];
pattern[onezero] = pattern[onezero + 1];
pattern[onezero + 1] = backup;
//Find right side
std::string right_side = pattern.substr(onezero + 2, n - onezero + 2);
std::string right_side_reversed;
//Reverse right side
for (auto r_it = right_side.rbegin(); r_it != right_side.rend(); r_it++){
right_side_reversed += *r_it;
}
//Remove old right side and add the reversed one
pattern.erase(pattern.begin() + onezero + 2, pattern.end());
pattern += right_side_reversed;
//Print out the pattern
std::cout << pattern << '\n';
}
//To avoid presentation error, only print out new line on all cases except for the last
if (i < numOfTests - 1){
std::cout << '\n';
}
}
return 0;
}