Given a string composed of A and B, you can choose to flip or not flip each of its prefixes, flip as 1, not flip as 0, returns the operation sequence with the smallest lexicographically ordered string.
N <= 1e3
Idea: consider a very long segment A [T, w]
Flip the T-1 and W to move this section A to the front
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<cmath> 5 #include<iostream> 6 #include<algorithm> 7 #include<map> 8 #include<queue> 9 #include<vector>10 #include<ctime>11 using namespace std;12 typedef long long ll;13 typedef unsigned int uint;14 typedef unsigned long long ull;15 typedef pair<int,int> PII;16 typedef vector<int> VI;17 #define fi first18 #define se second 19 #define MP make_pair20 #define N 21000021 #define M 13022 #define MOD 100000000723 #define eps 1e-8 24 #define pi acos(-1)25 26 char a[N];27 int b[N],c[N];28 29 int main()30 {31 scanf("%s",a+1);32 int n=strlen(a+1);33 for(int i=1;i<=n;i++)34 if(a[i]==‘a‘) c[i]=0;35 else c[i]=1;36 37 int i=0;38 memset(b,0,sizeof(b));39 while(i<=n)40 {41 i++;42 if(c[i]==1||i>n) continue;43 int t=i;44 int w=i;45 while(i+1<=n&&c[i+1]==0){i++; w++;}46 b[t-1]^=1; b[w]^=1;47 }48 for(int i=1;i<=n-1;i++) printf("%d ",b[i]);49 printf("%d",b[n]);50 return 0;
[Cf1_3c] smallest word (constructor)