1038. Recover the smallest number (30)-string sort

Source: Internet
Author: User

The topics are as follows:

Given a collection of number segments, you is supposed to recover the smallest number from them. For example, given {321, 3214, 0229,}, 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 the one test case. Each case gives a positive an integer n (<=10000) followed by N number segments. Each segment contains a non-negative an integer of no more than 8 digits. All the numbers in a line is separated by a space.

Output Specification:

For each test case, print the smallest number on one line. Do not output leading zeros.

Sample Input:
5 32 321 3214 0229 87
Sample Output:
22932132143287

Initially see this topic, I first think of is a bucket sort, set 0 to 9th barrels, first put the number in the bucket according to the highest position, and then for different barrels, according to the order from the high priority, where the number of small corresponding string small, if the number of different and the front long string and the short string before the exact same, Then look at the long string behind the part and the bucket ordinal which is larger than the number of barrels is larger than the description of the numbers should be put back, because all the numbers in the bucket is the beginning of the bucket number, if you put such a number in front, then a new concatenation of the string name can start with a bucket ordinal number of large numbers, The implementation is to first define the string comparison function by the rules above, and then insert the order for each bucket. Attention to the problem is the first number of the selection, you should choose to remove the leading 0 in bucket 0 number of the smallest, throw it away, and then the other number of the above, and finally from the bucket 0 start output to the last bucket. I later implemented this algorithm, but there are some bugs that are not solved and only get 20 points .


Later through the online access to data found that I want to complicate this problem, in fact, only need to compare strings and then sort, for two strings A and B, compare the size of AB and BA, in order to make the smallest number, to let the smallest string in front, that is, AB < BA is what we expect, So you just need to sort all the strings according to this rule.

It should be noted that the first number of outputs cannot have a leading 0, and if all the numbers are 0, we can only output one 0.

For these two requirements, I used StringStream to convert the string to a number.

For the first number, just switch directly to the digital output.

In order to determine whether all the numbers are 0, find the largest string, that is, the last one after the order, see if it is converted to a number 0, if it is, the largest is 0, indicating that all are 0, this time directly output a 0 and then return.

#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;    To exclude all 0 cases, first look for the largest string to see if it is 0    SS << nums[nums.size ()-1];    SS >> Num;    if (num = = 0) {        cout << 0 << Endl;        return 0;    }    Note clear the original input    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)-string sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.