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 (<=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;
}


转载于:https://www.cnblogs.com/aiwz/p/6154158.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值