Meaning of the questions: initial string to you, want you to give each character coloring, the provisions of adjacent characters of different colors can be exchanged, there is a deposit does not ask you for coloring strings can become an orderly way?
Ideas: we follow the greedy idea, if front of me bigger than my character in front of me, I am not sure you want to swap, so you can traverse bigger than my character, to see their greatest numbers in front of me is how much, then plus 1.
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+1;
char s[maxn];
int n,mx=0,ans[maxn],Max[maxn];
int main()
{
scanf("%d",&n);
scanf("%s",s);
for(int i=0;i<n;++i)
{
int cnt=0;
for(int j=s[i]+1;j<='z';++j)
cnt=max(cnt,Max[j]);
cnt++;
Max[s[i]]=cnt;
ans[i]=cnt;
mx=max(mx,cnt);
}
printf("%d\n",mx);
for(int i=0;i<n;++i) printf("%d ",ans[i]);
}
E1String Coloring (easy version)
meaning of the questions and ideas: and almost above, the color is specified only two, we put the above change it, once mx 2 outputs no greater than on the line.
#include<bits/stdc++.h>
using namespace std;
const int maxn=200+1;
char s[maxn];
int n,mx=0,ans[maxn],Max[maxn];
int main()
{
scanf("%d",&n);
scanf("%s",s);
for(int i=0;i<n;++i)
{
int cnt=0;
for(int j=s[i]+1;j<='z';++j)
cnt=max(cnt,Max[j]);
cnt++;
Max[s[i]]=cnt;
ans[i]=cnt;
mx=max(mx,cnt);
}
if(mx>2) puts("NO");
else {
puts("YES");
for(int i=0;i<n;++i) printf("%d",ans[i]-1);
}
}