PAT 1038 Recover the Smallest Number (30分) string巧排序
题目
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
个数字字符串,比如"123","0789","32","321"
,找出这几个串拼接后形成的最小的数字,并且要去掉前导零
思路:
思路大家都能想到,就是把这些字符串从小到大排序,越小的越在前,这样直接拼接,就能得到最小的数字,最后去掉前导0
。
重点:排序用的比较函数如何写?
bool cmp(string s1, string s2) {
// 字符串比较
return s1 < s2;
}
这样写有什么问题?比如 "32"
会被认为 小于
"321"
,从而放在前面,导致最后拼接的串是 32 321
,但实际上 32 321 > 321 32
(321
放在前面)
所以,这个函数应该这样写
bool cmp(string s1, string s2) {
// 字符串加法是拼接
return s1 + s2 < s2 + s1;
}
任意两个串组合只能得到两种组合结果,怎么样结果最小,就怎么样排序。
你可能还有个问题,如果某个字符串以0开始,它岂不是永远会被放在前面?比如0789
会被认为 小于 123
,所以最后存储结果是 0789 123
,但0789
实际上是789
,789>321
,所以你有点觉得结果应该是 321 0789
,嗯?是不是觉得有点不对了,0789 123
还就是小于321 0789
,所以,这样写是没错的。
为啥呢???? 以0
开始的串,排在最前面,0
会被当做前导0
移除,相当于组合数少了一位,如果放在中间,0
不能省略,相当于组合数长了一位,长了一位那肯定更大了啊
既然这么麻烦,我能不能把所有串输入后,都把前导0
去了,这样就可以直接用第一种排序方法了??,当然不可以,你这不相当于破坏原来数字了,比如 0123
和0456
,最终结果是01230456->1230456
,你把两个0
去了,成啥了 123456
,这差了多少?
综上,比较函数只能这么写
bool cmp(string s1, string s2) {
// 字符串加法是拼接
return s1 + s2 < s2 + s1;
}
去除前导0
,这个比较好办,字符串全拼接在一起后,判断第一个字符,如果是0
,就去除第一个字符。
// 去除前导0
while(res[0] == '0') res.erase(res.begin());
这里我用的erase()
方法,注意这个函数只传一个参数(位置索引)的情况下,如果参数是个数字,那么它会把字符串从这个索引往后部分全部删除;如果传入是个迭代器,那么它移除的只是这个位置上的一个字符。
最后记得考虑一下特殊情况
// 排除特殊情况
if(res == "") cout << 0;
完整代码
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
// 不能写成 return a < b
// 比如 "32" < "321",但是 32 321 > 321 32
bool cmp(string s1, string s2) {
// 字符串加法是拼接
return s1 + s2 < s2 + s1;
}
string str[10000];
int main() {
// n个数字串
int n;
cin >> n;
// 输入
for(int i = 0; i < n; i++)
cin >> str[i];
// 排序
sort(str, str + n);
// 拼接
string res = "";
for (int i = 0; i < n; ++i)
res += str[i];
// 去除前导0
while(res[0] == '0') res.erase(res.begin());
// 排除特殊情况
if(res == "") cout << 0;
cout << res;
return 0;
}
PAT 1038 Recover the Smallest Number (30分) string巧排序的更多相关文章
- 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 分)
题意: 输入一个正整数N(<=10000),接下来输入N个字符串,每个字符串包括至多8个字符,均为数字0~9.输出由这些字符串连接而成的最小数字(不输出前导零). trick: 数据点0只包含没 ...
- 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)
题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...
- PAT 1038 Recover the Smallest Number[dp][难]
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- 1038 Recover the Smallest Number (30)(30 分)
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- PAT Advanced 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) - 字符串排序
题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...
随机推荐
- spark机器学习从0到1协同过滤算法 (九)
一.概念 协同过滤算法主要分为基于用户的协同过滤算法和基于项目的协同过滤算法. 基于用户的协同过滤算法和基于项目的协同过滤算法 1.1.以用户为基础(User-based)的协同过滤 用相似统 ...
- vue 细节注意
*只有vm.$data这些被代理的属性是响应的,能够重新渲染视图 *注意,不要在实例属性或者回调函数中(如 vm.$watch('a', newVal => this.myMethod()))使 ...
- 8.1Go并发
第八章 Go并发 Go语言区别于其他语言的一大特点就是出色的并发性能,最重要的一个特性那就是go关键字. 并发场景: UI小姐姐一边开着PS软件,一边微信疯狂的和产品经理打字交流,后台还听着网易云音乐 ...
- kudu_单master集群安装
1.配置JDK1.7/1.8,免密设置,ntp时间同步配置. 2. 将下载下来的文件放到/etc/yum.repos.d/ 目录下后,进行下一步 3.使用yum管理器安装 (集群搭建) sudo yu ...
- ORA-12519,TNS:no appropriate service handler found的问题 超过连接数
http://www.2cto.com/database/201205/133542.html ORA-12519,TNS:no appropriate service handler found的问 ...
- Java——关键字和保留字
Java关键字50个 abstract assert boolean break byte case catch char class const continue default do double ...
- Nginx 实现 HTTPS(基于 Let's Encrypt 的免费证书)
SSL / TLS加密会为您的用户带来更高的搜索排名和更好的安全性. Let’s Encrypt 是一个认证机构(CA).它可以提供免费证书,并且已经被大多数浏览器所信任.另外,通过工具 Certbo ...
- jq代替jsdom操作部分
接触js后学习了一些js操作html的方法 js可以配合css完成许多动画和操作.初次接触jquery感觉不是很习惯,毕竟js有了习惯,但是jq还是省去了很多繁琐的操作步骤. 首先使用之前 ...
- [软件版本贴]SD.TEAM软件集
手机轰炸机-版本:v1.0.下载地址:http://pan.baidu.com/1.zip! 营销软件盒-版本:v1.0.下载地址:http://pan.baidu.com/2.zip! 不加群提取群 ...
- 读Pyqt4教程,带你入门Pyqt4 _011
当我们想要改变或者增强已存在的窗口组件时,或者准备从零开始创建自定义窗口组件时,可以使用绘图.我们通过使用PyQt4工具包提供的绘图API来绘图. 绘图在 paintEvent() 方法中进行.绘制代 ...