Min number greedy, put the smallest numbers in the top

Source: Internet
Author: User

Topic linksproblem 2111 Min numberaccept:499 submit:949
Time limit:1000 mSec Memory limit:32768 KB problem Description

Now is given one non-negative integer n in 10-base notation, it'll only contain digits (' 0 '-' 9 '). You is allowed to choose 2 integers i and j, such that:i!=j, 1≤i<j≤|n|, here |n| means the length of n ' s 10-base notation. Then we can swap n[i] and n[j].

For example, n=9012, we choose I=1, j=3, then we swap n[1] and n[3], then we get 1092, which is smaller than the original N.

Now you are allowed to operate at the most M times, so what's the smallest number can get after the the operation (s)?

Problem, leading zero is not allowed!

Input

The first line of the input contains an integer T (t≤100), indicating the number of the test cases.

Then T-cases, for any case, only 2 integers n and M (0≤n<10^1000, 0≤m≤100)

Outputfor Each test case, the output of the minimum number we can get after no more than M operations. Sample Input39012 09012 19012 2 Sample Output901210921029


Each operation can be changed to two digits on the number

Up to M operations

What is the minimum number that can be obtained after the operation?


Greed, the first bit can not be zero, and then find the smallest number constantly placed in the front of the number of digits.

9,012 the first position can not be 0 so the rest of the smallest 1 in the first 1092; the second is already the smallest so the third place 9 is replaced by the remaining smallest 2 to 1029;





Min number greedy, put the smallest numbers in the top

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.