• 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. }

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

  • 相关阅读:
    消防设备电源监控系统在城市建筑中的应用
    Collection的使用
    如何将Mysql数据库的表导出并导入到另外的架构
    适合新手的Pytorch的中文文档
    C/C++ 通过域名获取服务器真实IP地址
    JAVA设计模式之模板方法模式
    Linux线程间交互
    ClickHouse 如何实现数据一致性
    2024年3月第15届蓝桥杯青少组STEMA考试C++中高级真题试卷
    利用STM32CubeMX软件生成USB_HOST_HID连接鼠标和键盘扫码枪
  • 原文地址:https://blog.csdn.net/QZZ_PP/article/details/125628260