PAT Advanced 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 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)
解题思路
- 对所有数字段排序,排序技巧有技巧性(s1+s2<s2+s1,排序结束后所有s1,s2对都是取s1+s2,所以整个数组s1+s2...sk组合为最小数字)
该证明来自晴神笔记P163
- 将排序处理完的所有数字段拼接,擦除前导0(擦除技巧有技巧性)
易错点
- 特殊情况:所有数字段都为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) [贪⼼算法]的更多相关文章
- pat 甲级 1038. Recover the Smallest Number (30)
1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 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)
题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...
- PAT甲1038 Recover the smallest number
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- PAT 1038 Recover the Smallest Number (30分) string巧排序
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- 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
https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 Given a collection of ...
- 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 them. Fo ...
随机推荐
- 111-PHP类变量之间的赋值标识为同一个对象
<?php class mao{ //定义猫类 public $age=0; //定义多个属性并初始化 public $weight=50; public $color='white'; } $ ...
- Essay写作常见错误精选
Essay写作常见错误精选.Essay写作有许多不为人注意的小细节,如果申请人在这些细节上不注意,往往会犯一些很典型的错误.和小编一起来看看留学Essay写作常见错误解析. 1)直接把申请学校A的Es ...
- PHP数据库基础(简单的)
经常用到的函数 $link=mysql_connect("数据库地址","用户名","密码");//链接数据库mysql_query(“se ...
- robotframework+selenium2Library怎样不用手动关掉代理
每次跑脚本,启动浏览器都要手动关掉代理,太费劲了,发现最简单的办法就是在局域网设置里面勾上跳过本地地址的代理服务器,并且在例外里写上127.0.0.1 就这么简单有没有??
- UVA - 1647 Computer Transformation(计算机变换)(找规律)
题意:初始串为一个1,每一步会将每个0改成10,每个1改成01,因此1会依次变成01,1001,01101001,……输入n(n<=1000),统计n步之后得到的串中,"00" ...
- apicloud - addEventListener 接收不到 sendEvent 的解决方法
要将 api.addEventListener 放在最前面 , 减少受到其他事件的影响 apiready = function () { api.addEventListener({ ...
- C++基础--函数模板
函数模板是通用的函数描述,其使用泛型来定义函数.其实就是有些操作,如果撇开具体的变量的数据类型,其操作是一样的如果我们将这些操作写成一个模板,在调用不同变量的时候就设定好变量类型就可了,后续的操作基本 ...
- ng-repeat动态生成的DOM如何获取宽度(封装好的方法)
define(['custom/bootstrapApp'],function(app){ app.filter('getTabWidth',function(){ return function(a ...
- Python基础+爬虫基础
Python基础+爬虫基础 一.python的安装: 1.建议安装Anaconda,会自己安装一些Python的类库以及自动的配置环境变量,比较方便. 二.基础介绍 1.什么是命名空间:x=1,1存在 ...
- Django学习路线
