• M - Two Operations


    M - Two Operationshttps://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:

    1. you can swap any two characters in the string.
    2. 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.

    正解:

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. const int N=1e5+10;
    4. int vis[N];
    5. int main()
    6. {
    7. string s;
    8. cin>>s;
    9. int len=s.size();
    10. for(int i=0;i<len;i++)
    11. {
    12. vis[s[i]]++;
    13. }
    14. for(int i=97;i<=121;i++)
    15. {
    16. vis[i+1]=vis[i+1]+vis[i]/2;//转换完成后加到下一组
    17. vis[i]=vis[i]%2;//vis[i]剩下的
    18. }
    19. for(int i=122;i>=97;i--)
    20. {
    21. while(vis[i]--)
    22. {
    23. printf("%c",i);
    24. }
    25. }
    26. cout<<endl;
    27. }

    错解:

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. bool cmp(char a,char b)
    4. {
    5. return a>b;
    6. }
    7. int main()
    8. {
    9. char s[10010];
    10. int n;
    11. map<char,int>m;
    12. scanf("%s",s);
    13. n=strlen(s);
    14. sort(s,s+n,cmp);
    15. for(int i=0;i<n;i++)
    16. {
    17. m[s[i]]++;
    18. }
    19. for(int i=0;i<n;i++)
    20. {
    21. if(s[i]=='z')
    22. printf("z");
    23. else
    24. {
    25. int num=m[s[i]];
    26. if(num%2==0)
    27. {
    28. num=num/2;
    29. while(num--)
    30. {
    31. printf("%c",s[i]+1);
    32. }
    33. m[s[i]]=0;
    34. }
    35. else
    36. {
    37. num=num/2;
    38. while(num--)
    39. {
    40. printf("%c",s[i]+1);
    41. }
    42. printf("%c",s[i]);
    43. m[s[i]]=0;
    44. }
    45. }
    46. }
    47. return 0;
    48. }

    感觉这题没说清楚,没说转换完后还可以再转换

  • 相关阅读:
    读excel报错LeftoverDataException
    HAL库实现基于STM32+RN8302B的电压采集
    蓝桥杯第18481题——皇家守卫(单调栈+线段树)
    1. 约瑟夫问题
    “华为杯”研究生数学建模竞赛2015年-【华为杯】B题:数据的多流形结构分析(续)(附python代码实现))
    “深入理解事件处理器、表单综合案例和组件通信“
    踩坑篇-Nacos+Sping-gateway+shiro实现分布式认证权限框架
    app逆向1某联
    Openssl
    武田公司2022财年第一季度业绩强劲;正稳步实现全年的管理层指引目标
  • 原文地址:https://blog.csdn.net/QZZ_PP/article/details/125628260