题目

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 diferent 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 (<=10000) 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. Do not output leading zeros.

Sample Input:

5 32 321 3214 0229 87

Sample Output:

22932132143287

题目分析

已知一系列数字段,顺序可以随意调换,求最后组合成的最小数字(打印需要去掉前导0)

解题思路

  1. 对所有数字段排序,排序技巧有技巧性(s1+s2<s2+s1,排序结束后所有s1,s2对都是取s1+s2,所以整个数组s1+s2...sk组合为最小数字)

该证明来自晴神笔记P163

  1. 将排序处理完的所有数字段拼接,擦除前导0(擦除技巧有技巧性)

易错点

  1. 特殊情况:所有数字段都为0,擦除前导0后,可能最终字符串为空,需要输出0

Code

#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(string &s1,string &s2) {
return s1+s2<s2+s1;
}
int main(int argc, char * argv[]) {
int N;
scanf("%d",&N);
string ss[N];
for(int i=0; i<N; i++) cin>>ss[i];
// 排序
sort(ss,ss+N,cmp);
// 获取字符串结果
string rs;
for(int i=0; i<N; i++) rs+=ss[i];
// 擦除前导0
while(rs.length()!=0&&rs[0]=='0')rs.erase(rs.begin());
// 若rs都为0,擦除完后,长度为0;
if(rs.length()==0)printf("0");
else printf("%s",rs.c_str());
return 0;
}

PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]的更多相关文章

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

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

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

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

  3. 1038. Recover the Smallest Number (30)

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

  4. PAT甲1038 Recover the smallest number

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

  5. PAT 1038 Recover the Smallest Number (30分) string巧排序

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

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

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

  7. PAT 甲级 1038 Recover the Smallest Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 Given a collection of ...

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

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

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

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

随机推荐

  1. 148-PHP strip_tags函数,剥去字符串中的 HTML 标签(二)

    <?php //定义一段包含PHP代码的字符串 $php=<<<PHP 这里是PHP代码的开始 <?php echo "hello!"; PHP; $ ...

  2. 实验吧-隐写术-九连环(steghide)

    下载图片: 拿到kali里binwalk发现有压缩文件,然后foremost分解出来,将分出的压缩文件打开,发现已经被加密. 到这里就有几个思路了:1)暴力破解 2)伪加密 3)继续从图片中寻找信息 ...

  3. 说说lock到底要锁谁?

    波安搬... http://www.cnblogs.com/wolf-sun/p/4209521.html ---------------------------------------------- ...

  4. TypeScript 文件引入 Html (ts import html webpack)

    我们的目标是把html引入ts文件,webpack打包时就能把html打进js文件,减少文件加载啦 1 安装 text-loader npm install text-loader --save-de ...

  5. 【LeetCode】电话号码的字母组合

    [问题]给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:" 输出:["ad ...

  6. python利用百度云接口实现车牌识别

    一个小需求---实现车牌识别. 目前有两个想法 调云在线的接口或者使用SDK做开发(配置环境和编译第三方库很麻烦,当然使用python可以避免这些问题) 自己实现车牌识别算法(复杂) ! 一开始准备使 ...

  7. java web实现在线编辑word,并将word导出(一)

    前段时间领导交代了一个需求:客户需要一个能够web在线编辑文字,如同编辑word文档一样,同时能够将编辑完成的内容导出为word文档并下载到本地. 我们选择了前台使用富文本插件的形式用于编辑内容,使用 ...

  8. String类型不属于八种基本类型

    String不属于8种基本数据类型,String是一个对象.因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. new String( ...

  9. Django2.0中的urlpattern匹配不输入任何网址时的写法

    如果使用urlpattern匹配不输入任何网址时,应该如何写? 例如:仅匹配http://127.0.0.1:8000/时想要跳转到某个页面,这时urlpattern中的url规则应该写成: 情况1: ...

  10. CPU的成本构成

    1)设计成本: 工程师的工资,EDA等开发工具的费用.设备费用.场地费用等等. 2)硬件成本: 硬件成本=(晶片成本+掩膜成本+封装.测试成本)/成品率 1.晶片成本 一片硅晶圆 晶片成本=晶圆成本/ ...