CF1296E2.String Coloring (hard version)

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

This is a hard 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 ss consisting of nn lowercase Latin letters.

You have to color all its characters the minimum number of 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 ss ).

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 find the minimum number of colors which you have to color the given string in 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.

输入格式

The first line of the input contains one integer nn ( 1n21051 \le n \le 2 \cdot 10^5 ) — the length of ss .

The second line of the input contains the string ss consisting of exactly nn lowercase Latin letters.

输出格式

In the first line print one integer resres ( 1resn1 \le res \le n ) — the minimum number of colors in which you have to color the given string so that after coloring it can become sorted by some sequence of swaps.

In the second line print any possible coloring that can be used to sort the string using some sequence of swaps described in the problem statement. The coloring is the array cc of length nn , where 1cires1 \le c_i \le res and cic_i means the color of the ii -th character.

输入输出样例

  • 输入#1

    9
    abacbecfd

    输出#1

    2
    1 1 2 1 2 1 2 1 2
  • 输入#2

    8
    aaabbcbb

    输出#2

    2
    1 2 1 2 1 2 1 1
  • 输入#3

    7
    abcdedc

    输出#3

    3
    1 1 1 1 1 2 3
  • 输入#4

    5
    abcde

    输出#4

    1
    1 1 1 1 1
首页