-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathD_Binary_String_To_Subsequences.cpp
More file actions
56 lines (46 loc) · 980 Bytes
/
D_Binary_String_To_Subsequences.cpp
File metadata and controls
56 lines (46 loc) · 980 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
string s;
cin >> n >> s;
int k = 1;
vector<int> ans(n);
vector<int> t1, t2; // availability of 0 and 1
for(int i=0;i<n;i++) {
if(s[i] == '1') {
if(t1.size()) {
ans[i] = t1.back();
t1.pop_back();
}
else {
ans[i] = k++;
}
t2.push_back(ans[i]);
}
else {
if(t2.size()) {
ans[i] = t2.back();
t2.pop_back();
}
else {
ans[i] = k++;
}
t1.push_back(ans[i]);
}
}
cout << k - 1 << "\n";
for(auto i : ans) cout << i << " ";
cout << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _;
cin >> _;
while (_-->0) {
solve();
}
return 0;
}