码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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. }

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

  • 相关阅读:
    AtCoder Beginner Contest 254 Ex. Multiply or Divide by 2(思维题/优先队列)
    政府信息化与电子政务、企业信息化与电子商务、数据库和数据仓库的区别、商业智能系统处理过程、数据仓库结构图、数据挖掘、数据仓库和数据湖的对比
    C++ 小游戏 视频及资料集(六)
    AI4 决策树的生成与训练-信息增益
    opencv实现人脸识别(c++实现)
    期货开户趋势的本质是惯性
    SpringCloud Feign异步调用传参问题
    LeetCode每日一题(931. Minimum Falling Path Sum)
    C# 多线程
    [springboot]结合redis详述声明式缓存注解的使用-Cacheable、CacheEvict、CachePut、Caching
  • 原文地址:https://blog.csdn.net/QZZ_PP/article/details/125628260
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号