[Code Jam 2022 Round 1B] ASeDatAb

ASeDatAb

  • can not pass set 2 😭
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
#include <bits/stdc++.h>

#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("Ofast")//Comment optimisations for interactive problems (use endl)
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")

#define MAX_N 1010
#define ll long long
#define pll pair<ll, ll>
#define vpll vector<pll>
#define vll vector<ll>
#define vs vector<string>
#define vvs vector<vs>
#define vvll vector<vll>
#define all(a) begin(a), end(a)
using namespace std;

bool require(unordered_set<string>& us, string s) {
string ss = s + s;
for(ll i = 0; i < 8; i++) {
if(us.count(ss.substr(i,8)))
return false;
}
return true;
}

vs build(ll n) {
string s = string(8 - n,'0') + string(n,'1');
unordered_set<string> cand;
do if(require(cand, s)) {
cand.insert(s);
}while(next_permutation(all(s)));

return vs(all(cand));
}

ll pick(vs& cand, vll& adj) {
for(ll i = 0; i < cand.size(); i++)
if(adj[i] <= 8)
return i;
return -1;
}

ll miPick(vs& cand, vector<unordered_set<string>>& adj) {
for(ll i = 0; i <= 8; i++) {
if(adj[i].empty()) continue;
for(ll j = 0; j < cand.size(); j++)
if(adj[i].count(cand[j]))
return j;
}
return 0;
}

void solve(vvs cand) {
ll n, i = 0, j = 0;
vvll adj(10, vll(10, 0));
vector<vector<unordered_set<string>>> to(10, vector<unordered_set<string>>(10));
string s;
while(true) {
cin>>n;
if(n == -1) exit(0);
else if(n == 0) break;
adj[i][j]++;
to[i][n].insert(cand[i][j]);
ll p = pick(cand[n], adj[n]);
if(p != -1) {
i = n, j = p;
s = cand[i][j];
} else {
i = n, j = miPick(cand[n], to[n]);
s = cand[i][j];
}
cout<<s<<endl;
}
}

int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.setf(ios::fixed);
cout.precision(6);
ll tc;
vvs cand(9);
for(ll i = 0; i <= 8; i++) {
cand[i] = build(i);
}
cin>>tc;
while(tc--) {
cout<<"00000000"<<endl;
solve(cand);
}

return 0;
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/04/25/PS/Google/ASeDatAb/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.