Codeforces Round #661 (Div. 3) D. Binary String To Subsequences

Codeforces Round #661 (Div. 3) D. Binary String To Subsequences

题目链接

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.

Input

The first line of the input contains one integer t (1≤t≤2e4) — the number of test cases. Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤2e5) — the length of s. The second line of the test case contains n characters ‘0’ and ‘1’ — the string s.

It is guaranteed that the sum of n does not exceed 2e5 (∑n≤2e5).

Output

For each test case, print the answer: in the first line print one integer k (1≤k≤n) — the minimum number of subsequences you can divide the string s to. In the second line print n integers a1,a2,…,an (1≤ai≤k), where ai is the number of subsequence the i-th character of s belongs to.

If there are several answers, you can print any.

Example

input

4
4
0011
6
111111
5
10101
8
01010000

output

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 

思维题~
首先对所有子序列标号,用 v [ 0 ] v[0] v[0] 存后面需要添加字符 0 0 0 的子序列编号,用 v [ 1 ] v[1] v[1] 存后面需要添加字符 1 1 1 的子串编号,遍历一遍即可得到答案,比如此时遍历的是字符 1 1 1,我们就看 v [ 1 ] v[1] v[1] 的大小,如果 v [ 1 ] v[1] v[1] 为空,就在 v [ 0 ] v[0] v[0] 中添加一个子序列编号;如果非空,就从 v [ 1 ] v[1] v[1] 末尾取一个编号添加到 v [ 0 ] v[0] v[0] 即可,对字符 0 0 0 同理,AC代码如下:

#include
using namespace std;
typedef long long ll;
int main(){
     
    int t,n;
    cin>>t;
    while(t--){
     
        string s;
        cin>>n>>s;
        int ans[n+1]={
     0},id=1,sum=0;
        vector<int>v[2];
        for(int i=0;i<n;i++){
     
            if(s[i]=='0'){
     
                if(v[0].size()==0){
     
                    ans[i]=id;
                    v[1].push_back(id);
                    id++;
                }else{
     
                    ans[i]=v[0].back();
                    v[0].pop_back();
                    v[1].push_back(ans[i]);
                }
            }else{
     
                if(v[1].size()==0){
     
                    ans[i]=id;
                    v[0].push_back(id);
                    id++;
                }else{
     
                    ans[i]=v[1].back();
                    v[1].pop_back();
                    v[0].push_back(ans[i]);
                }
            }
            sum=max(sum,ans[i]);
        }
        cout<<sum<<endl;
        for(int i=0;i<n;i++) cout<<ans[i]<<" ";
        puts("");
    }
	return 0;
}

你可能感兴趣的:(vector,思维,Codeforces)