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特别不喜欢那些随便转载别人的原创文章又不给 ...
随机推荐
- 玩转X-CTR100 l STM32F4 l DHT11温湿度传感器
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] DHT11数字温湿度传感器是一款含有已校准数字信号输 ...
- 玩转X-CTR100 l 平台-4WD智能小车
我造轮子,你造车,创客一起造起来!更多塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍使用X-CTR100控制器搭建4WD智能 ...
- 解决 Error: could not open `C:\Program Files\Java\jre7\lib\i386\jvm.cfg'
解决 Error: could not open `C:\Program Files\Java\jre7\lib\i386\jvm.cfg' 重装JDK后,因为没有装在以前的目录,运行java命令后报 ...
- 获取exe所在目录路径,速度
// test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #include &l ...
- promise的生命周期
每个promise都会经历一个短暂的生命周期: 先是处于进行中(pending)状态,此时操作并未完成,所以他也是未处理的(unsettled): 一旦异步惭怍执行结束,promise则 变为已处理( ...
- QAV250四轴穿越机安装全程详解(多图)
QAV250四轴穿越机安装全程详解 最近团队准备使用轻型穿越机QAV250做实验,本文记录了QAV250的安装过程,整理了开箱后较合理的安装顺序,以及各个步骤的注意事项,希望对有需要的朋友有所帮助.主 ...
- RCNN、SPP-net、Fast-RCNN和Faster-RCNN
RCNN RCNN (Regions with CNN features) 的核心思想是把图像划分成N(2000)个独立的区域,分别提取每个区域的CNN特征,然后把这些特征使用SVM等分类器进行结果预 ...
- QT 5.4.1 for Android Windows环境搭建
QT 5.4.1 for Android Windows环境搭建 2015-5-13 目录 一.参考文章: 二.准备软件: 三.安装准备好的软件: 四.配置Qt 5.4.1 for Android 五 ...
- cocoapods 安装过程及常见问题
1.可以参考这个网页的教程:http://code4app.com/article/cocoapods-install-usage 2.按照以下步骤进行安装: 1.配置rugy静态环境 gem sou ...
- CTF-练习平台-Misc之 中国菜刀,不再web里?
八.中国菜刀,不再web里? 下载文件后解压,是一个数据包,用wireshark打开,题中说的是菜刀,那就找http协议,首先过滤出http协议包 在第四个里面找到一句话木马 Flag应该在木马之后拿 ...