-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackTracking.cpp
More file actions
133 lines (119 loc) · 1.85 KB
/
BackTracking.cpp
File metadata and controls
133 lines (119 loc) · 1.85 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <bits/stdc++.h>
#define _ 0
using namespace std;
int n, k, s, sum, ans;
int a[100];
bool used[100], check;
vector<string> v;
string w;
set<string> se;
bool cmp(string& a, string& b) {
if (a.size() == b.size()) {
return a < b;
}
return a.size() < b.size();
}
void output_1() {
for (int i = 1; i <= n; ++i) {
if (a[i]) {
cout << v[a[i]];
}
}
cout << "\n";
}
void output_2() {
if (v[a[n]] == w) {
for (int i = 1; i <= n; ++i) {
cout << v[a[i]] << " ";
}
cout << "\n";
}
}
void output_3() {
for (int i = 1; i <= k; ++i) {
cout << a[i] << " ";
}
cout << "\n";
}
void Try(string s, int demA, int demB, int demC) {
if (s.size() <= n) {
if (demA && demB && demC && (demA <= demB) && (demB <= demC)) {
v.push_back(s);
}
if (s.size() == n) return;
}
Try(s+"A", demA+1, demB, demC);
Try(s+"B", demA, demB+1, demC);
Try(s+"C", demA, demB, demC+1);
}
void Try_1(int i) {
for (int j = 0; j <= 1; ++j) {
a[i] = j;
if (i == n) {
output_1();
}
else {
Try_1(i+1);
}
}
}
void Try_2(int i) {
for (int j = 1; j <= n; ++j) {
if (!used[j]) {
a[i] = j;
used[j] = true;
if (i == n) {
output_2();
}
else
Try_2(i+1);
used[j] = false;
}
}
}
void Try_3(int i) {
for (int j = a[i-1]+1; j <= n-k+i; ++j) {
a[i] = j;
if (i == k) {
output_3();
}
else {
Try_3(i+1);
}
}
}
void out() {
for (int i = 1; i <= n; ++i) {
if (used[i]) {
cout << a[i] << " ";
}
}
cout << "\n";
}
void Try(int i, int x) {
for (int j = x; j <= n; ++j) {
sum += a[j];
used[j] = true;
if (sum <= s) {
if (sum == s) {
out();
check = true;
ans = min(ans, i);
}
else
Try(i+1, j+1);
}
sum -= a[j];
used[j] = false;
}
}
int main() {
cin >> n;
v.push_back("");
for (int i = 1; i <= n; ++i) {
string s; cin >> s;
v.push_back(s);
}
Try_1(1);
return (0^_^0);
}