思路:
直接贪心,先从后往前找到一个位置,满足它后面的字符包含了还没有选的字符,然后这个就是合法位置。
每次找出字符最小的合法位置
c o d e code code
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
bool f[27], a[27];
int tot, m;
string s;
int main()
{
freopen("aorder.in", "r", stdin);
freopen("aorder.out", "w", stdout);
cin>>s;
for(int i=0; i<s.size(); i++)
{
if(f[s[i]-'a']==0)
m++;
f[s[i]-'a']=1;
}
tot=m;
int l=-1;
while(m--)
{
int tot1=0, flag=0, p=1e9;
char c='{';
for(int i=s.size()-1; i>l; i--)
{
if(a[s[i]-'a']==0)
a[s[i]-'a']=1;
if(flag==0)
{
int bz=1;
for(int j=0; j<=26; j++)
if(a[j]!=f[j]&&f[j]==1)
{
bz=0;
break;
}
if(bz)
flag=1;
}
if(flag==1&&f[s[i]-'a']==1)
{
if(s[i]<c)
c=s[i], p=i;
else if(s[i]==c&&i<p)
p=i;
}
}
l=p;
f[c-'a']=0;
printf("%c", c);
memset(a, 0, sizeof(a));
}
return 0;
}