cf 949A Zebras

4 篇文章 0 订阅

一 原题

A. Zebras
time limit per test
1 second
memory limit per test
512 megabytes
input
standard input
output
standard output

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 001001010 are zebras, while sequences 101100101 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
Copy
0010100
output
3
3 1 3 4
3 2 5 6
1 7
input
Copy
111
output
-1


二 分析

题意:给定一个长度不超过2e5的01字符串s,zebra串的形式为0(01)*0,问s能否划分成若干子序列,每个子序列都是一个zebra串。

分析:每个zebra串中0的数量比1的数量多1,所以如果答案如果是可行,那么s中1的数量减去0的数量k就是子序列的数量。如果k<=0,肯定无解。假设有k个桶对应k个子序列,我们的任务就是把s中的每个字符放到一个桶中。桶可以分成两类:下一个字符需要0的和下一个字符需要1的。初始化时每个桶都是要0的,当0放入某个桶后,它成为需要1的那一类;当1放入某个桶后,它成为需要0的那一类。在放字符的过程中可能发生当前字符为x,而不存在需要x的桶,那么此时就可以判定无解了。最后,每个桶都应该进入需要1的状态(zebra串以0结束),如果此时还有需要0的桶,那么也无解。

其实有解的条件等价于对于s的任意前缀和后缀,其中0的数量要大于等于1的数量。满足该条件时,在上述算法中一定不会出现当前字符为x而没有需要x的桶的情况。


三 代码

/*
AUTHOR: maxkibble
LANG: c++ 17
PROB: cf 949A
*/

#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>

#define pb push_back

const int maxn = 2e5 + 10;

char s[maxn];
int n, num;
std::queue<int> q0, q1;
std::vector<int> ans[maxn];

int main() {
    scanf("%s", s + 1);
    n = strlen(s + 1);
    for (int i = 1; i <= n; i++) {
        if (s[i] == '0') num++;
        else num--;
    }
    if (num <= 0) {
        puts("-1");
        return 0;
    }
    for (int i = 0; i < num; i++) q0.push(i);
    for (int i = 1; i <= n; i++) {
        if (s[i] == '0') {
            if (q0.empty()) {
                puts("-1");
                return 0;
            }
            int x = q0.front(); q0.pop();
            q1.push(x);
            ans[x].pb(i);
        }
        else {
            if (q1.empty()) {
                puts("-1");
                return 0;
            }
            int x = q1.front(); q1.pop();
            q0.push(x);
            ans[x].pb(i);
        }
    }
    if (!q0.empty()) {
        puts("-1");
        return 0;
    }
    printf("%d\n", num);
    for (int i = 0; i < num; i++) {
        printf("%d", ans[i].size());
        for (int item: ans[i]) printf(" %d", item);
        puts("");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值