C - Smallest Word CodeForces - 1043C(思维+构造)

C. Smallest Word
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 1 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 a is lexicographically smaller than a string b if and only if one of the following holds:

a is a prefix of b, but a≠b;
in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
Input
The first and the only line contains a string s (1≤|s|≤1000), describing the initial string formed by magnets. The string s consists only of characters ‘a’ and ‘b’.

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

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

Examples
inputCopy
bbab
outputCopy
0 1 1 0
inputCopy
aaaaa
outputCopy
1 0 0 0 1
Note
In the first example, IA can reverse the second and the third prefix and get a string “abbb”. She cannot get better result, since it is also lexicographically smallest string obtainable by permuting characters of the initial string.

In the second example, she can reverse any subset of prefixes — all letters are ‘a’.

题意:给你一个只包含字符 ‘a’ 与 ‘b’ 的字符串,你可以从头到尾每次访问一个字符,而且可以选择反转从第一个字符到访问字符的子字符串。文字不好表达,用例子说:假设一个字符串
字符串:abbab
下标 : 12345
假设访问到下标3,翻转,结果为:
字符串:bbaab
下标 :12345

题目问你怎么翻转得到的字符串,字典序最小;

思路:题目说只有字符a和b,所以我们可以考虑从前往后考虑,如果遇到a字符,将它翻转,
很明显,字典序会变小,如果遇到b字符将其翻转,很明显字典序会变大,但是呢,还可以发现
如果b字符后面还有a字符的话,再翻转一次a可以让b字符处于当前最后一个位置,使得字典序变小,所以一共就两种情况
如果是a字符,直接翻转
如果是b字符,如果b字符后面有a字符,直接翻转
代码:

#include<bits/stdc++.h>
#define LL long long
#define Max 100005
#define Mod 1e9+7
const LL mod=1e9+7;
const LL inf=0x3f3f3f3f;
using namespace std;
char a[Max];
int ans[Max];
int main()
{
    scanf("%s",a);
    int len=strlen(a),ca=0;
    for(int i=0; i<len; i++)
    {
        if(a[i]=='a')//统计a的数量
            ca++;
    }
    for(int i=0; i<len; i++)
    {
        if(a[i]!=a[i+1])//如果连续相等,只要翻转最后一个就可以了
            if(a[i]=='a')//是a直接翻转
            {
                ans[i]=1;
            }
            else if(a[i]=='b' && ca>0)//是b,且后面还有a字符,翻转
            {
                ans[i]=1;
            }
        if(a[i]=='a')
            ca--;
    }
    for(int i=0; i<len; i++)
    {
        printf("%d ",ans[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Gee_Zer/article/details/89211766
今日推荐