"Sword refers to offer" face question 33-Arrange the array into the smallest number _ face questions

Source: Internet
Author: User
Topic Description:

Enter an array of positive integers to concatenate all the numbers in the array into a number, printing the smallest of all the numbers that can be spliced. And the proof of the algorithm is given. For example, the input array, {3,32,321}, prints out the smallest number that 3 digits can emit is 321323. Topic Analysis:

The simplest way to do this is to get the smallest number in the array by arranging the numbers. This is not not not, but the complexity of time space is relatively large.
The bottom one starts with a simple example:
If the given array is {3,32}, the number of integers that can be spliced is two: 332 and 323,323 is less than 332, so we put 32 at the front of 3, so the numbers are relatively small. Just imagine, if we can sort a set of data by the way we compare them, and then print out the array in turn, the result is satisfying the result. So what is the comparison of the above methods.
It's not a simple comparison (3<32,3 should be in front of 32). Not necessarily), 3 and 32 for stitching, 32 and 3 stitching, finished after comparing the size of two results, we are in ascending order.
Why is this arrangement to get the right results?

Proof: (Refer to the link at the end of the document, thank you for the author ~)
You can use the disprove method, assuming that the smallest number is xxxxxx, and that there are at least a pair of strings that satisfy this relationship: a > B, but in the number of components A is in front of b According to the position of A and B, divided into three kinds of circumstances:
(1) xxxxab, with BA instead of AB can get XXXXBA, this number is less than xxxxab, with the assumption of contradictions. As a result, the above hypothetical relationship does not exist in the smallest number of rows.
(2) abxxxx, with a BA instead of AB can get baxxxx, this number is less than abxxxx, with the assumption of contradictions. As a result, the above hypothetical relationship does not exist in the smallest number of rows.
(3) AXXXXB, this step proves to be a bit of a hassle. The middle part can be regarded as a whole ayb, then ay < Ya,yb < by IS established. Ay and by are represented as 10-digit digits, the following are the relationships, where the number of a,y,b digits is n,m,k.
Relationship 1:ay < ya => A * 10^m + y < y * 10^n + a => A * 10^m-a < Y * 10^n-y => A (10^m-1)/(10^n- 1) < y
Relationship 2:yb < by => y * 10^k + b < b * 10^m + y => y * 10^k-y < b * 10^m-b => y < b ( 10^M-1)/(10^k-1)
Relationship 3:a (10^m-1)/(10^n-1) < Y < b (10^m-1)/(10^k-1) =>/(10^n-1) < b/(10^k -1) => A*10^k-a < b * 10^n-b =>a*10^k + b < b * 10^n + a => a < b
This contradicts the assumption of a > B. As a result, the above hypothetical relationship does not exist in the smallest number of rows.

The title requires that each number is a positive integer, but the 2-digit concatenation may be greater than the maximum range of integers, so this is also a problem involving a large number of data. As for large data problems, we typically do this by converting numbers into strings, which are still the case here. Implementation code:

#include <iostream>
using namespace std;
#include <string>
#include <algorithm>
bool Compare (const string& str1,const string& str2 )
{
    string s1 = str1+str2;
    string s2 = str2+str1;
    Return s1<s2;
}
void getmin (int arr[],int n)
{
    string* pArr = new string[n];//defines a string array, storing the corresponding string for each number for
    (int i = 0; i < N ++i)
    {
       char buffer[10];
       _itoa (arr[i],buffer,10);
       Parr[i] = buffer;
    Sort (parr,parr+n,compare);
    for (int i = 0; i < n; ++i)
    {
       cout<<parr[i];
    }
    cout<<endl;
}
int main ()
{
    //int arr[] = {32,321,1};
    int arr[] = {3,32,321};
    Getmin (arr,sizeof (arr)/sizeof (arr[0));
    System ("pause");
    return 0;
}

In order to solve the problem of large data, this is implemented using C + + string, but also can be implemented with the C language pointer, here only gives a realization ~

Reference Document: http://blog.csdn.net/cxllyg/article/details/7659525

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.