A1038. Recover the Smallest Number
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的更多相关文章
- PAT甲级——A1038 Recover the Smallest Number
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- A1038 Recover the Smallest Number (30 分)
一.技术总结 此问题是贪心类问题,给出可能有前导零的数字串,将他们按照某个顺序拼接,使生成的数最小. 解决方案,就是使用cmp函数,因为两两字符串进行拼接,进行排序从小到大. 拼接过后会有0可能出现在 ...
- PAT_A1038#Recover the Smallest Number
Source: PAT A1038 Recover the Smallest Number (30 分) Description: Given a collection of number segme ...
- 1038 Recover the Smallest Number (30 分)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- 把数组排成最小的数/1038. Recover the Smallest Number
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. Give ...
- 1038. Recover the Smallest Number (30) - 字符串排序
题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...
- PAT甲1038 Recover the smallest number
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- 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[dp][难]
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
随机推荐
- 行业干货-如何逆向解决QT程序汉化中乱码问题
前言 “一款QT开发的国外软件,大概率是没有做中文支持的,所以你汉化中,不论怎么设置编码都一定是乱码.面对这个问题,你去互联网上找答案,答案却大多是复制粘贴的开发中解决乱码的文章,可是我们是要逆向中解 ...
- 索引节点(inode)爆满问题处理
关于磁盘空间中索引节点爆满的问题还是挺多的,借此跟大家分享几个情况: 情况一 在公司一台配置较低的Linux服务器(内存.硬盘比较小)的/data分区内创建文件时,系统提示磁盘空间不足,用df -h命 ...
- Python_命名空间和作用域_25
# 函数进阶 a = def func(): print(a) func() # 命名空间和作用域 # print() # input() # list # #命名空间 有三种 #内置命名空间 —— ...
- #个人博客作业Week1——流行的源程序版本管理软件和项目管理软件
1.TFS(Team Foundation Server)(1)定义:TFS是一个高可扩展.高可用.高性能.面向互联网服务的分布式文件系统,主要针对海量的非结构化数据, 它构筑在普通 ...
- Linux内核分析第四章 读书笔记
Linux内核分析第四章 读书笔记 第一部分--进程调度 进程调度:操作系统规定下的进程选取模式 面临问题:多任务选择问题 多任务操作系统就是能同时并发地交互执行多个进程的操作系统,在单处理器机器上这 ...
- Linux内核期中
Linux内核期中总结 一.计算机是如何工作的 个人理解:计算机就是通过和用户进行交互,执行用户的指令,这些指令存放在内存中,通过寄存器存储,堆栈变化,来一步步顺序执行. 二.存储程序计算机工作模型 ...
- JQuery Cross Domain Ajax(jsonp)
http://www.pureexample.com/jquery/cross-domain-ajax.html http://www.pureexample.com/ExampleTesterII- ...
- Laravel 5.5 文档 ] 快速入门 —— 安装配置篇
服务器要求 Laravel 框架对PHP版本和扩展有一定要求,不过这些要求 Laravel Homestead 都已经满足了,不过如果你没有使用 Homestead 的话(那真是一件很遗憾的事情),有 ...
- CentOS virt-manager 安装Win2008r2的一种GUI方法
1. 必须在物理机上面安装CentOS机器. 安装方法上一个blog里面简单写过. 注意一点,重复安装时 总是提示no disk found 我的解决办法使用 windows 安装盘 格式化了下磁盘重 ...
- Python模块笔记
__name__属性 一个模块被另一个程序第一次引入时,其主程序将运行.如果我们想在模块被引入时,模块中的某一程序块不执行,我们可以用__name__属性来使该程序块仅在该模块自身运行时执行. #!/ ...