A1038 Recover the Smallest Number (30)
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)相关推荐
- pat1038. Recover the Smallest Number (30)
1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PAT甲级1038 Recover the Smallest Number (30 分):[C++题解]贪心、排列成最小的数、字符串
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 贪心: 对于字符串a和b,如果 a+b < b+a (这里+代表字符串中的连接)代表字典序更小.举例 a = 321 , b ...
- 1038. Recover the Smallest Number (30)
1038. Recover the Smallest Number (30) 进行排序,注意comp的写法: #include <iostream> #include <vector ...
- 1038 Recover the Smallest Number (30分)
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- 1038 Recover the Smallest Number (30 分)【难度: 中 / 知识点: 贪心 思维】
https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 PAT上的这道题的数据有点弱,其它网站上的数 ...
- PAT (Advanced Level) 1038. Recover the Smallest Number (30)
注意前导零的消去. #include <iostream> #include <string> #include <sstream> #include <al ...
- PAT——Recover the Smallest Number
Recover the Smallest Number 题目 AC代码 参考 知识点总结 题目 Given a collection of number segments, you are suppo ...
- PAT甲级:1038 Recover the Smallest Number
题目描述: Given a collection of number segments, you are supposed to recover the smallest number from th ...
- PAT-A-1038 Recover the Smallest Number 【贪心】 【二刷】
贪心算法,最核心的在于贪心策略上 Given a collection of number segments, you are supposed to recover the smallest num ...
最新文章
- VIVO X1手机通过USB连接电脑访问tomcat
- [JavaScript]JS由来
- 如何去设计硬件与程序之间的通信协议
- 这样子称象你试过没有?
- 百度内部培训PPT流出:数据分析的道与术
- america/los_angeles 时区 java_在Java ME中将“America / Los Angeles”时区转换为“PST”或“PDT”...
- TopCoder中插件的用法
- linux重启gpu_远程配置Ubuntu深度学习服务器GPU驱动+Docker+CUDA多个版本
- Codeforces 990G 点分治+暴力
- MacOS壁纸文件夹如何查找
- 数据中台:建立在数据网络效应之上的赛道
- pythondocumentation_python官方文档
- Debussy-54v9安装
- 【applicationContext.xml】spring 配置文件头部声明
- 在Visio中快速的画一个虚线框
- 有了它,Python“咸鱼”迅速翻身!
- 租用香港服务器机房机柜,费用由哪些部分组成
- 最终幻想:探讨小鹏G9 800V 高压动力系统和架构路线
- 我们扒了扒那个“阿里美女高管”,真的不简单(附最新回应)
- 简述HEVC与VVC的视频编码过程