First, the problem description
Given n-bit integer A, after removing any of these k<=n digits, the remaining numbers are arranged in the original order to form a new positive integer.
If you enter a positive integer: 178543;
Delete 4 of them
Gets: 13
Second, the solution of the idea-greedy algorithm
Here's the idea of the previous error:
Find the smallest number in the number and make a new positive integer n-k;
But soon there was a problem, though each time it was the smallest number in every position of the integer, the relative relationship of the position was ignored, as in the following example:
An integer that is entered: 178906; 6-digit integer
Delete 4 of them;
According to this idea, that is to choose 6-4=2 a minimum number, namely 0 and 1, according to the number of the original order, the obtained is 10;
But in fact, it should be 06, or 6.
So change the train of thought, call "the recent descent point" priority.
Take advantage of the steepest descent point, where the first element is found each time it is greater than the next element. As the above example, the first one to delete is 9, because 9>0;
The resulting integer is 17806, the second deletion is 8, because 8>0, the resulting integer is 1706, the third one is 7, because 7>0, the resulting integer is 106;
The fourth deletion is 1, because 1>0, gets 06, is the correct answer.
Third, program design
(1) Similarly, the program that gives the wrong design idea:
(2) The correct design idea of the procedure:
-----deletion problem of greedy algorithm