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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 使用MyEclipse开发Java EE应用:EJB项目开发初探(下)
你开学,我放价!MyEclipse线上狂欢继续!火热开启中>> [MyEclipse最新版下载] 三.EJB 3.x项目中的持久性支持 当创建EJB 3.x项目时,作为选项您可以添加JPA ...
- MyEclipse WebSphere开发教程:WebSphere 8安装指南(二)
[周年庆]MyEclipse个人授权 折扣低至冰点!立即开抢>> [MyEclipse最新版下载] IBM为使用WebSphere测试应用程序的开发人员提供了免费的WebSphere Ap ...
- Ubuntu 12.04硬盘安装教程
从服务器下载Ubuntu 12.04光盘镜像文件到 C 盘.下载地址:\\192.167.100.225\share\Tool\Ubuntu\ubuntu-12.04.1-desktop-amd64. ...
- ARM裸板开发:04_MMU 链接地址与运行地址不一致时,(SDRAM)初始化程序地址无关码问题的分析
ARM裸板开发过程,程序的链接地址设置为为0x30000000,而前期的启动代码以及相关硬件的初始化代码需要在内部iRAM(steppingstone,起始地址0x0)的4K中运行.链接地址与运行地址 ...
- JPush删除别名及回调函数(SWIFT)
JPush(极光)删除别名传空字符串即可,官方回调函数的例子为OC的.用SWIFT其实也差不多. //用户登出后删除别名 APService.setAlias("", callba ...
- 一次delete基表回收DBA权限的危险性操作
1.0事件还原:测试库某个对象,使用具有DBA角色用户导出,生产环境普通用户无法导入数据,因此需要回收测试库dba角色,revoke无法回收 2.0操作流程 回收角色报错 测试环境测试,delete删 ...
- 判断颜色信息-RGB2HSV(opencv)
前言 项目车号识别过程中,车体有三种颜色黑车黑底白字.红车红底白字.绿车黄底绿字,可以通过判断车体的颜色信息,从而判断二值化是否需要反转,主要是基于rgb2hsv函数进行不同颜色的阈值判断. matl ...
- 五个对你有用的Everything搜索技巧
分享五个对你有用的Everything搜索技巧: 一, empty:(查找空文件夹);二, dupe:(查重复文档);三, 空格(与), |(或),!(非); 四, e:\ (路径搜索);五, wil ...
- Maven配置dubbo环境简单例子
环境准备: 1.zookeeper:zookeeper-3.4.6版本 2.maven:apache-maven-3.3.9版本 3.dubbo监控工具:dubbo-admin-2.5.4-SNAPS ...
- js模板引擎---jtemplates使用
昨天记录了如何使用腾讯的模板引擎,今天记录一下jquery的模板引擎jtemplates.官网:http://jtemplates.tpython.com/ 编写模板:需要在页面引入jquery和jt ...