-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeBruijnSequence.cpp
More file actions
42 lines (37 loc) · 881 Bytes
/
DeBruijnSequence.cpp
File metadata and controls
42 lines (37 loc) · 881 Bytes
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
//De Bruijn Sequence - https://cses.fi/problemset/task/1692
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
void solve() {
int n;
cin >> n;
string ans;
set<string> seen;
auto dfs = [&](auto dfs, string& current) -> void {
for (char c = '0'; c <= '1'; c++) {
string next = current + c;
if (seen.find(next) == seen.end()) {
seen.insert(next);
next = next.substr(1);
dfs(dfs, next);
ans.push_back(c);
}
}
};
string start = string(n - 1, '0');
dfs(dfs, start);
ans += start;
cout << ans << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
// cin >> T;
while (T--) {
solve();
}
return 0;
}