题目链接:Zebras

题意

给定一个 010101 字符串,要求找出其中满足下列条件的子序列:

  1. 子序列中 010101 相间;
  2. 子序列以 000 开头,以 0" role="presentation" style="position: relative;">000 结尾;
  3. 任意两个子序列之间没有交集;
  4. 所有子序列的并为整个序列。

输出找出的子序列方案,子序列的个数不必最少,只要合法即可。

输入

输入只有一行,为一个只包含 000 和 1" role="presentation" style="position: relative;">111 的字符串 s (1≤|s|≤2×105)s(1≤|s|≤2×105)s~(1\leq|s|\leq2\times10^5)。

输出

第一行输出一个整数 k (1≤k≤|s|)k(1≤k≤|s|)k~(1\leq k\leq|s|),表示子序列的个数,接下去 kkk 行,每行表示一个子序列,每行第一个整数为 li (1≤li≤|s|)" role="presentation" style="position: relative;">li (1≤li≤|s|)li (1≤li≤|s|)l_i~(1\leq l_i\leq|s|),表示第 iii 个子序列中的字符个数,接下去 li" role="presentation" style="position: relative;">lilil_i 个整数每个整数表示序列中的第 jjj 个元素在原字符串中的下标,下标从 1" role="presentation" style="position: relative;">111 开始。如果无法构造出满足条件的序列,输出 −1−1-1。

样例

输入
0010100
输出
3
3 1 3 4
3 2 5 6
1 7
输入
111
输出
-1

题解

将所有 000 字符所在的下标放到一个 set" role="presentation" style="position: relative;">setsetset 中,所有 111 字符所在的下标放到一个 set" role="presentation" style="position: relative;">setsetset 中,每次交替从这两个 setsetset 中选出大于当前 000 或者 1" role="presentation" style="position: relative;">111 的下标的最小值,将这个下标记录到答案中,并从查找的集合中删去,如果某一次查找中,查找到的最后一个数字不是 000,则说明 0" role="presentation" style="position: relative;">000 的数量不够,需要输出 −1−1-1,如果所有的 010101 间隔序列都查找完毕,而存 111 的下标的集合仍为非空,则输出 −1" role="presentation" style="position: relative;">−1−1-1,其他情况均可以构造出满足条件的 010101 间隔序列,最后注意把所有多出来的 00<script type="math/tex" id="MathJax-Element-58">0</script> 都单独放到一个序列作为答案。

过题代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <algorithm>
#include <sstream>
using namespace std;#define LL long long
const int maxn = 200000 + 100;
int len, cnt, Index, num;
bool flag;
vector<int> G[maxn];
set<int> st[2];
set<int>::iterator it;
char str[maxn];int main() {#ifdef LOCALfreopen("test.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);#endif // LOCALios::sync_with_stdio(false);while(scanf("%s", str + 1) != EOF) {cnt = 0;flag = true;len = strlen(str + 1);st[0].clear();st[1].clear();for(int i = 1; i <= len; ++i) {st[str[i] - '0'].insert(i);}while(!st[0].empty() && !st[1].empty()) {Index = 0;G[++cnt].clear();it = st[0].begin();while(it != st[0].end() && it != st[1].end()) {num = *it;G[cnt].push_back(num);st[Index].erase(it);Index = 1 - Index;it = st[Index].upper_bound(num);}if(str[G[cnt].back()] == '1') {flag = false;break;}}if(!st[1].empty() || !flag) {printf("-1\n");continue;}while(!st[0].empty()) {G[++cnt].clear();G[cnt].push_back(*st[0].begin());st[0].erase(st[0].begin());}printf("%d\n", cnt);for(int i = 1; i <= cnt; ++i) {len = G[i].size();printf("%d", len);for(int j = 0; j < len; ++j) {printf(" %d", G[i][j]);}printf("\n");}}return 0;
}

