Zebras CodeForces - 950C(思维)

时间:2023-03-09 01:48:12
Zebras CodeForces - 950C(思维)

借鉴自:

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

https://blog.****.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 = ;
const ll mod = 1e9+;
const int INF = 1e9;
const double eps = 1e-; int n,m;
char s[maxn];
vector<int>vec[maxn]; int main()
{
scanf("%s",s+);
int len=strlen(s+);
int Max=;
int zero=;
for(int i=;i<=len;i++)
{
if(s[i]=='') vec[++zero].push_back(i); //zero前面都是以0结尾的
else
{
if(zero==) return puts("-1"),; //确保每个子序列以0开始
vec[zero--].push_back(i); //这个位置放了1之后下一次又可以放0了
}
Max=max(Max,zero); //Max为当前已经用了几个容器,即分成了几个子序列
}
if(Max!=zero) return puts("-1"),; //确保每个子序列以0结尾
printf("%d\n",Max);
for(int i=;i<=Max;i++)
{
printf("%d",vec[i].size());
for(int j=;j<vec[i].size();j++)
{
printf(" %d",vec[i][j]);
}
puts("");
}
}