A. Zebras

time limit per test1 second 
memory limit per test512 megabytes 
inputstandard input 
outputstandard 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 
inputCopy 
0010100 
output 

3 1 3 4 
3 2 5 6 
1 7 
inputCopy 
111 
output 
-1

看了别人的博客后才知道这道题要用vector容器来做,不然开辟的内存会爆掉,同时也让我知道了vector的基础用法

{

  #include<vector>头文件

  vector<int> a;相当于定义了一个大小可变的a数组

  a.size();a内元素的数量

  a.begin();数组a的起始位置

  a.end();数组a的最后一个元素的位置+1

  a.push_back(i);将i放入数组a的末尾

  a.pop_back();删除a末尾的元素

  a.clear();清空数组a;

  a.erase(vec.begin()+5);删除第6个元素

  a.insert(a.begin()+c,b)在第c个插入b

  a.insert(a.begin()+i,b);在第i+1个元素前面插入b

  a.empty();a数组如果为空输出1,非空输出0 

  sort(a.begin(),a.end())//升序排序

  cout<<vec[0]<<endl;使用下标访问元素,记住下标是从0开始的。

  vector<int>::iterator it;迭代器:一个元素指向下一个元素

  例:for(it=a.begin();it!=a.end();it++)

        cout<<*it<<endl;使用迭代器访问元素,输出全部元素

  reverse(a.begin(),a.end());将整个a倒置

}

别的大牛对vector的介绍

{

  在c++中,vector是一个十分有用的容器。

  作用:它能够像容器一样存放各种类型的对象,简单地说,vector是一个能够存放任意类型的动态数组,能够增加和压缩数据。

  vector在C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。

  特别注意:

  使用vector需要注意以下几点:

  1、如果你要表示的向量长度较长(需要为向量内部保存很多数),容易导致内存泄漏,而且效率会很低;

  2、Vector作为函数的参数或者返回值时,需要注意它的写法:

double Distance(vector<int>&a, vector<int>&b) 其中的“&”绝对不能少!!!

  使用sort排序:需要头文件#include<algorithm>,

  sort(vec.begin(),vec.end());(默认是按升序排列,即从小到大).

  可以通过重写排序比较函数按照降序比较,如下:

  定义排序比较函数:

  bool Comp(const int &a,const int &b)
  {
      return a>b;
  }
  调用时:sort(vec.begin(),vec.end(),Comp),这样就降序排序。

}

顺带<queue>与<stack>容器的简单使用方法

{

  queue<int> a
  a.push(b) 尾部插入b
  a.front 第一个元素
  a.pop 删除第一个元素
  队列:早进早出

  stack<int> a
  a.push(b) 头部插入b
  a.top() 最后一个元素
  a.pop() 删除最后一个元素
  栈:早进晚出

}

大概思路:设两个数组,v1和v0,用来存放数组中以1结尾和以0结尾的数组序号 

每次读取一个字符,如果是0,则查看v1是否有以1结尾的数组,优先放在那个1后面;否则就重新开辟一个新的答案数组。如果是1,则查看v0是否有以0结尾的数组,如果没有,则不满足题意,否则放在以0结尾的那个数组里 .

如果想读入的时候直接从1开始 那么可以scanf(“%s”,s+1); 需要注意:int len = strlen(s+1)而不是strlen(s) 
求长度 string s->s.length()或s.size() .

string a 只能用cin>>s读入 

代码:

#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
vector<int> v0;
vector<int> v1;
vector<int> ans[200050];
int main()
{
  string s;
  cin>>s;
  int len=s.length();
  //cout<<len<<endl;
  int i,j,sum;
  int lg=1;//标记是否满足题意
  sum=0;//记录有多少数组
  for(i=0;i<len&&lg==1;i++)
  {
    s[i]=s[i]-'0';//将字符转换为数字
    if(s[i]==0)
    {
      //cout<<"1"<<endl;
      if(v1.size()==0)//要开辟新数组
      {
        v0.push_back(sum);//压入的是数组序号
        ans[sum++].push_back(i);//记录位置
      }
      else
      {
        int last=v1[v1.size()-1];
        v1.pop_back();
        v0.push_back(last);
        ans[last].push_back(i);
      }
      //cout<<"3"<<endl;
    }
    else if(s[i]==1)
    {
      //cout<<"2"<<endl;
      if(v0.size()==0)//1没有0可以接,不满足题意
      lg=0;
      else
      {
        int last=v0[v0.size()-1];
        v0.pop_back();
        v1.push_back(last);
        ans[last].push_back(i);
      }
    }
  }

  if(lg&&v1.size()==0)
  {
    cout<<sum<<endl;
    for(i=0;i<sum;i++)
    {
      int sz=ans[i].size();
      cout<<sz;
      for(j=0;j<sz;j++)
      cout<<" "<<ans[i][j]+1;
      cout<<endl;
    }
  }
  else
  cout<<"-1"<<endl;
  return 0;
}

