Prove safety offer: the minimum number of aligned array (java)

Title: enter a positive integer array, the array arranged in which all of a number of digital mosaic, all numbers can print a mosaic out of. 3,32,321 input array} {e.g., print the minimum number of three digits can be arranged to 321,323.

    The most direct approach to this topic should be the first find of this array full array of all numbers, and then stitching together each permutation, finally obtain the minimum value of the numbers line up. Seeking array arrangement 28 and the surface is very similar questions. According to permutations and combinations of the just, n numbers a total of n! Arrangement, we'll look at a faster algorithm.

    This question actually hope we can find a collation array according to the rules of this sort can be arranged after a minimum number. To determine the ordering rules, it is necessary to compare two numbers, that is, given two numbers m and n, we need to identify a rule which should determine m and n are standing in the front, rather than just comparing the two digital values ​​which greater.

    The requirements of the subject, two numbers m and n can splicing and said digital mn nm. If mn <nm, then we should print out Mn, which is to be shot in front of the N m, then to maintain mn <we define the m (isSmall) n, an integer array is first converted to a string nm, defined isSmall rule: i.e., the first bit <m, n of the first, if they are equal, then the next <n, m of a comparison, if mn = nm, m equal to n.

public void printMin(int[] arr){  
        int[] clone = arr.clone();  
        qsort(clone,0,clone.length-1);  
        for(int i : clone)  
            System.out.print(i);  
    }  
    //规则+快排  
    public void qsort(int[] arr,int left,int right){  
        if(left < right){  
            int main_number = arr[right];  
            int small_cur = left;  
            for(int j = left;j<right;j++){  
                if(isSmall(String.valueOf(arr[j]),String.valueOf(main_number))){  
                    int temp = arr[j];  
                    arr[j] = arr[small_cur];  
                    arr[small_cur] = temp;  
                    small_cur++;  
                }  
            }  
            arr[right]= arr[small_cur];  
            arr[small_cur] = main_number;  
            qsort(arr,0,small_cur-1);  
            qsort(arr,small_cur+1,right);  
        }  
    }  
    public boolean isSmall(String m,String n){  
        String left = m+n;  
        String right = n+m;  
        boolean result = false;  
        for(int i = 0;i<left.length();i++){  
            if(left.charAt(i)<right.charAt(i))  
                return true;  
            else  
                if(left.charAt(i)>right.charAt(i))  
                    return false;  
        }  
        return result;  
    }  

    Proof of the effectiveness of the rules: reflexive, symmetric, transfer

    Obviously aa = aa, meet reflexive;

    If a (isSmall) b, the ab <ba, so ba> ab, so b a, satisfy symmetry (isSmall!);

    If a (isSmall) b, the ab <ba. Assume that a and b l bits decimal expressed and m bits, so ab <ba-> a / (10 ^ l-1) <b / (10 ^ m-1 )

    If b (isSmall) c, then the bc <cb. Suppose c decimal representation has n bits, then b / (10 ^ m-1 ) <c / (10 ^ n-1), of a (isSmall) b introduced a / (10 ^ l-1) <b / (10 ^ m-1), and therefore a / (10 ^ n-1 ) <c / (10 ^ l-1), so a (isSmall) c, transitive .

   Next, according to the number n proved after Collation foregoing, represents A1A2 ... An, remains minimal. We use contradiction, assume that such is not the smallest number of splicing, i.e., there are two x and y (0 <x <y < n), after the exchange of the number x and number y, A1A2 ... Ay .. .Ax ... An <A1A2 ... Ax ... Ay ... An. Since A1A2 ... Ay ... Ax ... An is a good sort accordance with the rules, so there are Ax (isSmall) Ax + 1 ( isSmall) Ay-1 (isSmall) Ay. Ay We have been exchanged and the front of the digital exchange until the exchange and Ax so far, so there A1A2 ... Ax ... Ay-1 ... Ay ... An <A1A2 ... AyAx ... Ay-1 ... An. Similarly we have been take the digital exchange Ax behind it, Ay-1 up to and until the exchange. Thus A1A2 ... AyAx ... Ay-1 ... An <A1A2 ... AyAx + 1 ... Ay-1Ax ... An. Therefore A1A2 ... Ax ... Ay ... An < A1A2 ... Ay ... Ax ... An, and contradicts the assumption. Therefore, the assumption does not hold, our algorithm is correct.


Published 118 original articles · won praise 35 · views 120 000 +

Guess you like

Origin blog.csdn.net/abc7845129630/article/details/52739249