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

                       

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值