CF1399D Binary String To Subsequences
Idea:
First define the a and b queues, which represent the end of 0 and the end of 1, respectively. If a 0 comes in, first determine whether the b queue is empty, and if it is not empty, move the first element of the b queue to the a queue, and store the element The sequence number array, if empty, the counter is +1, add an element equal to cnt in the a queue, store cnt in the sequence number array, and if 1 comes in, the opposite is true.
Code:
#include<bits/stdc++.h>
using namespace std;
int t,cnt,n,i,l,x[200001];
string s;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>t;
while(t--){
queue<int>a,b;
cnt=0;
cin>>n>>s;
for(i=0;i<n;i++){
if(s[i]=='0'){
if(b.empty()){
cnt++;
x[i]=cnt;
a.push(cnt);
}
else{
x[i]=b.front();
b.pop();
a.push(x[i]);
}
}
else{
if(a.empty()){
cnt++;
x[i]=cnt;
b.push(cnt);
}
else{
x[i]=a.front();
a.pop();
b.push(x[i]);
}
}
}
cout<<cnt<<endl;
for(i=0;i<n;i++)
cout<<x[i]<<' ';
cout<<endl;
}
return 0;
}