题意描述

You are given a binary string s consisting of n zeros and ones.

Your task is to divide the given string into the minimum number of subsequences in such a way that each character of the string belongs to exactly one subsequence and each subsequence looks like “010101 …” or “101010 …” (i.e. the subsequence should not contain two adjacent zeros or ones).

Recall that a subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, subsequences of “1011101” are “0”, “1”, “11111”, “0111”, “101”, “1001”, but not “000”, “101010” and “11100”.

You have to answer t independent test cases.

要求以010101或者101010的形式分组,求每个0或1属于哪个分组

思路

我们使用两个队列来分别表示0和1的空间,如果遇到0时,判断1的空间内是否存在元素,如果存在的话,则说明0和该元素一组,否则需要重新开辟一个空间;遇到1的话同理。

AC代码

#include<bits/stdc++.h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=2*1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
int ans[N];
void solve(){mst(ans,0);int n;cin>>n;string s;cin>>s;queue<int> q0,q1;int cnt=1;rep(i,0,n){if(s[i]=='0'){if(q1.empty()) ans[i]=cnt++;else{ans[i]=ans[q1.front()];q1.pop();}q0.push(i);}else{if(q0.empty()) ans[i]=cnt++;else{ans[i]=ans[q0.front()];q0.pop();}q1.push(i);}}cout<<cnt-1<<endl;rep(i,0,n) cout<<ans[i]<<' ';cout<<endl;
}
int main(){IOS;int t;cin>>t;while(t--){solve();}return 0;
}

codeforces 1399D相关推荐

  1. CodeForces 375D Tree and Queries

    传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...

  2. 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)

    题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...

  3. 【codeforces 812C】Sagheer and Nubian Market

    [题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...

  4. CodeForces 获得数据

    针对程序的输出可以看见 CodeForces :当输入.输出超过一定字符,会隐藏内容 所以:分若干个程序进行输入数据的获取 1. 1 for (i=1;i<=q;i++) 2 { 3 scanf ...

  5. codeforces水题100道 第二十七题 Codeforces Round #172 (Div. 2) A. Word Capitalization (strings)...

    题目链接:http://www.codeforces.com/problemset/problem/281/A 题意:将一个英文字母的首字母变成大写,然后输出. C++代码: #include < ...

  6. CodeForces 595A

    题目链接: http://codeforces.com/problemset/problem/595/A 题意: 一栋楼,有n层,每层有m户,每户有2个窗户,问这栋楼还有多少户没有睡觉(只要一个窗户灯 ...

  7. codeforces A. Jeff and Digits 解题报告

    题目链接:http://codeforces.com/problemset/problem/352/A 题目意思:给定一个只有0或5组成的序列,你要重新编排这个序列(当然你可以不取尽这些数字),使得这 ...

  8. Codeforces Round #506 (Div. 3)

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

  9. Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)

    Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈) 标签: codeforces 2017-06-02 11:41 29人阅读 ...

最新文章

  1. mysql timestamp比较查询
  2. jmu-python-凯撒密码加密算法_7-30 jmu-python-凯撒密码加密算法 (10 分)
  3. HDU 4321 Contest 3
  4. Android的Menu状态动态设置方法onPrepareOptionsMenu(Menu menu) (转载)
  5. 《研磨设计模式》chap22 装饰模式Decorator(4)AOP+总结
  6. vue ui 启动,浏览器报错Unexpected token
  7. 电脑怎么改网络ip地址_电脑的桌面路径怎么改?
  8. java 常用 函数_java在线报表中有哪些常用函数
  9. 问题三十五: 怎么用ray tracing画二次曲面(quadratic surfaces)(3)——椭球抛物面
  10. SDUT 2482 二叉排序树
  11. 2020年 2月 省市区sql(一)
  12. Java课程设计之你画我猜
  13. win7管理员取得所有权
  14. docker 启动失败:specified both as a flag and in the configuration fir/daemon.json
  15. coxphfit+matlab,计算Cox比例风险模型的coxph和cph函数有什么区别?
  16. Andriod studio 学习 之打包
  17. 使用Tensorflow 2进行猫狗分类识别
  18. 网络安全究竟是什么?如何成为一位优秀的网络安全工程师?
  19. CV算法工程师自修指南
  20. 猿大师播放器有哪些不同的版本,分别适合哪些不同需求的场景?

热门文章

  1. JS实现GPS坐标转化为高德坐标
  2. 转载别人的ftp,觉得目录结构不错,学习
  3. 服务器128g内存显示64g可用,内存从64G换成128G,会造成什么影响?
  4. 百度地图三维盒子模型标注
  5. 电网计算机专业分数线,这几类大学受“国家电网”青睐,录取分数线不高,毕业就有铁饭碗...
  6. php离开页面吗,关闭(不离开)页面时jquery beforeunload?
  7. 什么是ajax?,ajax能干什么?
  8. mysql用户函数_MySQL 函数、用户自定义函数
  9. 利用fixed把div浮动在底部
  10. 本地服务器搭建(2.4)---Centos基本软件安装介绍