1. 程式人生 > >Codeforces Round #519 by Botan Investments C. Smallest Word

Codeforces Round #519 by Botan Investments C. Smallest Word

C. Smallest Word

IA has so many colorful magnets on her fridge! Exactly one letter is written on each magnet, 'a' or 'b'. She loves to play with them, placing all magnets in a row. However, the girl is quickly bored and usually thinks how to make her entertainment more interesting.

Today, when IA looked at the fridge, she noticed that the word formed by magnets is really messy. "It would look much better when I'll swap some of them!" — thought the girl — "but how to do it?". After a while, she got an idea. IA will look at all prefixes with lengths from 11 to the length of the word and for each prefix she will either reverse this prefix or leave it as it is. She will consider the prefixes in the fixed order: from the shortest to the largest. She wants to get the lexicographically smallest possible word after she considers all prefixes. Can you help her, telling which prefixes should be chosen for reversing?

A string aa is lexicographically smaller than a string bb if and only if one of the following holds:

  • aa is a prefix of bb, but a≠ba≠b;
  • in the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb.

Input

The first and the only line contains a string ss (1≤|s|≤10001≤|s|≤1000), describing the initial string formed by magnets. The string ss consists only of characters 'a' and 'b'.

Output

Output exactly |s||s| integers. If IA should reverse the ii-th prefix (that is, the substring from 11 to ii), the ii-th integer should be equal to 11, and it should be equal to 00 otherwise.

If there are multiple possible sequences leading to the optimal answer, print any of them.

Examples

input

Copy

bbab

output

Copy

0 1 1 0

input

Copy

aaaaa

output

Copy

1 0 0 0 1

題意:給你一個ab字串,它的字首串以第i個字元結尾,你可以反轉任意一個字首串,最終使得ab串字典序最小,需要反轉的字首串,在其結尾處標記1,其他標記0,輸出標記結果。

思路:造個數據推推規律就行了,很容易會發現任何一個串都有辦法變成左邊全a,右邊全b。

舉例:bbbaabba  首先將在第5個位置反轉->aabbbbba        接著我們反轉第6個位置->bbbbbaaa,最後反轉最後位置->aaabbbbb。

規律很明顯了吧,其實我們的目的就是把所有的a放到左邊去。所有從左向右掃描,碰到字母a,判斷最左端是否積累了a:

1.有a,此時在字母a的左邊反轉,將前面的a連線過來。

2.沒有a,此時在字母a處反轉(當然一定是連續a的最靠右的位置)   

就這樣- -,手推一下就能感覺到規律,事實上我們整個過程把散的a連線起來了。

#include <bits/stdc++.h>
#define ll long long
using namespace std;
char s[1010];
int main()
{
    while(~scanf("%s",s))
    {
        int len=strlen(s);
        int k=0;
        if(s[0]=='a')k=1;   //判定最左端是否存在a
        for(int i=0;i<len;i++)
        {
            if(i==len-1)   //最後一個位置直接處理
            {
                if(k==0)printf("1\n");   //bbbaaa這種再轉一次
                else printf("0\n");
                break;
            }
            if(k==1)   //有a
            {
                if(s[i]=='b'&&s[i+1]=='a')
                {
                    k=0;
                    printf("1 ");
                }
                else printf("0 ");
            }
            else   //沒有a
            {
                if(s[i]=='a'&&s[i+1]=='b')
                {
                    k=1;
                    printf("1 ");
                }
                else printf("0 ");
            }
        }
    }
    return 0;
}