Powered by:NEFU AB-IN

Link

文章目录

  • A1038 Recover the Smallest Number (30)
    • 题意
    • 思路
    • 代码

A1038 Recover the Smallest Number (30)

  • 题意

    Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.
    给定一个数组,数组中包含若干个整数,数组中整数可能包含前导 0。你需要将数组中的所有数字拼接起来排成一个数,并使得该数字尽可能小。

  • 思路

    判断数字a, b拼凑成的数字ab还有哪个大,直接用拼接之后的字符串比较大小就可以
    即如果字符串比较 a b < b a ab<ba ab<ba,则ab表示的数小于ba表示的数,注意是小于号,没有等于!!别管为啥了,cpm函数都这样

    所以,如果也排成最大的数也是同理

    另原理,一个集合如果能够排序,那么它一定是全序集,全序集需要满足三个性质:反对称性、传递性、完整性
    本题可证明用 return a + b < b + a; 的方法,来达到题目所需的要求,并且能够证明以上三条性质

  • 代码

    /*
    * @Author: NEFU AB-IN
    * @Date: 2023-01-09 12:57:42
    * @FilePath: \GPLT\A1038\A1038.cpp
    * @LastEditTime: 2023-01-09 13:20:26
    */
    #include <bits/stdc++.h>
    using namespace std;
    #define int long long
    #undef int#define SZ(X) ((int)(X).size())
    #define ALL(X) (X).begin(), (X).end()
    #define IOS                                                                                                            \ios::sync_with_stdio(false);                                                                                       \cin.tie(nullptr);                                                                                                  \cout.tie(nullptr)
    #define DEBUG(X) cout << #X << ": " << X << '\n'
    typedef pair<int, int> PII;const int N = 1e5 + 10, INF = 0x3f3f3f3f;signed main()
    {IOS;int n;cin >> n;vector<string> s(n);for (int i = 0; i < n; ++i)cin >> s[i];sort(ALL(s), [&](const string &a, const string &b) { return a + b < b + a; });string res;for (int i = 0; i < SZ(s); ++i)res += s[i];int i = 0;while (i < SZ(res) - 1 && res[i] == '0') // 去掉前导0的同时,一定要保留一位,也就是如果最后剩下0,也要保留++i;cout << res.substr(i);return 0;
    }
    

A1038 Recover the Smallest Number (30)相关推荐

  1. pat1038. Recover the Smallest Number (30)

    1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  2. PAT甲级1038 Recover the Smallest Number (30 分):[C++题解]贪心、排列成最小的数、字符串

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 贪心: 对于字符串a和b,如果 a+b < b+a (这里+代表字符串中的连接)代表字典序更小.举例 a = 321 , b ...

  3. 1038. Recover the Smallest Number (30)

    1038. Recover the Smallest Number (30) 进行排序,注意comp的写法: #include <iostream> #include <vector ...

  4. 1038 Recover the Smallest Number (30分)

    题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...

  5. 1038 Recover the Smallest Number (30 分)【难度: 中 / 知识点: 贪心 思维】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 PAT上的这道题的数据有点弱,其它网站上的数 ...

  6. PAT (Advanced Level) 1038. Recover the Smallest Number (30)

    注意前导零的消去. #include <iostream> #include <string> #include <sstream> #include <al ...

  7. PAT——Recover the Smallest Number

    Recover the Smallest Number 题目 AC代码 参考 知识点总结 题目 Given a collection of number segments, you are suppo ...

  8. PAT甲级:1038 Recover the Smallest Number

    题目描述: Given a collection of number segments, you are supposed to recover the smallest number from th ...

  9. PAT-A-1038 Recover the Smallest Number 【贪心】 【二刷】

    贪心算法,最核心的在于贪心策略上 Given a collection of number segments, you are supposed to recover the smallest num ...

最新文章

  1. VIVO X1手机通过USB连接电脑访问tomcat
  2. [JavaScript]JS由来
  3. 如何去设计硬件与程序之间的通信协议
  4. 这样子称象你试过没有?
  5. 百度内部培训PPT流出:数据分析的道与术
  6. america/los_angeles 时区 java_在Java ME中将“America / Los Angeles”时区转换为“PST”或“PDT”...
  7. TopCoder中插件的用法
  8. linux重启gpu_远程配置Ubuntu深度学习服务器GPU驱动+Docker+CUDA多个版本
  9. Codeforces 990G 点分治+暴力
  10. MacOS壁纸文件夹如何查找
  11. 数据中台:建立在数据网络效应之上的赛道
  12. pythondocumentation_python官方文档
  13. Debussy-54v9安装
  14. 【applicationContext.xml】spring 配置文件头部声明
  15. 在Visio中快速的画一个虚线框
  16. 有了它,Python“咸鱼”迅速翻身!
  17. 租用香港服务器机房机柜,费用由哪些部分组成
  18. 最终幻想:探讨小鹏G9 800V 高压动力系统和架构路线
  19. 我们扒了扒那个“阿里美女高管”,真的不简单(附最新回应)
  20. 简述HEVC与VVC的视频编码过程

热门文章

  1. Anaconda详细安装及使用教程(带图文)
  2. NOI Online 2022
  3. STM32F103ZET6的时钟系统RCC配置
  4. 【GDKOI2005】建立航道
  5. 湖南大学计算机系纪学斌,王东-湖大信息科学与工程学院
  6. nodejs对连接mysql数据库进行封装,使用库实现对数据库的增删查改操作
  7. 解决microsoft windows 恶意软件删除工具 占用内存高
  8. java使用莱布尼茨公式迭代高精度的圆周率
  9. 操作系统——饥饿、死锁、死循环的区别
  10. Mac常用快捷键汇总(程序员必会)