CF1209C Paint the Digits

You are given a sequence of n digits d1d2dn. 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 tt (1t10000) — the number of test cases in the input.

The first line of each test case contains an integer nn (1≤n≤2⋅10^5) — 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 nn for all test cases in the input does not exceed 210^5

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 nn 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.

题意解释:给定T组数据,每组数据第一行输入数字串长度,第二行输入数字串,用数字1和2对数字串进行涂色,被1涂色的数字子串和被2涂色的数字子串拼接成新的数字串,要求新的数字串是非递减的。

解题思路:对原数字串进行排序,然后从后往前和从前往后各涂一次,若涂不完则输出“-”。

#include <bits/stdc++.h>
using namespace std;
int b[200005];
int main()
{
    int t;
    cin>>t;
    for(int i=1;i<=t;++i)
    {
        memset(b,0,sizeof(b));
        int n;
        cin>>n;
        string a;
        cin>>a;
        string c=a;
        sort(c.begin(),c.end());
        int j=n-1;
        for(int i=n-1;i>=0;--i)
        {
            if(a[i]==c[j])
            {
                b[i]=2;
                j--;
            }
        }
        j++;
        int p=0;
        for(int i=0;i<=n-1;++i)
        {
            if(b[i]==0&&a[i]==c[p])
            {
                b[i]=1;
                p++;
            }
        }
        if(p!=j)
        {
            cout<<"-
";
            continue;
        }
        for(int i=0;i<n;++i)
        {
            cout<<b[i];
        }
        cout<<endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/yoududezongzi/p/11524412.html