Introduction to the algorithm----greedy algorithm, delete the number of K, so that the remaining number of the smallest

Source: Internet
Author: User

First issue:

1 n-bit positive integer A, delete the K-bit in it, get a new positive integer b, design a greedy algorithm, and get the smallest b for a given and K;

I. my idea: First Look at the example: a=5476579228; remove 4 bits, then the number of digits n=10,k=4, the minimum required number B is n-k=6 bit;

1, first find the highest number, because it is 6 digits, so the top bit can not be taken on the last 5 bit (because the relative order of the number can not be changed, assuming that if the last five bits of the 5th digit of 7, then the B can not be 6-bit, Up to 4 bits of 79228) It's important to understand this! So the question becomes from 1th to K+1 (n (n-k-1)) Take the minimum value, why is k+1, can think for yourself. In this case, it becomes

/54765/79228 Select the smallest number in the middle of the slash!

2, so according to the sequence number 1, to obtain the minimum 4, then the highest level has been determined to be 4; then the 6-bit number becomes 5 bit to determine, the same upper reasoning process, the second high cannot take any number in the 4 bit , because the first bit determines the second position on the 4, So the number before 4 can not be taken (because the relative order of the numbers can not change), so it becomes 54/7657/9228, the minimum value of the number within the slash, gets 5. 3, then take the third digit 54765//228, the third take 7;547657//28 Fourth take 2;54765792/2/8, fifth digit can only be 2; sixth digit is 8. ; The resulting figure is 457228;

4, continue to think of a problem if the input integer A is 3346579228, the same n=10,k=4; what kind of problems do you encounter? The same upper process first step:/33465/79228, at this time the interval has two the same minimum value 3, which value should be used? Is it obvious that the first 3,why should be selected? Because imagine if you take the second, the second time you can only select the minimum value in 4657, and take the first 3, you can get 3 in 34657.

5, this algorithm idea is probably this, the algorithm concrete how to realize it? First of all we need to know the program body to Loop n-k times, because only then we can cycle out the smallest number , followed by how to take the minimum value within the interval. I use the loop through the entire interval to achieve the minimum value, the most critical is to determine the starting position of the interval, the position of the first loop is best determined to be 1, the end position is k+1, the start of the second cycle is the first time to take the minimum value of the coordinate value plus 1, the end position is K + 2; then continue to record the minimum value of the coordinate value to calculate the next start position.

6. This is my code:

#include <iostream>
using namespace Std;
int main () {
int num,k,n=0,a[100],x;
cin>>num>>k;
X=num;

Calculate Length (a);
while (x>0) {
X=X/10;
n++;
}
a[0]=0;

//the typed number into the defined array;
 for (int i=n;i>0;i--)
 {
   int s=num%10;
  a[i]=s;
  num=num/10;
 }
 int j,p=0,minn[n-k+1],min,q;
 minn[0]=0;
 for (int i=1;i<=n-k;i++)//n-k cycles;
 {
  min=a[p+1];

Define q record coordinates; min[] record the minimum value taken each time
q=p+1;
for (j=p+1;j<=k+i;j++) {
if (a[j]<min)
{
MIN=A[J];
Q=j;
}
}
p=q;
Minn[i]=min;
}
for (int i=1;i<=n-k;i++) {
cout<<minn[i];
}
return 0;
}

Introduction to the algorithm----greedy algorithm, delete the number of K, so that the remaining number of the smallest

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.