The problem of deletion (the classical problem of greedy law)

Source: Internet
Author: User

Problem Description: Enter a high-precision positive integer n with the keyboard, and the remaining digits after the s digits are removed to form a new positive integer in the left and right order.

Programming given N and S, looking for a scheme to make the remaining numbers oh the smallest number of new numbers.

Thinking analysis: Using the greedy method to approximate the target to delete one of the S characters, the total number of each step is to choose a number of the remaining number of the smallest character delete. Such a greedy choice because removing the S-character's straight solution contains the deletion of a

The optimal solution of the sub-problem of a digital character. Find the descending interval from left to right, and delete the first number. If no descending interval is found, the number at the end is deleted.

1 voidFind_min_integer (vector<int> &nums,intS)2 {3vector<int>result;4 5      if(s>nums.size ())6      {7 nums.clear ();8         return;9      }Ten      //finding recursive sequences from left to right One  A       while(s>0) -      { -         inti; the          for(i=0;(i<nums.size ()) && (nums[i]<=nums[i+1]); i++) -             ; -Nums.erase (i);//Delete the first character of the interval, including the end when the descending interval is not found -s--; +      } -  +       while(Nums.size () >1&&nums[0]==0)//Delete Invalid number of head 0 ANums.erase (0); at}

Greedy Choice Property: The Global optimal solution can be achieved by local optimal selection.

The problem of deletion (the classical problem of greedy law)

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.