M - Two Operations
https://vjudge.csgrandeur.cn/problem/Gym-102263M
Ayoub has a string SS consists of only lower case Latin letters, and he wants you to make some operations on it:
- you can swap any two characters in the string.
- you can delete any two adjacent characters that have the same value and replace them with the next character alphabetically,for example string "abbx""abbx" could be "acx""acx" after one operation, string "zz""zz" could not be changed; because z is the last character in the English alphabets.
Ayoub wants you to make the string lexicographically maximal using the mentioned operations as many times as you want, can you help him?
String x=x1x2...x|x|x=x1x2...x|x| is lexicographically larger than string y=y1y2...y|y|y=y1y2...y|y|, if either |x|>|y||x|>|y| and x1=y1,x2=y2,...,x|y|=y|y|x1=y1,x2=y2,...,x|y|=y|y|, or exists such number r(r<|x|,r<|y|)r(r<|x|,r<|y|), that x1=y1,x2=y2,...,xr=yrx1=y1,x2=y2,...,xr=yr and xr+1>yr+1xr+1>yr+1. Characters in lines are compared like their ASCII codes.
Input
The input contains a single string SS (1≤|S|≤105)(1≤|S|≤105).
It is guaranteed that string SS consists of only lower case Latin letters.
Output
print the lexicographically maximal string that could be obtained using these two operations.
Examples
input
Copy
abbx
output
Copy
xca
input
Copy
zyayz
output
Copy
zzza
Note
In the first test case Ayoub replaced "bb""bb" with "c""c" so the string has been changed to "acx""acx", then he swapped 'a' with 'x' so the result is "xca""xca" and it is the lexicographically maximal string.
正解:
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int vis[N];
int main()
{
string s;
cin>>s;
int len=s.size();
for(int i=0;i<len;i++)
{
vis[s[i]]++;
}
for(int i=97;i<=121;i++)
{
vis[i+1]=vis[i+1]+vis[i]/2;//转换完成后加到下一组
vis[i]=vis[i]%2;//vis[i]剩下的
}
for(int i=122;i>=97;i--)
{
while(vis[i]--)
{
printf("%c",i);
}
}
cout<<endl;
}
错解:
#include<bits/stdc++.h>
using namespace std;
bool cmp(char a,char b)
{
return a>b;
}
int main()
{
char s[10010];
int n;
map<char,int>m;
scanf("%s",s);
n=strlen(s);
sort(s,s+n,cmp);
for(int i=0;i<n;i++)
{
m[s[i]]++;
}
for(int i=0;i<n;i++)
{
if(s[i]=='z')
printf("z");
else
{
int num=m[s[i]];
if(num%2==0)
{
num=num/2;
while(num--)
{
printf("%c",s[i]+1);
}
m[s[i]]=0;
}
else
{
num=num/2;
while(num--)
{
printf("%c",s[i]+1);
}
printf("%c",s[i]);
m[s[i]]=0;
}
}
}
return 0;
}
感觉这题没说清楚,没说转换完后还可以再转换