Num 27:nyoj:0448 looking for maximum number [greedy]

Source: Internet
Author: User



A classic greedy question: title:


Find the maximum number of time limits: +Ms | Memory Limit:65535KB Difficulty:2
Describe

Delete the M numbers in the integer n so that the remaining digits are the largest of the new numbers in the original order,

For example, when n=92081346718538,m=10, the new maximum number is 9888.

Input
The first line enters a positive integer t, which indicates that there is a T group of test data
One row for each set of test data, with two numbers per row n,m (n may be a large integer, but the number of bits does not exceed 100 bits, and the number of digits of the first non-0,m less than the integer n is guaranteed)
Output
The output of each set of test data is one row, the maximum new number of the output remaining number in the original order
Sample input
292081346718538 101008908 5
Sample output
988898

Topic Analysis: Delete a certain number of numbers from a series of values, make the remaining number the largest;

Problem Solving Ideas:

first find the maximum value from s[0]-s[m] and record the next ID.
The second time, from the position of id+1, find the maximum value between s[id+1]-s[m+1] and record the position.
By this method, the end of the string is always found, that is, m==lens-1.

Code implementation:

1. Integers of more than 100 digits, implying that we want to use a character array to store;

2. The first number must be the largest number in front of the data string (in fact, each number from the back is the largest of the small range that starts back from this number);

AC Code:

#include <stdio.h> #include <string.h>char a[110];int main () {int t;scanf ("%d", &t), while (t--) {int Len, I,m,p;char ch;scanf ("%s%d", &a,&m); Len=strlen (a);p =0;while (M<len) {ch=a[p];for (i=p;i<=m;i++) if (A[i] >a[p]) {ch=a[i];p =i;} printf ("%c", ch); m++;p + +;} printf ("\ n");} return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Num 27:nyoj:0448 looking for maximum number [greedy]

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.