cf 950C - Zebras

本文介绍了一种用于将01字符串按特定规则分组的算法。该算法的目标是将字符串分割成若干子序列,每个子序列需以0开头并以0结尾,且0和1交替出现。通过使用两个队列来实现子序列的构建与输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值