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)N (≤10^4)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个数字,然后组合成一个数字,求最小的组合。
解题思路: 这道题的关键在于,抽象出题目要求的排序规则,使用sort
函数进行排序。我们不妨从Demo中总结一下——
显然,小的要放前面,0229比3214要小,比更短的87也更小,比其他都要小,所以放最前面。这一点符合string的operator<
规则,可以直接使用a<b。
但是麻烦的是, 并不是所有的字符串都符合这个规则,其中,3214比32要大,但是如果把3214放在32前面,显然不是正确的选择——
但如果 (a+b)<(b+a)的组合成立的话 ,我们发现,这几乎符合所有的条件,我们可以得到最小的排序组合。根据sort的排序规则,如果符合规则(a+b)<(b+a),那么a和b保持原来的顺序,否则交换顺序。
此外,测试数据存在边界情况,第二个测试点,最大的数据可能是0,此时要输出0。
/*
** @Brief:No.1038 of PAT advanced level.
** @Author:Jason.Lee
** @Date:2018-12-16
** @status: Accepted!
*/
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
int main(){int N;while(cin>>N){vector<string> segments;string temp;for(int i=0;i<N;i++){cin>>temp;segments.push_back(temp);}// 使用lambda表达式比定义cmp函数更简洁sort(segments.begin(),segments.end(),[](string a,string b){return (a+b)<(b+a);});// 使用stringstream可以轻易的把string转换成int型,//易于判断string是否是零,以及消除首部多余的0stringstream strNum;int number;strNum<<segments[N-1];strNum>>number;if(number==0){cout<<0<<endl;continue;}strNum.clear();strNum<<segments[0];strNum>>number;cout<<number;for(int i=1;i<N;i++){cout<<segments[i];}cout<<endl;}return 0;
}
总结
这道题有一种大道至简、返璞归真的感觉,最开始总结规律,搞得很复杂,把排序规则写的很冗余,但是还是通过了,后来参考了别人的代码,发现原来可以如此简洁,看来总结抽象的能力还是不够。发现了规律了之后,其实并不难。这道题我学到了——
1) 使用stringstream转换string到int,更快捷高效;
2) 发现规律,抽象建模很重要,或许这就是我们常说的解决问题的能力。
1038 Recover the Smallest Number (30 分)-字符串分段排序相关推荐
- PAT甲级1038 Recover the Smallest Number (30 分):[C++题解]贪心、排列成最小的数、字符串
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 贪心: 对于字符串a和b,如果 a+b < b+a (这里+代表字符串中的连接)代表字典序更小.举例 a = 321 , b ...
- 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 分)【难度: 中 / 知识点: 贪心 思维】
https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 PAT上的这道题的数据有点弱,其它网站上的数 ...
- 1038. Recover the Smallest Number (30)
1038. Recover the Smallest Number (30) 进行排序,注意comp的写法: #include <iostream> #include <vector ...
- PAT (Advanced Level) 1038. Recover the Smallest Number (30)
注意前导零的消去. #include <iostream> #include <string> #include <sstream> #include <al ...
- pat1038. Recover the Smallest Number (30)
1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- A1038 Recover the Smallest Number (30)
Powered by:NEFU AB-IN Link 文章目录 A1038 Recover the Smallest Number (30) 题意 思路 代码 A1038 Recover the Sm ...
- PAT甲级:1038 Recover the Smallest Number
题目描述: Given a collection of number segments, you are supposed to recover the smallest number from th ...
- PAT——Recover the Smallest Number
Recover the Smallest Number 题目 AC代码 参考 知识点总结 题目 Given a collection of number segments, you are suppo ...
- PAT-A-1038 Recover the Smallest Number 【贪心】 【二刷】
贪心算法,最核心的在于贪心策略上 Given a collection of number segments, you are supposed to recover the smallest num ...
最新文章
- 2018-3-15模式识别--学习笔记(一)
- select sqlite 唯一_SQLite中的SELECT子句使用通配符
- 不用额外变量交换两个整数的值
- Lambda架构在有赞广告平台的应用与演进
- STL之Map和MFC之CMap比较学习
- Geek的入门神器:micropython-能跑python的stm32开发板
- 华为鸿蒙os再见了安卓,再见了安卓!华为鸿蒙OS 2.0正式登场,开机只需19秒
- html5 type submit,input type=submit
- MATLAB学习笔记(十三)
- 优化mysql的21个建议_MySQL优化小建议
- iar编译工程的map怎么看使用flash大小_ESP8266_08基于flash的数据掉电保护
- HDU 4455 Substrings(线性dp,很有意思)
- 原理图端口符号_电气百科:电气原理图和接线图区别详解
- 页式存储系统的逻辑地址是由页号和页内地址两部分组成的
- 一文详解|增长那些事儿
- 【微信开发】定制消息推送
- Windows流氓软件残留文件强制清除基于命令行
- Android 更改软键盘右下角按键的样式以及监听此键的方法
- CentOS镜像说明(附下载地址)
- 李宏毅深度强化学习(国语)课程(2018) 笔记(二)Proximal Policy Optimization(PPO)
热门文章
- [OpenAirInterface实战-16] :OAI 软件无线电USRP X300/X310硬件详解
- vue中图片解析失败
- 电脑声卡维修经验和实例完全分析
- 椭圆部分面积计算公式及微积分推导过程
- allure用例定制参数及报告效果展示
- A slightly scary story on Amazons
- 出现C:\Users\liu\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\contrib\rnn\pytho
- 工字型钢弹性截面模量计算公式_截面模量计算方法
- 计算机量子化学计算实验报告物化实验,化学反应焓变的量子化学理论计算实验报告.doc...
- winrar 5.80正式版全球发布