Greedy algorithm - delete a question

(1) Description of the problem: Given n-bit positive integer a, which remove any k <= n digits after the rest of the figures are arranged according to the original order to form a positive integer. For a given positive integer and n is a positive integer k, and an algorithm designed to identify the remaining digits of the minimum number number of new puncturing scheme;

After the minimum number for a given positive integer a, calculates numbers obtained by deleting k;: (2) Algorithm Design

(3) algorithm idea: to delete deleted, leaving to stay; a given number a, for every number on it, using the greedy algorithm, local optimum way, leaving the minimum number of bits; premise: during the removal process, anxious to ensure that the numbers on the minimum position, but also to ensure that the principles enough deleted when the number of deleted;

(4) core code:

 

public class DeleteData {

    /**
     * Given positive integer
     */
    private static Integer num;

    /**
     * Data storage array
     */
    private static Integer[] data;

    /**
     * The total number of deleted
     */
    private static Integer total;

    /**
     * Remove logo
     */
    private static Boolean[] deleteFlag;

    /**
     * Initialization data
     */
    private static void initData() {
        Integer index = 0;
        Scanner input = new Scanner(System.in);

        System.out.println ( "Please enter a given positive integer:" );
        String numStr = input.nextLine();
        num = Integer.valueOf(numStr);

        System.out.println ( "Please enter the deletion of the number of positive integers:" );
        total = input.nextInt();
        data = new Integer[numStr.length()];
        deleteFlag = new Boolean[numStr.length()];

        // split num elements stored in the data array [178543 -> data = {3, 4, 5, 8, 7, 1} } 
        the while (! Num = 0 ) {
            data[index] = num % 10;
            a = a / 10 ;
            deleteFlag[index] = false;
            index++;
        }
        // data 数组逆置
        for (int i = 0, j = data.length - 1; i < j; i++, j--) {
            Integer temp = data[i];
            data[i] = data[j];
            data[j] = temp;
        }
    }

    /**
     * Delete
     * / 
    Private  static  void The deleteData () {
         int Find = 0;        // initial start looking from the subscript is 0 
        the while (Total = 0! {)
             Int min = 10 ;
             int index = -1 ;
             // data.length - I expressed as a subscript i behind the remaining figures, is greater than the total number of digits equal to find, delete enough to ensure that 
            for ( int i = Find; i <data.length && data.length - i> = total; i ++ ) {
                 IF ( Data [I] < min) {
                    min = data [i];
                    index = i;
                }
            }
            if (index >= 0) {
                deleteFlag [index] = to true ;    // left to stay in the digital 
            }
            the Find = index + 1;            // next start looking for a directly from the index, because the current position is already minimal, does not have to be repeated to find 
            total-- ;
        }
    }

    /**
     * Output
     */
    private static void print() {
        System.out.println ( "after deleting the minimum value is:" );
         for ( int I = 0; I <deleteFlag.length; I ++ ) {
             IF (deleteFlag [I]) {
                System.out.print(data[i]);
            }
        }
    }

    public  static  void main (String [] args) {
         // initialization data 
        initData ();

        // delete 
        deleteData ();

        // output 
        Print ();
    }

}

 

(5) Input Output:

Please enter a given positive integer:
 178 543
Please enter the number of positive integers deleted:
2
After deleting a minimum of:
13

(6) Summary: The idea of ​​the algorithm to delete a number of issues of great reaction greedy, delete each cycle number, are looking for local optimal solution, after the last finish removed, leaving is the optimal solution;

 

Guess you like

Origin www.cnblogs.com/blogtech/p/12294316.html