PAT Advanced 1038 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 diferent orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.
Input Specification:
Each input file contains one test case. Each case gives a positive integer N (<=10000) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print the smallest number in one line. Do not output leading zeros.
Sample Input:
5 32 321 3214 0229 87
Sample Output:
22932132143287
题目分析
已知一系列数字段,顺序可以随意调换,求最后组合成的最小数字(打印需要去掉前导0)
解题思路
- 对所有数字段排序,排序技巧有技巧性(s1+s2<s2+s1,排序结束后所有s1,s2对都是取s1+s2,所以整个数组s1+s2...sk组合为最小数字)
该证明来自晴神笔记P163
- 将排序处理完的所有数字段拼接,擦除前导0(擦除技巧有技巧性)
易错点
- 特殊情况:所有数字段都为0,擦除前导0后,可能最终字符串为空,需要输出0
Code
#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(string &s1,string &s2) {
return s1+s2<s2+s1;
}
int main(int argc, char * argv[]) {
int N;
scanf("%d",&N);
string ss[N];
for(int i=0; i<N; i++) cin>>ss[i];
// 排序
sort(ss,ss+N,cmp);
// 获取字符串结果
string rs;
for(int i=0; i<N; i++) rs+=ss[i];
// 擦除前导0
while(rs.length()!=0&&rs[0]=='0')rs.erase(rs.begin());
// 若rs都为0,擦除完后,长度为0;
if(rs.length()==0)printf("0");
else printf("%s",rs.c_str());
return 0;
}
PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]的更多相关文章
- pat 甲级 1038. 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 分)(思维题,贪心)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to ...
- 1038. Recover the Smallest Number (30)
题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...
- PAT甲1038 Recover the smallest number
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- PAT 1038 Recover the Smallest Number (30分) string巧排序
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- 1038. Recover the Smallest Number (30) - 字符串排序
题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...
- PAT 甲级 1038 Recover the Smallest Number
https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 Given a collection of ...
- 1038 Recover the Smallest Number (30)(30 分)
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- 1038 Recover the Smallest Number (30分)(贪心)
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
随机推荐
- 【转】AutoMapper对象映射
什么是AutoMapper?AutoMapper是一个简单的小型库,用于解决一个看似复杂的问题 - 摆脱将一个对象映射到另一个对象的代码.这种类型的代码是相当沉闷和无聊的写,所以为什么不发明一个工具来 ...
- you-get加ffmpeg获取视频素材并转格式
最近做视频,觉得素材不好下载,下载了转格式又很麻烦,终于,在网上ob了很久的我找到了属于自己的工具. you-get视频下载 当你在网上找视频素材的时候发现了一个自己觉得很有意思的视频,但是获取这个视 ...
- Django中出现no such table: django_session
这个错误跟Session的机制有关, 既然要从Web服务器端来记录用户信息, 那么一定要有存放用户session id对应信息的地方才行. 所以,我们需要创建django_session表. Djan ...
- 操作CLOB数据——oracle
DECLARE V_UPDATE CLOB := '{"cpc_msg_tel":"15098025316","cvm_money":&qu ...
- C# Process类详解
C# Process类详解 Process[] processes = Process.GetProcessesByName(current.ProcessName); 根据进程名字找到所有进程,返回 ...
- [BJDCTF2020]ZJCTF,不过如此
0x00 知识点 本地文件包含伪协议 ?text=php://input //执行 post: I have a dream ?file=php://filter/read/convert.base6 ...
- mongodb replication set 主从切换
今天被问到mongodb副本集主从切换的问题,然后发现没有相关命令的中文文档,这里翻译记录一下 rs.stepDown() rs.stepDown(stepDownSecs, secondaryCat ...
- 【LeetCode】搜索旋转排序数组
[问题]假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 搜索一个给定的目标值,如果数组中存在这个 ...
- java课程课后作业190530之找水王
从题目中我们可以看出,水王有着相当严苛的条件才可以成为,那就是必须拥有一半的评论量才可以当上水王.当然这就是破题的关键,最简单的算法当然是用O(N平方)的复杂度的那种算法,但显然,我们需要的不是这种. ...
- JVM探秘:jinfo查看JVM运行时参数
本系列笔记主要基于<深入理解Java虚拟机:JVM高级特性与最佳实践 第2版>,是这本书的读书笔记. 如何查看JVM运行时参数,对于线上JVM调优是很关键的,因为只有知道了当前使用的JVM ...
