Codeforces-C. Zebras-0101010

C. 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 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
Copy
0010100
output
3
3 1 3 4
3 2 5 6
1 7
input
Copy
111
output
-1

 比赛时一直超时,当时的想法就是一边一边地搜索0,1添加到vec中,果断超时。

后来想到一次遍历实现将0,1放到vec中,不过每次放要从第一个开始搜索,符合条件就放进去,这也同样会超时。

看了一下别人的解析,瞬间感悟,一次遍历时,根本不需要从vec数组的头部开始搜索,完全可以在上一次放置的基础上放入,实现了内部O(1)时间复杂度的可能性。

其实就是将vec数组分为两部分,前一部分末尾是0,后一部分末尾是1,当前在两部分的交界处,遇0则往后放,遇1则往前放,实现了动态填补。

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define MAX 2*100005
 6 
 7 string str;
 8 vector<int> vec[MAX];
 9 
10 int main() {
11     cin>> str;
12     int len = str.length();
13     int cnt = 0;
14     int flag = 1;
15     int cntMax = 0;
16     for(int i = 0; i < len; i++) {
17         if(str[i] == '0') {
18             vec[cnt++].push_back(i+1);
19             cntMax = max(cntMax, cnt);
20         }
21         else {
22             if(cnt == 0) {
23                 flag = 0;
24                 break;
25             }
26             vec[--cnt].push_back(i+1);
27         }
28     }
29     if(cnt != cntMax) flag = 0;
30     if(!flag) {
31         puts("-1");
32         return 0;
33     }
34     cout<< cntMax<< endl;
35     for(int i = 0; i < cntMax; i++) {
36         int siz = vec[i].size();
37         printf("%d", siz);
38         for(int j = 0; j < siz; j++) {
39             printf(" %d", vec[i][j]);
40         }
41         puts("");
42     }
43     return 0;
44 }

探究0,010,01010..的特点,可以看出总是0的个数比1的个数多1个,所以,我们可以从输入的字符串看出有多少个zebra字符串,自然是num(0)-num(1)个。

原文地址:https://www.cnblogs.com/ACMessi/p/8544031.html