CodeForces1209 C.Paint the Digits (枚举)

C. Paint the Digits

You are given a sequence of n digits d1d2…dn. You need to paint all the digits in two colors so that:

each digit is painted either in the color 1 or in the color 2;
if you write in a row from left to right all the digits painted in the color 1, and then after them all the digits painted in the color 2, then the resulting sequence of n digits will be non-decreasing (that is, each next digit will be greater than or equal to the previous digit).
For example, for the sequence d=914 the only valid coloring is 211 (paint in the color 1 two last digits, paint in the color 2 the first digit). But 122 is not a valid coloring (9 concatenated with 14 is not a non-decreasing sequence).

It is allowed that either of the two colors is not used at all. Digits painted in the same color are not required to have consecutive positions.

Find any of the valid ways to paint the given sequence of digits or determine that it is impossible to do.

Input

The first line contains a single integer t (1≤t≤10000) — the number of test cases in the input.

The first line of each test case contains an integer n (1≤n≤2⋅105) — the length of a given sequence of digits.

The next line contains a sequence of n digits d1d2…dn (0≤di≤9). The digits are written in a row without spaces or any other separators. The sequence can start with 0.

It is guaranteed that the sum of the values ​​of n for all test cases in the input does not exceed 2⋅105.

Output

Print t lines — the answers to each of the test cases in the input.

If there is a solution for a test case, the corresponding output line should contain any of the valid colorings written as a string of n digits t1t2…tn (1≤ti≤2), where ti is the color the i-th digit is painted in. If there are several feasible solutions, print any of them.

If there is no solution, then the corresponding output line should contain a single character ‘-’ (the minus sign).

Example
input
5
12
040425524644
1
0
9
123456789
2
98
3
987
output
121212211211
1
222222222
21
-
Note

In the first test case, d=040425524644. The output t=121212211211 is correct because 0022444 (painted in 1) concatenated with 44556 (painted in 2) is 002244444556 which is a sorted sequence of n given digits.

分析:

题目说所有案例的n的和不超过2e5
可以从0到9枚举分界点x
小于x的全部放到左边(涂成1)
大于x的全部放到右边(涂成2)
等于x的判断一下,如果可以放到右边就放到右边否则放到左边
因为x是右边的最小值,所以放到右边不会影响后面的数(x是左边的最大值,会影响)

code:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
const int maxm=2e5+5;
char s[maxm];
int mark[maxm];
int n;
bool check(int x){
    int last=-1;
    int last2=-1;
    for(int i=1;i<=n;i++){
        if(s[i]<x){//小于的放到左边
            mark[i]=1;
            if(s[i]<last)return 0;//冲突剪枝
            last=s[i];
        }else if(s[i]>x){//大于的放到右边
            mark[i]=2;
            if(s[i]<last2)return 0;//冲突剪枝
            last2=s[i];
        }else if(s[i]==x){//等于的判断一下
            if(s[i]>=last2){//能放右边优先右边,因为右边的最小值就是x,一定不会影响后面的
                mark[i]=2;
                last2=s[i];
            }else{//否则只能左边
                mark[i]=1;
                last=s[i];
            }
        }
    }
    return 1;
}
signed main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%s",&n,s+1);
        int ok=0;
        for(int i='0';i<='9';i++){//枚举分界点
            if(check(i)){
                ok=1;
                break;
            }
        }
        if(!ok){//不满足
            puts("-");
        }else{//满足
            for(int i=1;i<=n;i++){
                printf("%d",mark[i]);
            }
            puts("");
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值