Codeforces 949A Zebras(构造)相关推荐

  1. CodeForces 459C(构造题)

    http://codeforces.com/problemset/problem/459/C /** 题意:有n个同学,k辆车,d天(每天n个同学去一个地方)问经过d天后,任意的多个同学不能总在一起d ...

  2. Codeforces - 474D - Flowers - 构造 - 简单dp

    https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样. 我们要找一个01串,其中0的段要被划分为若干个连续k的0. 我们设想一个长度 ...

  3. Codeforces 989C (构造)

    传送门 题面: C. A Mist of Florescence time limit per test 1 second memory limit per test 256 megabytes in ...

  4. CodeForces - 1328D Carousel(构造+贪心)

    题目链接:点击查看 题目大意:给出一个旋转木马外观的排列,要求给每个木马刷上一种颜色,使得相邻的外观不同的木马颜色不同,问最少需要几种颜色,并提供构造方案 题目分析:没做出来的同学大概是没读懂题吧,一 ...

  5. Special Permutation CodeForces - 1352G(构造)

    思路:一开始想复杂了,直接搞的图论,TLE了.后来发现其实可以直接构造.前四个我们可以构造出2 3 1 4 的形式,如果n>=4的话,那么可以左右来回放置,这样就可以构造成功.只有n<=3 ...

  6. CodeForces.1174D.EhabandtheExpectedXORProblem(构造前缀异或和数组)

    题目链接 这道题比赛的时候没做出来,赛后补题的时候发现其实可以构造一个前缀异或和数组,然后根据初始化的第一个值进行填数,但是作为菜鸡的我虽然坚信自己的想法是正确的却想了很久也没有能够构造出来所谓的前缀 ...

  7. Codeforces 1138B Circus (构造方程+暴力)

    题意: 给你两个01串,要你选n/2个位置,使得选的位置在s1中"1"的数量等于未选的s2中"1"的数量 n<=5000,1s 思路: 设两个串中出现&q ...

  8. 【cf】Codeforces 题解等汇总

    [cf]Codeforces Round #774 (Div. 2) 前4题 [cf]Codeforces Round #774 (Div. 2) 前4题_legend_yst的博客-CSDN博客 [ ...

  9. Codeforces 题目合集+分类+代码 【Updating...】【361 in total】

    961A - Tetris                                                模拟                                      ...

  10. cf1200构造15道

    最近做构造,想对比下先做后看答案归纳,留下思路之后直接看答案归纳,然后再统一检测,还有直接看答案,归纳,检测三种方法哪种效率高些,于是先做个十五题试试第一个方法,花3天写了15道构造,等到归纳的时候已 ...

最新文章

  1. DeepMind科学家:AI对战《星际争霸》胜算几何?
  2. python开发工具
  3. php的bom头会影响格式,phpBOM头(字符#65279;)出现的原因以及解决方法_PHP程序员博客|高蒙个人博客...
  4. wxWidgets:wxMemoryDC类用法
  5. python导入模块--案例
  6. Recurrent Neural Network系列1--RNN(循环神经网络)概述
  7. ElasticSearch之高亮显示
  8. 基于JAVA+SpringMVC+Mybatis+MYSQL的同学录管理系统
  9. sphinx结合scws的mysql全文检索
  10. 40个良好用户界面设计Tips
  11. Android View框架总结(六)View布局流程之Draw过程
  12. github 设置语言为中文
  13. java中的保护(protected)修饰符的理解
  14. AI 教你画油画:任意画风都可驾驭
  15. vue项目-后台管理系统
  16. 如何在web项目中访问HTML页面
  17. 电脑说话,我家的电脑成精了!它开口说话了
  18. 时空大数据可视化专栏
  19. web实现QQ第三方登录
  20. Ubuntu 各版本 iso 下载

热门文章

  1. 解决双卡4G模式下不能接听和拨打电话问题
  2. “Bluetooth keeps stopping“
  3. TCPIP卷一(11):EIGRP的汇总、stub、leak-map参数
  4. 使用aspose.words将Word转为PDF
  5. 【期末大作业】简单的学生网页作业源码 基于html css javascript南京大学网页校园教育网站html模板(3页)
  6. 找到了airdrop无法发现对方的原因了,原因你想不到!
  7. 最新《Linux系统优化+Linux综合架构课程》
  8. 81个人脸关键点检测
  9. 127.0.0.1和localhost和本机IP三者的区别!!!
  10. SpringSecurity 密码加密