Oleg writes down the history of the days he lived. For each day he decides if it was good or bad. Oleg calls a non-empty sequence of days a zebra, if it starts with a bad day, ends with a bad day, and good and bad days are alternating in it. Let us denote bad days as 0 and good days as 1. Then, for example, sequences of days 0, 010, 01010 are zebras, while sequences 1, 0110, 0101 are not.
Oleg tells you the story of days he lived in chronological order in form of string consisting of 0 and 1. Now you are interested if it is possible to divide Oleg’s life history into several subsequences, each of which is a zebra, and the way it can be done. Each day must belong to exactly one of the subsequences. For each of the subsequences, days forming it must be ordered chronologically. Note that subsequence does not have to be a group of consecutive days.
给定一个01字符串,需要你把它分为k个子序列,其中k可以为任意正整数。
对子序列的要求为以0开始,以0结束。0,1相间
输出满足条件的一种结果即可。
对0结尾和1结尾的开两个队列互相转换模拟即可。。
#include<bits/stdc++.h>
using namespace std;
const int MAXN=2e5+5;
char s[MAXN];
vector<int>v[MAXN];
queue<int>q1,q2;
int cnt=0;
int main(){
// freopen("txt.out","r",stdin);
// freopen("orzmthq.out","w",stdout);
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<len;i++){
if(s[i]=='0'){
if(q2.size()){
v[q2.front()].push_back(i+1);
q1.push(q2.front()),q2.pop();
}
else {
v[++cnt].push_back(i+1);
q1.push(cnt);
}
}
else {
if(!q1.size()){puts("-1");return 0;}
else {
v[q1.front()].push_back(i+1);
q2.push(q1.front()),q1.pop();
}
}
}
if(q2.size()){puts("-1");return 0;}
printf("%d\n",cnt);
for(int i=1;i<=cnt;i++){
printf("%d ",v[i].size());
for(int j=0;j<v[i].size();j++){
printf("%d ",v[i][j]);
}
printf("\n");
}
return 0;
}