A - String Coloring (easy version)-贪心

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.InputThe 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.OutputIf 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

思路:这就是一道贪心问题,先想当前的最优解:把一个字符串分成俩个依次不减的字符串,把第一个大的写成0,把第二个写成1,如果找不到这样俩个的话就输出NO,我们想想这样为什么是对的,首先,我们想要的结果是经过交换整个字符串是单调递增的,0标记的字符是不着急交换的,1标记的字符是它的前面靠近的既有比他小的字符,又有比他大的0标记的字符,所以这时就着急往前走,所以标记1,而如果一个字符他的前面没有靠近比他小的,也没有比它大的,所有即使它标记为1的话,也走不到前面,所以就输出NO。
ac代码如下:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
 int n;
 string s;
 cin>>n>>s;
 string res;
 char lst0='a',lst1='a';
 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"<<endl;
 for(int i=0;i<n;i++) 
 cout<<res[i];
 return 0;
}
发布了29 篇原创文章 · 获赞 24 · 访问量 3666

猜你喜欢

转载自blog.csdn.net/qq_45772483/article/details/104326414
今日推荐