Блог пользователя mr.madhavnagpal

Автор mr.madhavnagpal, история, 6 лет назад, По-английски

// 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;

}

Полный текст и комментарии »

  • Проголосовать: нравится
  • -20
  • Проголосовать: не нравится