Find maximum number (nyoj448) (greedy)

Source: Internet
Author: User

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
Source
The
sixth session of Itat semi-finals B Vol. 2 adaptation
Uploaded by

Acm_ Zhao Minhao

Idea: If the maximum number, the number of m+1~len-1 need to be retained first, so that end=m,
Find the largest number from the 0~end. Write down the subscript t, output the maximum number, end++,
Then find the maximum number, output, and so on from the T+1~end, and the maximum!

#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;char s[102];int main () {int i,j,m,t,k,len,test,end,ans;scanf ("%d", &test), while (test--) {scanf ("%s%d", s,&m); Len=strlen (s); end=m; T=0;for (i=0,k=0;i<=end;) {    ans=s[i];for (j=i+1;j<=end;j++)     {    if (S[j]>ans)    {    ans=s[j ];    t=j;}    } end++;k++;i=t+1;printf ("%c", S[t]), T++;if (k==len-m)//t control the number of bits of output, the end of the flag. Break    ;} printf ("\ n");} return 0;}


Find maximum number (nyoj448) (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.