解题报告 CodeForces - 1296E1&E2

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 s).

After coloring, you can swap any two neighboring characters of the string that are colored differentcolors. 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 n (1≤n≤200) — the length of s.

The second line of the input contains the string s consisting of exactly n 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

题意:

给定一个字符串,用两种颜色(0,1)给每个字母染色,相邻的不同色的字母可以交换位置,问是否存在一种染色方式使得通过交换字母位置使该字符串变成有序字符串,存在则输出“YES”和染色方式,不存在则输出“NO”

思路:

1.划重点“相邻的不同色的”也就是说同种颜色之间不能交换

2.想要变成有序序列又不需要交换,该序列只能本身就是:有序的!所以同种颜色的字母排列是非递减的

3.由于只有两种颜色,所以只需要设两个变量f0,f1来维护当前颜色刚涂到的最大值

4.如果当前字母比f0和f1都小,那么不能保证“同种颜色的字母排列是非递减的”所以涂色失败,否则,涂色成功并更新最大值

代码:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define maxn 100005
#define mod 998244353
int n;
string s;
int a[maxn],f0=0,f1=0,f=0;

int main()
{
    cin>>n;
    cin>>s;
    for(int i=0;i<n;i++)
    {
        int tmp=s[i]-'a';
        if(tmp>=f0)
        {
            a[i]=0;
            f0=tmp;
        }
        else
        {
            if(tmp>=f1)
            {
                a[i]=1;
                f1=tmp;
            }
            else
            {
                f=1;
                cout<<"NO";
                break;
            }
        }
    }
    if(!f)
    {
        cout<<"YES"<<endl;
        for(int i=0;i<n;i++)
            cout<<a[i];
    }
    return 0;
}

 

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 differentcolors. 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 nn (1\leq n\leq2*10 ^{5}) — 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\leq res\leq 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 

题意:

给定一个字符串,相邻的不同色的字母可以交换位置,问用到颜色最少且通过交换位置可以将原字符串变成有序字符串的染色方式

思路:

看到染色先想了一下搜索然而无头绪...看了题解发现是一个非常巧妙的贪心

1.如果有比当前字母c1大却出现在当前字母前边的字母c2,那么c1和c2必须交换位置,即c1和c2不同色

2.为了方便计算颜色的种数,颜色编号从1开始累加,那么c1就要涂成比c2大1的颜色

3.c1可能要与前边的若干个字母进行交换,所以c1应涂成所有需要与它交换的颜色中最大的一个+1

4.由于最多只有26个字母所以,我们沿用EASY题的思路,设一个数组c来表示每个字母最新涂的颜色,将c初始化为0

代码:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define maxn 200005
#define mod 998244353
int n,d[maxn],c[35],sum=0;
string s;

int main()
{
    memset(c,0,sizeof c);
    cin>>n>>s;
    for(int i=0;i<n;i++)
    {
        d[i]=1;
        for(int j=25;j>s[i]-'a';j--)
        {
            d[i]=max(d[i],c[j]+1);
        }
        c[s[i]-'a']=max(c[s[i]-'a'],d[i]);
        sum=max(sum,d[i]);
    }
    cout<<sum<<endl;
    for(int i=0;i<n;i++)
        cout<<d[i]<<" ";
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值