-----deletion problem of greedy algorithm

Source: Internet
Author: User

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

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.