Codeforces 950C Zebras

9 篇文章 0 订阅
1 篇文章 0 订阅

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.

Input

In the only line of 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 a way to divide history into zebra subsequences, in the first line of output you should print an integer k (1 ≤ k ≤ |s|), the resulting number of subsequences. In the i-th of following k lines first print 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 ascending order. Days are numbered starting from 1. Each index from 1 to n must belong to exactly one subsequence. If there is no way to divide day history into zebra subsequences, print -1.

Subsequences may be printed in any order. If there are several solutions, you may print any of them. You do not have to minimize nor maximize the value of k.

Examples
Input
0010100
Output
3
3 1 3 4
3 2 5 6
1 7
Input
111
Output

-1

题意:给你一串01字符串,然后让你分成几种情况:

(1)只有1个0 (2)首和尾为0,中间1010101交替

然后让你输出你分的数量,每一行输出每一个集合的数量 然后输出每一个集合里的元素(注意:按升序)



解题思路:

看到这题,一开始感觉很简单,当我WA了几发后,我就知道难点就在于如何快速的处理出能用0把1给包围住形成题目所要求的串。一开始是暴力模拟,最终超时了。然后我第二个想法就是建边,按照图的思想来做,但是还是超时了。


之后我想到了一个优化,不额为开数组,直接在vis数组上动手脚,但还是超时了。orz.   mmzz.

这时候我就知道了一定是在查询,寻找的时候错了。 然后苦思冥想了许久,也没想出来。最后在大佬的思路提供下,我终于写出来了。

一开始有一些证明我觉得还是可以的,虽然之后没用到:

(1)0的个数一定比1的个数大1个以上,否则这个串就不能被分解了。

(2) 只要你把1给用完了,剩下的可以用做事第一种情况的字符串,直接输出就行。

开一个动态数组 和两个队列,以及一个数组用来记录队列中元素所在的动态数组。

然后就做出来了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=2e5+10;
int n,pre[maxn];
char ch[maxn];
vector<int> ve[maxn];
queue<int> qu1,qu0;
int main(){
	int i,j;
	scanf("%s",ch);
	if(ch[0]=='1'){
		printf("-1\n");return 0;
	}
	int len=strlen(ch);
	while(!qu0.empty()) qu0.pop();
	while(!qu1.empty()) qu1.pop();
	memset(pre,0,sizeof(pre));
	int top=2;
	pre[0]=1;
	ve[1].push_back(0);
	qu0.push(0);
	int flag=1;
	for(i=1;i<len;i++){
		if(ch[i]=='1'){
			if(!qu0.empty()){
				int k=qu0.front();qu0.pop();
				k=pre[k];
				pre[i]=k;
				ve[k].push_back(i);
				qu1.push(i);
			}
			else{
				flag=0;break;
			}
		}
		else{
			if(!qu1.empty()){
				int k=qu1.front();qu1.pop();
				k=pre[k];
				pre[i]=k;
				ve[k].push_back(i);
				qu0.push(i);
			}
			else{
				pre[i]=top;
				qu0.push(i);
				ve[top++].push_back(i);
			}
		}
	}
	if(!qu1.empty()||!flag){
		printf("-1\n");return 0;
	}
	printf("%d\n",top-1);
	for(i=1;i<top;i++){
		printf("%d",ve[i].size());
		for(j=0;j<ve[i].size();j++){
			printf(" %d",ve[i][j]+1);
		}
		printf("\n");
	}
	return 0;
} 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值