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 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 (≤10​4​​) 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. Notice that the first digit must not be zero.

Sample Input:

  1. 5 32 321 3214 0229 87

Sample Output:

  1. 22932132143287
  2. 题意:输入n个非负数数,求这n个数字组成的最小数
  3. 分析:贪心,对各个字符串排序,如果直接从小到大排序,会出现错误,比如32321,排序后合并为32321,显然32132比它更小。
  4. 注意到这点题目就好做了,对sort自定义比较函数,a+b<b+a,那么让a+b在前面即可。
  5. 有个测试点是去掉前导0的时候可能字符串长度会变成0,此时直接输出0
  1. /**
  2. * Copyright(c)
  3. * All rights reserved.
  4. * Author : Mered1th
  5. * Date : 2019-02-26-11.49.45
  6. * Description : A1038
  7. */
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<iostream>
  11. #include<cmath>
  12. #include<algorithm>
  13. #include<string>
  14. #include<unordered_set>
  15. #include<map>
  16. #include<vector>
  17. #include<set>
  18. using namespace std;
  19. ;
  20. string a[maxn];
  21. bool cmp(string a,string b){
  22. return a+b<b+a; //如果a+b<b+a,把a+b排在前面
  23. }
  24. int main(){
  25. #ifdef ONLINE_JUDGE
  26. #else
  27. freopen("1.txt", "r", stdin);
  28. #endif
  29. int n;
  30. cin>>n;
  31. ;i<n;i++){
  32. cin>>a[i];
  33. }
  34. sort(a,a+n,cmp);
  35. string ans="";
  36. ;i<n;i++){
  37. ans+=a[i];
  38. }
  39. ]=='){
  40. ans.erase(ans.begin());
  41. }
  42. if(ans.size()) cout<<ans;
  43. ";
  44. ;
  45. }
  1.  

1038 Recover the Smallest Number (30 分)的更多相关文章

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

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

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

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

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

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

  4. 【PAT甲级】1038 Recover the Smallest Number (30 分)

    题意: 输入一个正整数N(<=10000),接下来输入N个字符串,每个字符串包括至多8个字符,均为数字0~9.输出由这些字符串连接而成的最小数字(不输出前导零). trick: 数据点0只包含没 ...

  5. 1038. Recover the Smallest Number (30)

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

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

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

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

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

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

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

  9. PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]

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

  10. PAT甲题题解-1038. Recover the Smallest Number (30)-排序/贪心,自定义cmp函数的强大啊!!!

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6789138.html特别不喜欢那些随便转载别人的原创文章又不给 ...

随机推荐

  1. L1-008 求整数段和

    给定两个整数A和B,输出从A到B的所有整数以及这些数的和. 输入格式: 输入在一行中给出2个整数A和B,其中−,其间以空格分隔. 输出格式: 首先顺序输出从A到B的所有整数,每5个数字占一行,每个数字 ...

  2. Linux->卸载Mysql方法总结

    如何在Linux下卸载MySQL数据库呢? 下面总结.整理了一下Linux平台下卸载MySQL的方法. MySQL的安装主要有三种方式:二进制包安装(Using Generic Binaries).R ...

  3. 免费申请一年版Eset NOD32 Antivirus激活码(无限制)- 已失效

    You will receive a 1-year license of the antivirus software Eset NOD32 (version 6), including all up ...

  4. 中断一个telnet连接

    假如我要telnet一个端口通不通,测试通过之后后出现黑屏的界面 这时候需要按下ctrl+] 组合键 然后输入 quit 即可退出telnet窗口,可以继续测试下一个端口~

  5. Mysql命令insert into:向表中插入数据(记录)

    insert into命令用于向表中插入数据. insert into命令格式:insert into <表名> [(<字段名1>[,..<字段名n > ])] v ...

  6. 深入理解uwsgi和gunicorn网络模型

    前言: 去年10月份建了一个python技术群,到现在为止人数已经涨到700人了.最一开始我经常在群里回应大家的问题,不管是简单还是困难的,我都会根据自己的经验来交流. 让人新奇的是一些初学者关注最多 ...

  7. vue 登录验证引擎

    1.router配置: 路由元信息 const router = new VueRouter({ routes: [ { path: '/foo', component: Foo, children: ...

  8. dubbo支持的注册中心

    dubbo支持的注册中心 Dubbo提供的注册中心有如下几种类型可供选择: Multicast注册中心 Zookeeper注册中心 Redis注册中心 Simple注册中心 ZooKeeper是一个开 ...

  9. Jenkins自动化部署代码

    通过jenkins自动化部署项目代码可以大幅度节省打包上传部署的时间,提高开发测试的工作效率 ========== 完美的分割线 =========== 1.Jenkins是什么 1)Jenkins是 ...

  10. [LeetCode&Python] Problem 806. Number of Lines To Write String

    We are to write the letters of a given string S, from left to right into lines. Each line has maximu ...