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:

5 32 321 3214 0229 87

Sample Output:

22932132143287

题目大意:给出几个数,要求求出它们的片段拼接,并且这个数是所有数中最小的。

//一看到就不太明白怎么做,拼接不同总长度也不一定相同,有0开头的,如果放在中间的话就算是中间一位了。没什么思路,考试遇到这个的话会跪。

 代码来自:https://www.liuchuo.net/archives/2303

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
bool cmp0(string a, string b) {//两个相同的数相加,它们的长度肯定是相同的。
return a + b < b + a;
}
string str[];//使用字符串数组!
int main() {
int n;
scanf("%d", &n);
for(int i = ; i < n; i++)
cin >> str[i];//以String作为输入。
sort(str, str + n, cmp0);
string s;
for(int i = ; i < n; i++)
s += str[i];//将字符串拼接。
while(s.length() != && s[] == '')
s.erase(s.begin());//将开头的0除去。
if(s.length() == ) cout << ;
cout << s;
return ;
}

//柳神的代码简直叹为观止。厉害了,学习了。

1.对字符串的处理,关键是这个cmp0函数的使用。

PAT 1038 Recover the Smallest Number[dp][难]的更多相关文章

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

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

  2. PAT 1038. Recover the Smallest Number

    #include <iostream> #include <cstdlib> #include <vector> #include <algorithm> ...

  3. PAT甲1038 Recover the smallest number

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

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

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

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

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

  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

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

  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. ...

随机推荐

  1. asp.net 判断session是否过期

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  2. 如何实现Asp与Asp.Net共享Session

    <iframe align="top" marginwidth="0" marginheight="0" src="http ...

  3. 数据库 数据库SQL语句四

    多表查询 等值连接 --查询员工信息,员工号,姓名,月薪,部门名称 select e.empno,e.ename,d.dname from emp e,dept d where e.deptno=d. ...

  4. 【UVa】Partitioning by Palindromes(dp)

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=sh ...

  5. 常用的jQuery前端技巧收集

    调试时巧用console.log(),这比用alert()方便多了. jquery易错点:元素拼接的时候,元素还未添加到DOM,就用该预添加元素操作. ajax动态获取的数据,还没有装载html元素, ...

  6. KVC/KVO之暴力的KVC

    本章将分为三个部分: KVC是什么 KVC之Set/Get KVC键值路径之Set/Get KVC是什么 KVC,即 NSKeyValueCoding,一个非正式的 Protocol,提供一种机制来间 ...

  7. Linq------错误:EntityType: EntitySet 'Products' is based on type 'Product' that has no keys defined.

    解决方法: [Table("bma_products")] public class Product { //加上[Key]即可 [Key] public int pid{get; ...

  8. iOS开发之--使用storyboard下,tabbar小图标和文字颜色的设置

    在开发项目的时候,如果是使用故事版设计的架构,那么在设置tabbar小图标的时候,可能会出现一点小问题, 成功的设置方法如下: 1.设置seleectedImage和image,其实就是非选中状态的图 ...

  9. 85、android handler的警告Handler Class Should be Static or Leaks Occur

    转载:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1106/1922.html 在使用Handler更新UI的时候,我是这样写 ...

  10. Android 防破解技术简介

    Android 防破解技术简介 这几年随着互联网的不断发展,Android App 也越来越多!但是随之而来的问题也越来越多,这其中比较令人头疼的问题就是:有些不法分子利用反编译技术破解 App,修改 ...