解題報告 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;
}

 

發佈了10 篇原創文章 · 獲贊 1 · 訪問量 1214
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章