Binary String to Subsequences, Round 661, div3

Revision en1, by mr.madhavnagpal, 2020-08-06 11:45:27

// I am Nothing, You Are Nothing! // Krishna is Everything ... // Jai Shree Krishna

include <bits/stdc++.h>

using namespace std;

define int long long

const int N = 1e5 + 5, mod = 1e9 + 7;

void solve() { int n; cin >> n;

vector<int> a(n);
stack<int> ones, zeroes;
int count = 0;

char ch;
for (int i = 0; i < n; i++) {
    cin >> ch;
    int x = ch - '0';
    if (x == 0) {
       if (!zeroes.empty()) {
         a[i] = zeroes.top();
         zeroes.pop();
         ones.push(a[i]);
       } else {
         a[i] = ++count;
         ones.push(count);
       }
    } else {
       if (!ones.empty()) {
         a[i] = ones.top();
         ones.pop();
         zeroes.push(a[i]);
       } else {
         a[i] = ++count;
         zeroes.push(count);
       }
    }
}

cout << count << endl;
for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
}
cout << endl;

}

int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL);

int test;
cin >> test;

for (int i = 1; i <= test; i++) {
    solve();
}

return 0;

}

Tags #stack

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English mr.madhavnagpal 2020-08-06 11:45:27 1070 Initial revision (published)