当前位置:网站首页>D. Binary String To Subsequences
D. Binary String To Subsequences
2022-08-09 21:55:00 【秦小咩】
D. Binary String To Subsequences
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given a binary string ss consisting of nn 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 tt independent test cases.
Input
The first line of the input contains one integer tt (1≤t≤2⋅1041≤t≤2⋅104) — the number of test cases. Then tt test cases follow.
The first line of the test case contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of ss. The second line of the test case contains nn characters '0' and '1' — the string ss.
It is guaranteed that the sum of nn does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).
Output
For each test case, print the answer: in the first line print one integer kk (1≤k≤n1≤k≤n) — the minimum number of subsequences you can divide the string ss to. In the second line print nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤k1≤ai≤k), where aiai is the number of subsequence the ii-th character of ss belongs to.
If there are several answers, you can print any.
Example
input
Copy
4 4 0011 6 111111 5 10101 8 01010000
output
Copy
2 1 2 2 1 6 1 2 3 4 5 6 1 1 1 1 1 1 4 1 1 1 1 1 2 3 4
=========================================================================
贪心的策略当然是能加就加进原本就有的,不能加的时候再重新开辟一个
这里用vector进行模拟,末尾存当前串的编号即可
# include<iostream>
# include<string.h>
# include<vector>
using namespace std;
int ans[200000+10];
int main ()
{
//从前往后扫
//1的话就加进去0,把加进去的0结尾变成1结尾
//0的话就加1,把加进去的1结尾变成0结尾
//也就是贪心+模拟,能加就加即可,并不影响后来的
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
vector<int>v[2];
string s;
cin>>s;
s=" "+s;
int now=s[1]-'0';
int cnt=1;
ans[1]=cnt;
v[now].push_back(ans[1]);
for(int i=2; i<=n; i++)
{
if(s[i]=='0')
{
if(v[1].empty())
{
cnt++;
ans[i]=cnt;
v[0].push_back(ans[i]);
}
else
{
int now=v[1].back();
v[1].pop_back();
ans[i]=now;
v[0].push_back(ans[i]);
}
}
else
{
if(v[0].empty())
{
cnt++;
ans[i]=cnt;
v[1].push_back(ans[i]);
}
else
{
int now=v[0].back();
v[0].pop_back();
ans[i]=now;
v[1].push_back(ans[i]);
}
}
}
cout<<cnt<<endl;
for(int i=1; i<=n; i++)
{
cout<<ans[i]<<" ";
}
cout<<endl;
}
return 0;
}
边栏推荐
- 好未来,想成为第二个新东方
- 2022年中国第三方证券APP创新专题分析
- 五星控股汪建国:以“植物精神”深耕赛道,用“动物精神”推动成长
- 4D Summary: 38 Knowledge Points of Distributed Systems
- shell学习
- Basic JSON usage
- MLOps的演进历程
- [Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]
- 深度剖析 Apache EventMesh 云原生分布式事件驱动架构
- nvm下node安装;node环境变量配置
猜你喜欢
leetcode 38. 外观数列
Xiaohei leetcode's refreshing rainy day trip, just finished eating Yufei Beef Noodles, Mala Tang and Beer: 112. Path Sum
POWER SOURCE ETA ETA Power Repair FHG24SX-U Overview
你真的了解乐观锁和悲观锁吗?
Interpretation of the paper (DropEdge) "DropEdge: Towards Deep Graph Convolutional Networks on Node Classification"
18-GuliMall 压力测试与性能监控
The kvm virtual machine cannot be started, NOT available, and the PV is larger than the partition
6 rules to sanitize your code
nvm下node安装;node环境变量配置
Evolution of MLOps
随机推荐
Jinshanyun earthquake, the epicenter is in bytes?
Let's talk about what DDL, DML, DQL and DCL are in SQL statements
Synchronization lock synchronized traces the source
1215 – Cannot add foreign key constraint
【软考 系统架构设计师】案例分析⑤ 质量属性和架构评估
Metasploit常用命令、技术功能模块
Blender程序化建模简明教程【PCG】
2.1.5 大纲显示问题
2022年中国第三方证券APP创新专题分析
反射机制篇
[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]
AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
nvm下node安装;node环境变量配置
用户代码未处理MetadataException
Evolution of MLOps
This article lets you quickly understand implicit type conversion [integral promotion]!
注意力引导网络用于视网膜图像分割
JS–比想象中简单
【微服务~Nacos】Nacos服务提供者和服务消费者
面试官:MySQL 中 update 更新,数据与原数据相同时会执行吗?大部分人答不上来!