Zebras CodeForces - 950C(思维)

借鉴自:

https://www.cnblogs.com/SuuT/p/8619227.html

https://blog.csdn.net/my_sunshine26/article/details/79502152

题意:

给定一个01字符串,需要你把它分为k个子序列,其中k可以为任意正整数。

对子序列的要求为

  1. 以0开始,以0结束

  2. 0,1相间

输出满足条件的一种结果即可。  输出的结果还要从低到高的顺序

扫一遍字符串,若为0就一直竖着往下写0,碰到1就回头往上写,再碰到0 就回头往下写······

判断无法构造的依据:如果写1写得超过了上界就跳出,如果最后写的0不在最下面也跳出

#include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)
 
typedef long long ll;
const int maxn = 200005;
const ll mod = 1e9+7;
const int INF = 1e9;
const double eps = 1e-9;
 
int n,m;
char s[maxn];
vector<int>vec[maxn];
 
int main()
{
    scanf("%s",s+1);
    int len=strlen(s+1);
    int Max=0;
    int zero=0;
    for(int i=1;i<=len;i++)
    {
        if(s[i]=='0') vec[++zero].push_back(i); //zero前面都是以0结尾的
        else
        {
            if(zero==0) return puts("-1"),0;   //确保每个子序列以0开始
            vec[zero--].push_back(i);          //这个位置放了1之后下一次又可以放0了
        }
        Max=max(Max,zero);                     //Max为当前已经用了几个容器,即分成了几个子序列
    }
    if(Max!=zero) return puts("-1"),0;         //确保每个子序列以0结尾
    printf("%d\n",Max);
    for(int i=1;i<=Max;i++)
    {
        printf("%d",vec[i].size());
        for(int j=0;j<vec[i].size();j++)
        {
            printf(" %d",vec[i][j]);
        }
        puts("");
    }
}

 

转载于:https://www.cnblogs.com/WTSRUVF/p/9404646.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值