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 (<=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
 #include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string str[], ans;
bool cmp(string a, string b){
return a + b < b + a;
}
int main(){
int N, index = -;
scanf("%d", &N);
for(int i = ; i < N; i++)
cin >> str[i];
sort(str, str + N, cmp);
for(int i = ; i < N; i++){
ans += str[i];
}
int i = ;
while(ans[i] != '\0' && ans[i] == ''){
index = i;
i++;
}
if(index != -)
ans.erase(, index + );
if(ans.length() == )
cout << "";
else cout << ans;
cin >> N;
return ;
}

总结:

1、题意:将若干字符串拼接起来,使得组成的数字最小。如果直接按照字典序由小到大排列再拼接是不对的。正确做法:比较串a和串b的位置关系时应考虑,如果a+b < b + a,则说明a前b后所组成的数字更小。

2、输出结果要去掉前导0,但不能仅仅对str[0]去0,因为有可能存在str[N] = {"0000", "0000", "0000"}的情况,应全部拼接起来后统一去除0,当最终得到的字串长度为0时,需要人为输出一个0。

3、string去除子串:str.erase(首位置, 长度)

A1038. Recover the Smallest Number的更多相关文章

  1. PAT甲级——A1038 Recover the Smallest Number

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

  2. A1038 Recover the Smallest Number (30 分)

    一.技术总结 此问题是贪心类问题,给出可能有前导零的数字串,将他们按照某个顺序拼接,使生成的数最小. 解决方案,就是使用cmp函数,因为两两字符串进行拼接,进行排序从小到大. 拼接过后会有0可能出现在 ...

  3. PAT_A1038#Recover the Smallest Number

    Source: PAT A1038 Recover the Smallest Number (30 分) Description: Given a collection of number segme ...

  4. 1038 Recover the Smallest Number (30 分)

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

  5. 把数组排成最小的数/1038. Recover the Smallest Number

    题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323.   Give ...

  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

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

  8. 1038. Recover the Smallest Number (30)

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

  9. PAT 1038 Recover the Smallest Number[dp][难]

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

随机推荐

  1. Linux umask

    新建一个文件或目录,它的默认权限是什么?如果要修改一个用户创建的文件和目录的默认权限该如何做?本文将介绍相关的内容.说明:本文的演示环境为 ubuntu 16.04. 文件的默认权限 为了查看用户创建 ...

  2. Mysql之binlog日志说明及利用binlog日志恢复数据操作记录

    众所周知,binlog日志对于mysql数据库来说是十分重要的.在数据丢失的紧急情况下,我们往往会想到用binlog日志功能进行数据恢复(定时全备份+binlog日志恢复增量数据部分),化险为夷! 一 ...

  3. Scrum Meeting NO.8

    Scrum Meeting No.8 1.会议内容 2.任务清单 徐越 序号 近期的任务 进行中 已完成 1 代码重构:前端通讯模块改为HttpClient+Json √ 2 添加对cookies的支 ...

  4. 2-Twentieth Scrum Meeting-20151220

    任务安排 成员 今日完成 明日任务 闫昊 请假(数据库)   唐彬 请假(数据库)   史烨轩  尝试使用downloadmanager对notification进行更新  尝试使用downloadm ...

  5. 个人项目Individual Project:n皇后问题

     源码的github链接: https://github.com/luhan420/test/tree/master 1.需求分析 在本次的课程设计中,用到的知识点主要有:类.函数.选择结构里的条件语 ...

  6. 关于摄影O2O的前期准备

    更新内容暂时在这位同学的博客:http://www.cnblogs.com/ys1101/

  7. Alpha冲刺之事后诸葛亮

    组长博客 作业博客 项目Postmortem 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的软件针对的是福大学子来到食堂会犹豫不决无法决定吃什么 ...

  8. Minify or format javascript file by web or notepad++

    Notepad++ plugin manager install 'JSTOOL' http://tool.oschina.net/codeformat/js https://www.cnblogs. ...

  9. Spring Cloud Zipkin

    Zipkin the idea is from the googlge paper:Dapper https://yq.aliyun.com/articles/60165 https://www.e4 ...

  10. Maven-Build Lifecycle(构建生命周期)

    https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html https://www.w3cschoo ...