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 different 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 (≤104) 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. Notice that the first digit must not be zero.
Sample Input:
5 32 321 3214 0229 87
Sample Output:
22932132143287
题意:输入n个非负数数,求这n个数字组成的最小数
分析:贪心,对各个字符串排序,如果直接从小到大排序,会出现错误,比如32和321,排序后合并为32321,显然32132比它更小。
注意到这点题目就好做了,对sort自定义比较函数,a+b<b+a,那么让a+b在前面即可。
有个测试点是去掉前导0的时候可能字符串长度会变成0,此时直接输出0。
/** * Copyright(c) * All rights reserved. * Author : Mered1th * Date : 2019-02-26-11.49.45 * Description : A1038 */ #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<string> #include<unordered_set> #include<map> #include<vector> #include<set> using namespace std; ; string a[maxn]; bool cmp(string a,string b){ return a+b<b+a; //如果a+b<b+a,把a+b排在前面 } int main(){ #ifdef ONLINE_JUDGE #else freopen("1.txt", "r", stdin); #endif int n; cin>>n; ;i<n;i++){ cin>>a[i]; } sort(a,a+n,cmp); string ans=""; ;i<n;i++){ ans+=a[i]; } ]=='){ ans.erase(ans.begin()); } if(ans.size()) cout<<ans; "; ; }
1038 Recover the Smallest Number (30 分)的更多相关文章
- 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分)(贪心)
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- PAT 1038 Recover the Smallest Number (30分) string巧排序
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- 【PAT甲级】1038 Recover the Smallest Number (30 分)
题意: 输入一个正整数N(<=10000),接下来输入N个字符串,每个字符串包括至多8个字符,均为数字0~9.输出由这些字符串连接而成的最小数字(不输出前导零). trick: 数据点0只包含没 ...
- 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 (30)
1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 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 ...
- PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- PAT甲题题解-1038. Recover the Smallest Number (30)-排序/贪心,自定义cmp函数的强大啊!!!
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789138.html特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 项目使用Nuget,然后SVN checkout后显示缺少引用
如下图黄色叹号: 解决方案: 1.先生成解决方案 2.执行如下: 这时候Nuget是存在了,但是还是显示缺少引用: 那么最后一步, 输入 :Update-Package -reinstall
- docker中进行IDA远程调试提示“TRACEME: Operation not permitted[1] Closing connection from 192.168.109.1...”的解决方法
加入 --security-opt seccomp:unconfined选项,关闭docker远程命令执行保护 如: docker run --security-opt seccomp:unconfi ...
- Android2.1消息应用(Messaging)
我想首先应该从AndroidManifest.xml文件开始,该文件是Android应用(APK)的打包清单,其中提供了关于这个应用程序的基本信息,如名称(application/@label),图标 ...
- SWIFT中使用AFNetwroking访问网络数据
AFNetworking 是 iOS 一个使用很方便的第三方网络开发框架,它可以很轻松的从一个URL地址内获取JSON数据. 在使用它时我用到包管理器Cocoapods 不懂的请移步: Cocoapo ...
- Springboot中使用缓存
在开发中,如果相同的查询条件去频繁查询数据库, 是不是会给数据库带来很大的压力呢?因此,我们需要对查询出来的数据进行缓存,这样客户端只需要从数据库查询一次数据,然后会放入缓存中,以后再次查询时可以从缓 ...
- HDU2034:人见人爱A-B
Problem Description 参加过上个月月赛的同学一定还记得其中的一个最简单的题目,就是{A}+{B},那个题目求的是两个集合的并集,今天我们这个A-B求的是两个集合的差,就是做集合的减法 ...
- python--selenium多线程执行用例实例/执行多个用例
python--selenium多线程执行用例实例/执行多个用例 我们在做selenium测试的时候呢,经常会碰到一些需要执行多个用例的情况,也就是多线 程执行py程序,我们前面讲过单个的py用例怎么 ...
- 2018-2019-2 20165212 《网络对抗技术》Exp3 免杀原理与实践
2018-2019-2 20165212 <网络对抗技术>Exp3 免杀原理与实践 一.实验内容 正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion ...
- 【转】python mysql数据库 'latin-1' codec can't encode character错误问题解决
UnicodeEncodeError: 'latin-1' codec can't encode character "UnicodeEncodeError:'latin-1' code ...
- xsl -fo 了解
XSL-FO是用于格式化XML数据的语言,全称为Extensible Stylesheet Language Formatting Objects(格式化对象的可扩展样式表语言),是W3C参考标准,现 ...