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

题意:

给定n个数,要求任意组合使得组合得到的数最小。

思路:

之前做过一道类似的。这道题给的样例好啊。

表面上看好像是应该将输入的数字存成字符,按照字典序从小到大输出。但是观察样例会发现并不是这样的。

因为单个的字典序最小并不代表组合了之后的字典序最小。

应该要写一个cmp, 用的是组合之后字典序最小的排前面

还有要注意都是0的时候要记得最后输出一个0

  1. #include <iostream>
  2. #include <set>
  3. #include <cmath>
  4. #include <stdio.h>
  5. #include <cstring>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <queue>
  9. #include <map>
  10. using namespace std;
  11. typedef long long LL;
  12. #define inf 0x7f7f7f7f
  13.  
  14. const int maxn = 1e4 + ;
  15. string strnum[maxn];
  16. int n;
  17.  
  18. bool cmp(string a, string b)
  19. {
  20. return a + b < b + a;
  21. }
  22.  
  23. int main()
  24. {
  25. scanf("%d", &n);
  26. for(int i = ; i < n; i++){
  27. cin>>strnum[i];
  28. }
  29. //while(cin>>strnum[n++]);
  30. sort(strnum, strnum + n, cmp);
  31. bool flag = true;
  32. for(int i = ; i < n; i++){
  33. int l = strnum[i].length();
  34. for(int j = ; j < l; j++){
  35. if(flag && strnum[i][j] == '')continue;
  36. if(flag && strnum[i][j] != ''){
  37. flag = false;
  38. }
  39. printf("%c", strnum[i][j]);
  40. }
  41. }
  42. if(flag){
  43. printf("");
  44. }
  45. printf("\n");
  46. return ;
  47. }

PAT甲1038 Recover the smallest number的更多相关文章

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

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

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

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

  3. PAT 甲级 1038 Recover the Smallest Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 Given a collection of ...

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

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

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

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

  6. 1038 Recover the Smallest Number (30 分)

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

  7. 1038. Recover the Smallest Number (30)

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

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

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

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

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

随机推荐

  1. Floyd算法解说

    開始知道Floyd算法是在<大话数据结构>这本书的无向带权图求最短路径看到的, 可是第一次没怎么看懂,所以就不看了,后来又看了两遍还是没明确,我以为是我理解能力有问题 后来从百度百科上看了 ...

  2. 产品半夜发现bug让程序员加班,程序员应如何回应?

    群友半夜2点被产品叫起来改东西,然后他打车去公司加班,群友愤慨,特为此创作一个小段子 产品:XXX出bug了你赶紧看看 修复一下 开发:我宿舍没电脑 没网络啊 产品:你看看周围有没有网吧远程一下 或者 ...

  3. Mybatis数据库连接报错:对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾

    Mybatis数据库连接报错:对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾 ============================== 蕃薯耀 ...

  4. Struts2开发模式漏洞

    当Struts2中的devMode模式设置为true时,存在严重远程代码执行漏洞.如果WEB服务以最高权限运行时,可远程执行任意命令,包括远程控制服务器. 如果为受影响的版本,建议修改配置文件stru ...

  5. mongo数据库查询结果不包括_id字段方法

    db.GPRS_PRODUCT_HIS_FEE.find({"条件字段" : "412171211145135"},{_id:0}) db.GPRS_PRODU ...

  6. Kali linux 试用:dnsenum

    dnsenum的目的是尽可能收集一个域的信息,它能够通过谷歌或者字典件猜测可能存在的域名,以及对一个网段进行反向查询.它可以查询网站的主机地址信息.域名服务器.mx record(函件交换记录),在域 ...

  7. 执行Batch批处理遇到的问题

    1.务必关掉自动提交 增强执行效率 conn.setAutoCommit(false); 2.executeBatch失效问题 <1>务必将语句pstmt = conn.prepareSt ...

  8. WP8.1学习系列(第十章)——中心控件Hub设计指南

    Windows Phone 应用商店应用中的中心控件指南   在本文中 说明 示例 用法指南 设计指南 相关主题 重要的 API Hub (XAML) HubSection (XAML) 说明 中心控 ...

  9. 【十大算法实现之KNN】KNN算法实例(含测试数据和源码)

    KNN算法基本的思路是比较好理解的,今天根据它的特点写了一个实例,我会把所有的数据和代码都写在下面供大家参考,不足之处,请指正.谢谢! update:工程代码全部在本页面中,测试数据已丢失,建议去UC ...

  10. springboot---->springboot中的格式化(一)

    这里面我们简单的学习一下springboot中关于数据格式化的使用.我以为你不是个好人,没想到你连个坏人都不是. springboot中的格式化 我们的测试环境是springboot,一个将字符串格式 ...