E1. String Coloring (easy version)(贪心)

E1. String Coloring (easy version)

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

This is an easy version of the problem. The actual problems are
different, but the easy version is almost a subtask of the hard
version. Note that the constraints and the output format are
different.

You are given a string 𝑠s consisting of 𝑛n lowercase Latin letters.

You have to color all its characters one of the two colors (each
character to exactly one color, the same letters can be colored the
same or different colors, i.e. you can choose exactly one color for
each index in 𝑠s).

After coloring, you can swap any two neighboring characters of the
string that are colored different colors. You can perform such an
operation arbitrary (possibly, zero) number of times.

The goal is to make the string sorted, i.e. all characters should be
in alphabetical order.

Your task is to say if it is possible to color the given string so
that after coloring it can become sorted by some sequence of swaps.
Note that you have to restore only coloring, not the sequence of
swaps.

Input

The first line of the input contains one integer 𝑛n (1≤𝑛≤2001≤n≤200)
— the length of 𝑠s.

The second line of the input contains the string 𝑠s consisting of
exactly 𝑛n lowercase Latin letters.

Output

If it is impossible to color the given string so that after coloring
it can become sorted by some sequence of swaps, print “NO” (without
quotes) in the first line.

Otherwise, print “YES” in the first line and any correct coloring in
the second line (the coloring is the string consisting of 𝑛n
characters, the 𝑖i-th character should be ‘0’ if the 𝑖i-th character
is colored the first color and ‘1’ otherwise).

Examples

input

Copy

9
abacbecfd
output

Copy

YES
001010101
input

Copy

8
aaabbcbb
output

Copy

YES
01011011
input

Copy

7
abcdedc
output

Copy

NO
input

Copy

5
abcde
output

Copy

YES
00000
Solution

思路如下

标解:Note that the actual problem is to divide the string into two subsequences that both of them are non-decreasing. You can note that this is true because you cannot the relative order of the elements colored in the same color, but you can write down subsequences of different colors in any order you want.

  • 思路:根据上面的标解,我们知道 这个题的本质问题是 能够把所给的 的字符串s 分成两个 非递减的s1、s2(如果分不成两个 就 结束输出 - 1)。在这一题里面 s中的字符被分到s1中的字符在原字符串s中的位置要 填 0, 位于s2中的字符在原串s中的位置 要填1.
  • 可行性:为什么这样可以?。。。在这一题中,我们可选择的涂色只有两种 0 或 1 ,不同颜色(数字)之间的位置是可以交换的,交换的最终的目的是要达到 在 s 中的字符安 字母升序排序,,,被分到s1中的子符在原字符串中的位置,都是0,可见它么不需要主动 别的字符(在s中并位于该字符串之前的字符)交换位置,而为位于 s2中的字符是需要与之前的字符进行位置交换的,而又因为 s2 是递增的 所以位于s2中对字符之间需要不需要交换位置,如果我们假设 s2 是非递增的,假设s2为: b c a ,由于s2中的涂色全部为1,,字符a 就无法 与 c、b进行位置交换,这样自然就无解了。。。。。。在用个例子模拟一下吧:设S为:a b c b a , 很明显前三个字符 a b c 均要涂成 0颜色(a、b、c 应该是被分到 s1🀄️),剩下的就剩 b 、a两个字符,对于倒数第二个字符 b ,它要与前面的c字符交换位置,那么它就要涂 1 颜色(应该被分到 s2🀄️)这个是S变为: a 、b、b、c、a,但是对于最后一个字符 a 它既要与位于 s1中的字符c交换位置,有要与位于 s2中的字符b进行位置交换,既不能被分到s1组也不能被分到s2组(因为同组之间 字符的相对位置是无法改变的),那说明 a 既不能被涂成 0 颜色,又不能被涂成 1 颜色,所以这就无解了

题解如下

#include<iostream>
#include<string>
using namespace std;

int main()
{
    //freopen("T.txt","r",stdin);
    int n;
    string s;
    cin >> n >> s;
    char lst0 = 'a',lst1 = 'a';
    string res;
    for(int i = 0; i < n; i ++)
    {
        if(s[i] >= lst0)
        {
            res += '0';
            lst0 = s[i];
        }
        else if(s[i] >= lst1)
        {
            res += '1';
            lst1 = s[i];
        }
        else
        {
            cout << "NO";
            return 0;
        }
    }
    cout << "YES\n";
    for(int i = 0; i < n; i ++)
        cout<<res[i];

    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值