题目

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. Django之forms.ModelForm

    通常在Django项目中,我们编写的大部分都是与Django 的模型紧密映射的表单. 举个例子,你也许会有个Book 模型,并且你还想创建一个form表单用来添加和编辑书籍信息到这个模型中. 在这种情 ...

  2. 机器学习必会工具gensim

    import jieba import gensim from gensim import corpora from gensim import models from gensim import s ...

  3. Hexo进阶设置

    部署平台选型 前言 GitHub和Gitee(码云)是国内外比较流行的代码托管平台,现都推出GitHub/Gitee Pages可以存放静态网页代码,因此可以用来搭建自己的博客. 优缺点 平台 优点 ...

  4. WordPress 设置图片的默认显示方式(尺寸/对齐方式/链接到)

    在文章中插入图片时,我们几乎每次都要设置图片的尺寸.对齐方式和链接方式,是比较耗时费力的.其实我们可以给这几个选项设置默认参数,省去我们每次设置的麻烦. 可以将下面的代码添加到主题的 function ...

  5. MySQL创建用户,并设置指定访问数据库

    一.创建用户并授权 1. 登录mysql mysql -u root -q输入密码2. 创建数据库(已有数据库就不需要建立) create database newDB;//以创建newDB为例3. ...

  6. el-select检索功能

    使用element-UI框架的使用,我们经常使用el-select下拉框,很多时候还需要使用可搜索的下拉框,然后elementUI官网的实例中只是提了一下filter-method可以自定义搜索方法, ...

  7. Linux以指定用户非root用户运行程序、进程

    方式一: 使用su命令切换用户运行 su 用户名 方式二: useradd -s /sbin/nologin -M test   -s /sbin/nologin表示创建一个禁止登陆的用户(比如www ...

  8. Multiple annotations found at this line:- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    解决办法: 右键所在项目 build path configure build path java build path Add Library server Run time (Apache Tom ...

  9. Java 第十一届 蓝桥杯 省模拟赛 梅花桩

    小明每天都要练功,练功中的重要一项是梅花桩. 小明练功的梅花桩排列成 n 行 m 列,相邻两行的距离为 1,相邻两列的距离也为 1. 小明站在第 1 行第 1 列上,他要走到第 n 行第 m 列上.小 ...

  10. Java实现 蓝桥杯 算法训练 猴子吃包子(暴力)

    试题 算法训练 猴子吃包子 问题描述 从前,有一只吃包子很厉害的猴子,它可以吃无数个包子,但是,它吃不同的包子速度也不同:肉包每秒钟吃x个:韭菜包每秒钟吃y个:没有馅的包子每秒钟吃z个:现在有x1个肉 ...