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. WPF没落了吗?

    从08年开始一直到现在,碰到所有的项目,我个人经手的,都用wpf开发. wpf应该说一直没有火过,一直平平淡淡. 个人为什么一直执着用wpf,开始使用是因公司项目,做了两年wpf开发,后来换工作一直搜 ...

  2. 如何在《救赎之路》中使用CPU粒子效果

    Unreal游戏引擎4.19版本的发布,可以使得游戏可以更好地利用Intel多核心处理器的性能,以提供更精彩的游戏体验.这里以<救赎之路>这款优秀的国产独立游戏为例说明如何在游戏中使用CP ...

  3. zabbix监控-基本原理介绍

    一.Linux下开源监控系统简单介绍1)cacti:存储数据能力强,报警性能差2)nagios:报警性能差,存储数据仅有简单的一段可以判断是否在合理范围内的数据长度,储存在内存中.比如,连续采样数据存 ...

  4. python 批量下载图片

    #coding=utf-8import re,sysimport urllib def getHtml(url): page = urllib.urlopen(url) html = page.rea ...

  5. Spring Cloud :断路器集群监控(Turbine)

    一. 简介      上一篇文章我们已经实现了对单个服务实例的监控,当然在实际应用中,单个实例的监控数据没有多大的价值,我们更需要的是一个集群系统的监控信息,这时我们就需要引入Turbine.Turb ...

  6. jenkins中配置svn 出现absolute path is not allowed

    代码: 兵马未动,粮草先行 作者: 传说中的汽水枪 如有错误,请留言指正,欢迎一起探讨. 转载请注明出处. 想用jenkins作自动化部署tomcat. svn代码已经checkout到本地目录了(/ ...

  7. 软件工程驻足篇章:第十七周和BugPhobia团队漫长的道别

    0x01 :序言 I am a slow walker, but I never walk backwards. 成长于被爱,学着爱人 成长的故事 也是年少的星期六结束的故事 就仿佛我和BugPhob ...

  8. Centos6.5下VNC Server配置

    整个流程如下~ 1.首先,检查下本机是否安装了vnc: 提示没有安装,那么开始装吧: 2.先关闭防火墙 查看防火墙状态: 关闭防火墙: 注:要切到root下 [carsonzhu@localhost ...

  9. FEAT: Front-End Auto Testing

    FEAT FEAT: Front-End Auto Testing 前端自动化测试 jest $ yarn add -D jest # OR $ npm i -D jest https://jestj ...

  10. Java微信二次开发(六)

    Token定时获取 需要导入库:添加log4j(slf4j-api-1.5.10.jar,slf4j-log4j12-1.5.10.jar,log4j-1.2.15.jar,并且在src下添加log4 ...