题目例如以下:

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 (<=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到9号桶。先依照最高位把数字分别装入桶内,然后针对不同的桶,依照从高位优先排序,当中数字小的相应的字符串小,假设位数不同而且前面长的字符串和短的字符串前面全然一致,则看长字符串后面的部分和桶序号哪个大,比桶序号大的说明这个数应该往后放,由于桶内的全部数字都是以桶序号开头。假设把这样一个数字放到前面,则会出现新拼接的字符串命名能够以桶序号开头却以一个比桶序号大的数开头,详细实现为先按上面的规则定义字符串比較函数。然后对每一个桶进行插入排序。须要注意的问题是第一个数的选取。应该选取桶0中除去前导0后数字最小的,把它抛开,再对其它数进行上面的排序,最后从桶0開始输出到最后一个桶。我后来实现了这个算法,可是有一些BUG没有解决,仅仅能得到20分。

后来通过网上查阅资料发现我把这个题想复杂了,事实上仅仅须要比較字符串然后排序就可以。对于两个字符串a和b,比較ab和ba的大小,为了让数字最小,要让最小的字符串在前面,也就是ab < ba是我们所期望的,因此仅仅须要把全部字符串依照这个规则排序就可以。

须要注意的是,输出时第一个数不能有前导0,假设全部数字都是0。我们仅仅能输出一个0。

为了这两个需求,我使用了stringstream把字符串转为数字。

对于第一个数字,仅仅须要直接转为数字输出就可以。

为了推断是否所有数字都是0,找到最大的字符串。也就是排序后的最后一个,看它转为数字是否是0,假设是,最大的都是0,说明所有都是0,这时候直接输出一个0然后返回。

#include <iostream>
#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
#include <sstream> using namespace std; int compare(string a, string b){
string ab = a + b;
string ba = b + a;
return ab < ba;
} int main(){ int N;
vector<string> nums;
string buffer;
cin >> N;
for(int i = 0; i < N; i++){
cin >> buffer;
nums.push_back(buffer);
}
sort(nums.begin(),nums.end(),compare);
stringstream ss;
int num; // 排除全0情况。先找最大的字符串看是否为0
ss << nums[nums.size()-1];
ss >> num;
if(num == 0){
cout << 0 << endl;
return 0;
}
// 注意清除原来的输入
ss.clear(); ss << nums[0];
ss >> num;
cout << num;
for(int i = 1; i < nums.size(); i++){
cout << nums[i];
}
cout << endl;
}

1038. Recover the Smallest Number (30) - 字符串排序的更多相关文章

  1. 1038. Recover the Smallest Number (30)

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

  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 (30)

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. hdu 4975 A simple Gaussian elimination problem.(网络流,推断矩阵是否存在)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4975 Problem Description Dragon is studying math. One ...

  2. 网页制作之html基础学习6-CSS浏览器兼容问题

    初学html和css时,每天切图,总会遇到很多浏览器兼容性问题.最近一直关注移动平台开发,就html和css来说,不用考虑那么多浏览器兼容性问题.到现在,以至于很多浏览器兼容性几乎忘光了.今天把以前总 ...

  3. 高级UIKit-07(AVAudioPlayer)

    [day09-1-AVAudioPlayer]:播放音乐案例 实现多媒体需要准备以下两点: 需要引入一个框架AVFoundation.framework 然后引入#import <AVFound ...

  4. cocos2d-x游戏开发系列教程-中国象棋01-工程文件概述

    上一篇博文我们看到了象棋的效果图,这一张我们来看象棋代码的整体概述 让我们先对整个代码框架有个了解. 主目录: 主目录包含内容如上图: classes目录:业务代码 proj.win32:包括main ...

  5. 机器时代的中国字幕(Automata.2014.720p.WEB-DL.DD5.1.H264-RARBG.srt)

    看字幕.再也看不下去.自己翻译的位 评价的探讨 1 00:01:58,452 --> 00:02:02,088 人工增雨 期限为32分钟16第二 2 00:02:02,089 --> 00 ...

  6. 安卓面试精华(Activity部分)

    过几天小弟要去面试了,当然免不了要好好复习下功课,其实很多东西也不是特别清楚,今天都当作一个回顾和巩固,希望我的这篇文章能对即将去找工作的同学有所帮助. 1. Q:什么是activity? 虽然这个问 ...

  7. QT update和repaint的区别

    void QWidget::repaint ( int x, int y, int w, int h, bool erase = TRUE ) [槽] 通过立即调用paintEvent()来直接重新绘 ...

  8. SEO分享:我为什么会有这么多的优质外链资源?

    前面小浪发了一篇文章" [完整版]我是怎样3个月把800指数的词做上首页的.",非常多人看了之后都表示非常佩服.顽强的运行力.确实SEO就是要顽强的运行力,也有人说吹牛吧,一天50 ...

  9. Cocos2d-x教程(30)-3.x版本号物理引擎的使用

    转载时请注明原文出处 : http://blog.csdn.net/u012945598/article/details/38417333 在Cocos2d-x 2.x的版本号中,开发人员能够直接使用 ...

  10. IOS开发之UIView总结

    如果想调用某个类的某个方法可以写成这样,这个方法来自NSObject类 performSelector: performSelector:withObject: performSelector:wit ...