String Coloring (easy version) CodeForces - 1296E1(贪心)

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 ss consisting of nn 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 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 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 nn (1≤n≤2001≤n≤200) — the length of ss.

The second line of the input contains the string ss consisting of exactly nn 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 nn characters, the ii-th character should be ‘0’ if the ii-th character is colored the first color and ‘1’ otherwise).

Examples
Input
9
abacbecfd
Output
YES
001010101
Input
8
aaabbcbb
Output
YES
01011011
Input
7
abcdedc
Output
NO
Input
5
abcde
Output
YES
00000
思路:具有相同颜色的是不可以交换的,那么这样的话,具有相同颜色的必须是按照非递减序列的,所以这个简单版本我们就构造两个非递减序列才可以。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

string s;
int n;

int main()
{
	scanf("%d",&n);
	cin>>s;
	string ans,a,b;
	int lena=0,lenb=0;
	a+='a',b+='a';
	for(int i=0;i<n;i++)
	{
		if(s[i]>=a[lena])
		{
			ans+='1';
			lena++;
			a+=s[i];
		}
		else if(s[i]>=b[lenb])
		{
			ans+='0';
			lenb++;
			b+=s[i];
		}
		else//如果两个不满足的话,就构造不出来这样的序列。
		{
			cout<<"NO"<<endl;
			return 0;
		}
	}
	cout<<"YES"<<endl<<ans<<endl;
	return 0;
}

努力加油a啊,(o)/~

发布了426 篇原创文章 · 获赞 24 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/104280150
今日推荐