题目链接:点击打开链接

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

题意:给你一个只含01的串。定义,0,010,01010 ····为斑马串(必须以0开头,以0结束)。然后从给定的字符串中构造斑马串,问能否构造。能的话输出构造结果,不能的话输出-1.构造出来的斑马串用的数字的位置 必须单调递增。

题解:用vector模拟。遇到0就加到1后面,遇到1就放到0后面,若1没有0可放输出-1。但是会时间超限。n方的时间复杂度过不去。需要优化。分析题意可知每次放数字,只需要知道放在那个正在构造的斑马串后。所以可以用两个队列来记录当前0或1,应该放在那个斑马串后面。网上题解有一种骚操作。但是我没明白啥意思。但是我感觉我得操作最骚 嘿嘿嘿。

具体实现过程看代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
char s[200005];
int a[200005];
vector<int> v[200005]; // 模拟斑马串
queue<int>q; // 存1的位置的队列
queue<int>q0; // 存0的位置的队列
int main(){for(int i = 0 ; i <= 200005 ; i ++)v[i].clear();while(!q.empty()){q.pop();}while(!q0.empty()){q0.pop();}scanf("%s",s);int len = strlen(s);if(s[0] == '1' || s[len-1] == '1'){  // 若第一个或最后一个为1,那么必定无法构造。 cout << "-1" << endl;return 0;}for(int i = 0 ; i < len ; i ++)a[i] = s[i] - '0';int ans = 1;v[0].push_back(0);q0.push(0);for(int i = 1; i < len ; i ++){if(a[i] == 0){if(q.empty()){ // 没有1可放。那么另起一行  ,然后记录0的位置 q0.push(ans);v[ans ++].push_back(i);}else { // 否者就放到1后面,然后记录0的位置 在那个斑马串后面 v[q.front()].push_back(i);q0.push(q.front());q.pop();}}else {if(q0.empty()){ // 若没有0可放 就输出-1 cout << "-1" << endl;return 0;}else { // 若有 就放到0后面,然后记录1的位置在那个斑马串后面 v[q0.front()].push_back(i);q.push(q0.front());q0.pop();}}}for(int i = 0 ; i < ans ; i ++){ // 判断是否都是斑马串 int end = v[i].size();if(a[v[i][end-1]]){cout << -1 << endl;return 0;}   }//cout << " GG" << endl;printf("%d\n",ans);int sum = 0 ;for(int i = 0 ; i < ans ; i ++){int num = v[i].size();printf("%d ",num);for(int j = 0 ; j < num ; j ++){printf("%d ",v[i][j]+1);}cout << endl;}
}

Codeforces Round #469 (Div. 2) C. Zebras相关推荐

  1. Codeforces Round #469 (Div. 2) A/B

    A题 http://codeforces.com/contest/950/problem/A 题意:给定三个数l,r,a,要我们求min(l+a1,r+a2)的最大值再乘以2:a1+a2<=a. ...

  2. Codeforces Round #469 (Div. 2)

    开学啦! A. Left-handers, Right-handers and Ambidexters time limit per test 1 second memory limit per te ...

  3. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

  4. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  5. 构造 Codeforces Round #302 (Div. 2) B Sea and Islands

    题目传送门 1 /* 2 题意:在n^n的海洋里是否有k块陆地 3 构造算法:按奇偶性来判断,k小于等于所有点数的一半,交叉输出L/S 4 输出完k个L后,之后全部输出S:) 5 5 10 的例子可以 ...

  6. Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解(每日训练 Day.16 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #696 (Div. 2) (A ~ E)超高质量题解 比赛链接:h ...

  7. Codeforces Round #712 Div.2(A ~ F) 超高质量题解(每日训练 Day.15 )

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #712 Div.2(A ~ F) 题解 比赛链接:https:// ...

  8. Codeforces Round #701 (Div. 2) A ~ F ,6题全,超高质量良心题解【每日亿题】2021/2/13

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 目录 A - Add and Divide B - Replace and Keep Sorted C ...

  9. Codeforces Round #700 (Div. 2) D2 Painting the Array II(最通俗易懂的贪心策略讲解)看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 整场比赛的A ~ E 6题全,全部题目超高质量题解链接: Codeforces Round #700 ...

  10. Codeforces Round #699 (Div. 2) F - AB Tree(贪心、树上DP)超级清晰,良心题解,看不懂来打我 ~

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Codeforces Round #699 (Div. 2) F - AB Tree Problem ...

最新文章

  1. 在 Shell 脚本里使用数组
  2. linux文件系统构成部分及磁盘分区要点
  3. const成员函数、const类对象、mutable数据成员
  4. C# 互通操作 (二)基础知识1
  5. 执行Dockerfile构建基础镜像,建立python工作环境
  6. ISP运营商实验室测试机架拓扑搭建经验分享
  7. 图论算法 —— 图论概述
  8. 用qq号获取用户头像和昵称
  9. jQuery Mobile 中文手册 Ajax开发版(2)
  10. 欧姆龙CP/CJ系列PLC包含哪些通讯方式呢?
  11. Python接口自动化测试
  12. 微信公众平台开发(九) 数据库操作
  13. JD-JUI反编译问题
  14. Python Windows发出警报声、蜂鸣器、声音报警
  15. JMETER进行REST API测试(分步指南)
  16. 盛大游戏技术总监徐峥:Unity引擎使用的三种方式
  17. GitHub 标星 6
  18. c/s程序版本自动升级的问题,如何判断client端版本号是否最新,然后从指定ftp服务器down...
  19. Word2016经典视频教程-初级版-曾贤志-专题视频课程
  20. Java后端对接微信支付(微信公众号、PC端扫码)

热门文章

  1. Linux安装R相关包出现icudt error
  2. C#爬虫爬取京东自营笔记本
  3. 数据预处理阶段“不处理”缺失值的思路
  4. 大数据是普惠金融的未来!
  5. Vue之filters传参问题
  6. 数据安全--3--数据安全5A之授权
  7. 智能电视聚好看连接服务器失败,智能电视为什么登录失败? 试试这样做
  8. 活久见:都 2203 年了,你还在使用 word 调试 API
  9. 下载安装electron和electron-builder遇到的问题及部分解决办法
  10. 电子签名、私钥、公钥