A - String Coloring -贪心

1.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;
}

2.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.InputThe first line of the input contains one integer nn

(1≤n≤2⋅1051≤n≤2⋅105

) — the length of ss

.The second line of the input contains the string ss

consisting of exactly nn

lowercase Latin letters.OutputIn the first line print one integer resres

(1≤res≤n1≤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 cc

of length nn

, where 1≤ci≤res1≤ci≤res

and cici

means the color of the ii

-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

思路:贪心问题全靠直觉。。

#include <bits/stdc++.h>
using namespace std;
int now[30];
char s[200005];
int num[200005];
int main()
{
    int n;
    scanf("%d",&n);
    scanf("%s",s+1);
    int ans=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=30;j++)
        {
            if(s[i]>=now[j])
            {
                num[i]=j;
                now[j]=s[i];
                ans=max(ans,j);
                break;
            }
        }
    }
    printf("%d\n",ans);
    for(int i=1;i<=n;i++)
    {
        printf("%d ",num[i]);
    }
    printf("\n");
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值