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. Hadoop源码分析之读文件时NameNode和DataNode的处理过程

    转自: http://blog.csdn.net/workformywork/article/details/21783861 从NameNode节点获取数据块所在节点等信息 客户端在和数据节点建立流 ...

  2. jsp中9个内置对象与servlet对应关系及四个作用域

    参考:  <jsp&servlet学习笔记.第2版.林信良><JSR-245 JavaServer Pages 2.2 Maintenance Release Specifi ...

  3. Hadoop是原Yahoo的Doug Cutting根据Google发布的学术论文研究而来

    Hadoop是原Yahoo的Doug Cutting根据Google发布的学术论文研究而来.Doug Cutting给这个Project起了个名字,就叫Hadoop. Doug Cutting在Clo ...

  4. HttpServlet中,用来处理POST请求的方法是(选择1项)

    HttpServlet中,用来处理POST请求的方法是(选择1项) A. doHead B. doGet C. doPost D. doPut 解答:C

  5. form 回车键(ENTER)

    <form class="clb_yyxx_nr" name="appointment_content" action="" onsu ...

  6. 解决同一页面jQuery多个版本或和其他js库冲突方法

    <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  7. 【Debian】时间设置

    http://blog.linuxphp.org/archives/567/ http://www.dedecms.com/knowledge/servers/linux-bsd/2012/0819/ ...

  8. LeetCode 125 Valid Palindrome(有效回文)(*)

    翻译 给定一个字符串.确定它是否是回文的,仅仅考虑当中的数字和字符并忽略其它. 比如. "A man, a plan, a canal: Panama" 是回文的. "r ...

  9. ViewPageIndicator

    关于我的总结 1.写一个类extend HorizontalScrollView 实现ViewPagerIndicator,这样写index需要自己写有点啰嗦 2.自定义一个类extends Hori ...

  10. Android无线测试之—UiAutomator UiScrollable API介绍六

    向前与向后滚动API 一.向前与向后滚动相关API 返回值 API 描述 boolean scrollBackward(int steps) 自动以步长向后滑动 boolean scrollBackw ...