-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path1399D.cpp
More file actions
executable file
·42 lines (36 loc) · 767 Bytes
/
1399D.cpp
File metadata and controls
executable file
·42 lines (36 loc) · 767 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
// Problem Code: 1399D
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
vector<int> subsequences(int n, string& s) {
vector<int> chain(n);
vector<vector<int>> seq(2);
for (int i = 0; i < n; i++) {
int pos = (s[i] == '1');
if (seq[pos ^ 1].empty())
seq[pos].push_back(seq[pos].size() + 1);
else {
seq[pos].push_back(seq[pos ^ 1].back());
seq[pos ^ 1].pop_back();
}
chain[i] = seq[pos].back();
}
return chain;
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
string s;
cin >> n >> s;
vector<int> chain = subsequences(n, s);
cout << *max_element(chain.begin(), chain.end()) << endl;
for (int i = 0; i < n; i++)
cout << chain[i] << " ";
cout << endl;
}
return 0;
}