String Coloring (hard version)

题目连接: String Coloring (hard version)

题目:

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 s consisting of n 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 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 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.

Input
The first line of the input contains one integer n (1≤n≤2⋅105) — the length of s.
The second line of the input contains the string s consisting of exactly n lowercase Latin letters.

Output
In the first line print one integer res (1≤res≤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 c of length n, where 1≤ci≤res and ci means the color of the i-th character.

Examples
Input

9
abacbecfd
Output
2
1 1 2 1 2 1 2 1 2

Input
8
aaabbcbb
Output
2
1 2 1 2 1 2 1 1 //这个答案好像有问题

Input
7
abcdedc
Output
3
1 1 1 1 1 2 3

Input
5
abcde
Output
1
1 1 1 1 1

解题思路:

思路一:

这个题相较简单版本而言, 我们可以明确知道一点, 该题一定存在解, 我们需要做的只是让涂色的代数和尽可能的小.
那么对于这个串而言,
如果在i位置存在某个字符不需要与前面的字符进行交换, 那么我们只需要把它涂成1(保证尽可能小).
如果在j位置存在某个字符需要与前面的字符进行交换, 那么我们需要比较所有之前出现过的字符ASC值比该字符大的涂色最大值+1(保证尽可能小), 这样我们才能确保他能和每一个比他大的字符进行交换. 否则如果该位置涂色如果小于等于最大值, 那么一定存在某个位置的字符需要进行交换, 而他们的涂色值相同.
其实我们观察可以发现一点, 如果某个字符在i位置出现时的涂色值为x, 当他在j位置又出现的时候, 此时的涂色值一定大于或者等于x, 不会小于x.

思路二:

这个思路也是从学长那里学习来的, 算是简单版本的一个扩展, 也是子串的思维, 一共26个字母, 子串的数量最多也只有26个.
我们如果把简单版本的思维借鉴过来, 即: 把字符串拆分成数个非降序的子串, 那么不同子串之间一定是可以交换的, 且相同子串内无需交换. 每个字符的涂色值等于他所处的子串值.

AC代码:

思路一AC代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int res[1000000]; //存储字符串结果
int let[30]; //每个字母的当前下标值
int main(void)
{
	string s; int n; cin >> n >> s;
	for (int i = 0; i < n; i++) {
		int op = s[i] - 'a'; //让a对应0, z对应25
		for (int j = op + 1; j <= 26; j++) { //这里<=26是为了赋值字符为z, 如果小于26的话, 那字符z的位置将不会被赋值
			res[i] = max(res[i], let[j] + 1); //该位置的值应该大于所有比该字符大的字母的值 (记为*)
		}
		let[op] = max(res[i], let[op]); //更新这个字母的最大值
	}
	
	cout << *max_element(let, let + 26) << endl; //输出最大涂色值

	for (int i = 0; i < n; i++) {
		printf("%d", res[i]);
		i == n - 1 ? printf("\n") : printf(" ");
	}
	return 0;
}

关于*处: 由于我们是顺序找查, 如果这个字母没出现过, 那let[j]也就是0, +1后也是涂色最小值1, 是不影响的. 如果这个字母在i位置之前出现过, 那么let[j]一定非0, 由于我们每次都给i位置字符的涂色值取最小, 因此如果当前i位置的涂色值涂色小于等于比i位置字符值大的字母的话, 则他一定会无法与某个大于i字符值位置的字符进行交换.

思路二AC代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int res[1000000]; //存放答案
int a[30]; //每个子串的最大字符.
int main(void)
{
	string s; int n; cin >> n >> s;
	int cou = 1; //子串数量, 默认为1
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < cou; j++) {
			if (s[i] >= a[j]) { a[j] = s[i], res[i] = j + 1; goto here; } //可以放到子串j上, 涂色值为j+1
		}
		a[cou++] = s[i], res[i] = cou; //不能放在所有存在的子串上, 则需要新开辟一个子串
	here:;
	}

	cout << cou << endl; //输出子串的数量, 就是最大涂色值

	for (int i = 0; i < n; i++) {
		printf("%d", res[i]);
		i == n - 1 ? printf("\n") : printf(" ");
	}
	return 0;
}

时隔一个月 我好像填了坑.

END

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逍遥Fau

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值