C. Zebras
time limit per test 1 second
memory limit per test 512 megabytes
Oleg writes down the history of the days he lived. For each day he decides if it was good orbad. 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 daysare alternating in it. Let us denote bad days as 0 and good days as 1. Then, forexample, sequences of days 0, 010, 01010 are zebras, while sequences 1, 0110, 0101 are not.
Oleg tells youthe story of days he lived in chronological order in form of string consistingof 0 and 1. Now you are interested if it ispossible to divide Oleg's life history into several subsequences, each of which is a zebra, and the way itcan be done. Each day must belong to exactly one of the subsequences. For eachof the subsequences, days forming it must be ordered chronologically. Note thatsubsequence does not have to be a group of consecutive days.
Input
In the only lineof input data there is a non-empty string s consisting of characters 0 and 1, which describes the history of Oleg's life. Its length (denoted as |s|) does not exceed 200 000 characters.
Output
If there is away to divide history into zebra subsequences, in the first line of output youshould print an integer k (1 ≤ k ≤ |s|), the resulting number of subsequences. In the i-th of following k lines firstprint the integer li (1 ≤ li ≤ |s|), which is the length of the i-th subsequence, and then li indices of days forming the subsequence. Indices must follow in ascendingorder. Days are numbered starting from 1. Each index from 1 to n must belong toexactly one subsequence. If there is no way to divide day history into zebrasubsequences, print -1.
Subsequences may be printed in anyorder. If there are several solutions, you may print any of them. You do nothave to minimize nor maximize the value of k.
Examples
Input
Copy
0010100
Output
3
3 1 3 4
3 2 5 6
1 7
Input
Copy
111
Output
-1
【题意】
给定一个01字符串,需要你把它分为k个子序列,其中k可以为任意正整数。
对子序列的要求为
-
以0开始,以0结束
-
0,1相间
输出满足条件的一种结果即可。
【思路】
显然根据题目的要求,我们对于每个子序列需要先放一个0,然后放一个1....依次重复,最后再放一个0
我们用vector来保存每个子序列的下标集合。
每次遇到0的时候可以先考虑放到最后一个元素为1的容器集合中,如果没有的话放到一个新的vector中。
遇到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("");
}
}