题目

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 32321放在前面)

所以,这个函数应该这样写

bool cmp(string s1, string s2) {
// 字符串加法是拼接
return s1 + s2 < s2 + s1;
}

任意两个串组合只能得到两种组合结果,怎么样结果最小,就怎么样排序。

你可能还有个问题,如果某个字符串以0开始,它岂不是永远会被放在前面?比如0789 会被认为 小于 123,所以最后存储结果是 0789 123,但0789实际上是789789>321,所以你有点觉得结果应该是 321 0789,嗯?是不是觉得有点不对了,0789 123还就是小于321 0789,所以,这样写是没错的。

为啥呢???? 以0开始的串,排在最前面,0会被当做前导0移除,相当于组合数少了一位,如果放在中间,0不能省略,相当于组合数长了一位,长了一位那肯定更大了啊

既然这么麻烦,我能不能把所有串输入后,都把前导0去了,这样就可以直接用第一种排序方法了??,当然不可以,你这不相当于破坏原来数字了,比如 01230456,最终结果是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巧排序的更多相关文章

  1. PAT 甲级 1038 Recover the Smallest Number (30 分)(思维题,贪心)

    1038 Recover the Smallest Number (30 分)   Given a collection of number segments, you are supposed to ...

  2. 1038 Recover the Smallest Number (30分)(贪心)

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

  3. 【PAT甲级】1038 Recover the Smallest Number (30 分)

    题意: 输入一个正整数N(<=10000),接下来输入N个字符串,每个字符串包括至多8个字符,均为数字0~9.输出由这些字符串连接而成的最小数字(不输出前导零). trick: 数据点0只包含没 ...

  4. pat 甲级 1038. Recover the Smallest Number (30)

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

  5. 1038. Recover the Smallest Number (30)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...

  6. PAT 1038 Recover the Smallest Number[dp][难]

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  7. 1038 Recover the Smallest Number (30)(30 分)

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

  8. PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]

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

  9. 1038. Recover the Smallest Number (30) - 字符串排序

    题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...

随机推荐

  1. vue+express上传头像到数据库中img的路径

    项目结构 express中间件指定静态资源目录 app.use("/static",express.static(path.join(__dirname,"/public ...

  2. 「雕爷学编程」Arduino动手做(8)——湿度传感器模块

    37款传感器和模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器与模块,依照实践出真知(动手试试)的理念,以学习和交流为目的,这里准备 ...

  3. interface和abstract 的区别和相同点

    在Java语言中,abstract class和interface是支持抽象类定义的两种机制. 不能创建abstract类的实例,然而可以创建一个变量,其类型是一个抽象类,并让它指向具体子类的一个实例 ...

  4. MYSQL 中binlog 参数的记录

    http://dev.mysql.com/doc/refman/5.7/en/replication-options-binary-log.html binlog_cache_size Command ...

  5. Shone.Math开源系列2 — 实数类型(含分数和无理数)的实现

    Shone.Math开源系列2 实数类型(含分数和无理数)的实现 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/ShoneSharp. 摘要: ...

  6. golang基础教程——字符串篇

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是golang专题的第6篇文章,这篇主要和大家聊聊golang当中的字符串的使用. 字符串定义 golang当中的字符串本质是只读的字符 ...

  7. golang如何优雅的编写事务代码

    目录 前言 需求 烂代码示例 重构套路 一.提前返回去除if嵌套 二.goto+label提取重复代码 三.封装try-catch统一捕获panic 前言 新手程序员概有如下特点 if嵌套特别多.重复 ...

  8. Mysql的一次查询的过程

    1.用户发起请求,这里往往时多线程并发访问 2.去数据库线程池拿数据库链接,如果没有线程池,每次访问都要和数据库建立一次连接,非常耗时,效率低下 3.数据库层面上来说,可能会有多个系统同时访问它,所以 ...

  9. 前端基础进阶(六):在chrome开发者工具中观察函数调用栈、作用域链与闭包

    在前端开发中,有一个非常重要的技能,叫做断点调试. 在chrome的开发者工具中,通过断点调试,我们能够非常方便的一步一步的观察JavaScript的执行过程,直观感知函数调用栈,作用域链,变量对象, ...

  10. nacos 配置

    具体请访问 https://nacos.io/zh-cn/docs/what-is-nacos.html 网站查看文档,现在开始使用Nacos. 1. 下载Nacos源码 Nacos可以通过 http ...