The array is ranked as the smallest number/1038. Recover the smallest number

Source: Internet
Author: User

The title description enters an array of positive integers, stitching up all the numbers in the array into a number, and printing the smallest of all the numbers that can be stitched together. For example, enter the array {3,32,321}, then print out the minimum number that these three numbers can be ranked as 321323.

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

1 classSolution2 {3      Public:        4         stringPrintminnumber (vector<int>num)5         {6             stringAns ="";7             intLen =num.size ();8             if(len==0)9                 returnans;Tenvector<string>nums (len); One              for(inti =0; i<len; i++) ANums[i] =to_string (Num[i]); - sort (Nums.begin (), Nums.end (), CMP); -              for(inti =0; i<len; i++) theans+=Nums[i]; -             returnans; -         } -         Static intcmpConst string&AMP;S1,Const string&S2) +         { -             returns1+s2<s2+S1; +         } A};

The array is ranked as the smallest number/1038. Recover the smallest number

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.