codeforce949A(顺带vector详细使用介绍)的更多相关文章

  1. Java 集合系列 05 Vector详细介绍(源码解析)和使用示例

    java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...

  2. 原来你是这样的BERT,i了i了! —— 超详细BERT介绍(一)BERT主模型的结构及其组件

    原来你是这样的BERT,i了i了! -- 超详细BERT介绍(一)BERT主模型的结构及其组件 BERT(Bidirectional Encoder Representations from Tran ...

  3. Window VNC远程控制LINUX:VNC详细配置介绍

    Window VNC远程控制LINUX:VNC详细配置介绍 //---------------------------------------vnc linux下的详细配置 1.VNC的启动/停止/重 ...

  4. ThinkPHP 自动创建数据、自动验证、自动完成详细例子介绍(十九)

    原文:ThinkPHP 自动创建数据.自动验证.自动完成详细例子介绍(十九) 1:自动创建数据 //$name=$_POST['name']; //$password=$_POST['password ...

  5. [原]Redis详细配置介绍

    Redis详细配置介绍 # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 ...

  6. Vector 容器简单介绍

    # Vector STL简要介绍 关于STL中的vector容器,以下做一些相关介绍. #### vector 简要概述 vector 称作向量类,属于容器类,实现了动态的数组,用于元素数量变化的对象 ...

  7. Java 集合系列06之 Vector详细介绍(源码解析)和使用示例

    概要 学完ArrayList和LinkedList之后,我们接着学习Vector.学习方式还是和之前一样,先对Vector有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它.第1部分 Vec ...

  8. java之vector详细介绍

    1 vector介绍 Vector简介 Vector 是矢量队列,它是JDK1.0版本添加的类.继承于AbstractList,实现了List, RandomAccess, Cloneable这些接口 ...

  9. 【转】Java 集合系列06之 Vector详细介绍(源码解析)和使用示例

    概要 学完ArrayList和LinkedList之后,我们接着学习Vector.学习方式还是和之前一样,先对Vector有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它.第1部分 Vec ...

随机推荐

  1. Vue.js表单校验;动画指令;避免内存泄露。

    Vue.js表单校验: 动画指令:创建自定义的滚动指令. 避免内存泄露. 避免内存泄露 在单页面应用开发时SPA,用户无需刷新浏览器.所以javascript应用需要自行清理组件来防止内存占用不断增长 ...

  2. uva11388

    因为a,b整除gcd(a,b),lcm(a,b)又整除a,b,因此如果lcm不整除gcd就是-1:否则的话,lcm=a*b/gcd.而a不能小于gcd,因此a就取gcd,b取lcm. #include ...

  3. Children's Game UVA - 10905

    看90,956这样的串,在比较完之前,就确定大小的,必定选大的放在前.而x=98,y=980;这样的,比较x+y和y+x的大小.如果x+y更小,y就放前. #include <iostream& ...

  4. Kindergarten CodeForces - 484D (贪心,好题)

    大意: 给定序列, 求划分为若干段, 使得总贡献最大, 每段的贡献为max-min 可以发现最优解一定是连续一段递增或递减, 然后dp即可. #include <iostream> #in ...

  5. C/S和B/S 《JavaWeb开发王者归来》学习笔记

    RCP 桌面程序(Desktop Program)也叫胖客户端程序(Rich Client Program),需要安装到计算机上才能运行.例如:word,excel,QQ等. TCP 瘦客户端程序(T ...

  6. Educational Codeforces Round 2 E - Lomsat gelral

    题意:每个节点有个值,求每个节点子树众数和 题解:可线段树合并,维护每个数出现次数和最大出现次数,以及最大出现次数的数的和 //#pragma GCC optimize(2) //#pragma GC ...

  7. meta标签常用设置

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...

  8. CacheManager.NET

    Cache缓存在计算机领域是一个被普遍使用的概念.硬件中CPU有一级缓存,二级缓存, 浏览器中有缓存,软件开发中也有分布式缓存memcache, redis.缓存无处不在的原因是它能够极大地提高硬件和 ...

  9. [转]2017年最具价值的十大开源项目!GitHub 年度报告~

    <GitHub 2017 年度报告>GitHub 每年都会在年度盛会中推出数据报告,其中列出了一些年度的数据,包括其网站中最受欢迎的编程语言.开源项目等.那么今年哪些开源项目最具价值呢?我 ...

  10. GitHub学习三-远程版本库更新与提交

    1.远程版本库更新 一般来说,将本地与远程相关联之后,首先将数据从远程更新下来再上传比较好. 输入 git pull origin master 如果新建版本库的话勾选了初始化包含readme.md, ...