Description

Now we have a number, you can swap any two adjacent digits of it, but you can not swap more than K times. Then, what is the largest probable number that we can get after your swapping?

Input

There is an integer T (1 <= T <= 200) in the first line, means there are T test cases in total.

For each test case, there is an integer K (0 <= K < 106) in the first line, which has the same meaning as above. And the number is in the next line. It has at most 1000 digits, and will not start with 0.

There are at most 10 test cases that satisfy the number of digits is larger than 100.

Output

For each test case, you should print the largest probable number that we can get after your swapping.

Sample Input

3
2
1234
4
1234
1
4321

Sample Output

3124
4213
4321

Hint


Submit Page
#include<iostream>
#include<cstdio>
#include<cmath>
#include<set>
#include<string>
#include<algorithm>
using namespace std; int main()
{
ios::sync_with_stdio(false);
int T,k;
string str;
cin>>T;
while(T--)
{
int temp=0;
cin>>k>>str;
while(k)
{
int Max=str[temp]-'0',flag=temp;
int pos=temp+k>str.length() ? str.length()-1 : temp+k;
for(int i=temp+1;i<=pos;i++)
{
if(str[i]-'0'>Max) Max=str[i]-'0',flag=i;
}
k-=flag-temp;
//cout<<flag<<endl; int ff=str[flag]-'0';
for(int i=flag-1;i>=temp;i--)
str[i+1]=str[i];
str[temp]=ff+'0';
temp++;
if(temp>=str.length()) break;
// cout<<k<<endl;
}
cout<<str<<endl;
} return 0;
}
/**********************************************************************
Problem: 1270
User: song_hai_lei
Language: C++
Result: AC
Time:12 ms
Memory:2180 kb
**********************************************************************/


Swap Digits的更多相关文章

  1. CSUOJ 1270 Swap Digits

    Description ) in the first line, which has the same meaning as above. And the number is in the next ...

  2. Maximum Swap LT670

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  3. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

  4. Educational Codeforces Round 75 (Rated for Div. 2) C. Minimize The Integer

    链接: https://codeforces.com/contest/1251/problem/C 题意: You are given a huge integer a consisting of n ...

  5. Codeforce 1251C. Minimize The Integer

    C. Minimize The Integer time limit per test2 seconds memory limit per test256 megabytes inputstandar ...

  6. SZU:A66 Plastic Digits

    Description There is a company that makes plastic digits which are primarily put on the front door o ...

  7. 交换基本数据类型的方法swap,并影响到主方法

    不知道朋友在哪里看到的问题,qq来问我,题目是:在不修改主方法的前提下使用一个方法交换两个int的值,方法如下: public static void main(String[] args) { In ...

  8. [LeetCode] Maximum Swap 最大置换

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  9. [Swift]LeetCode670. 最大交换 | Maximum Swap

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

随机推荐

  1. c#属性(Property)

    属性(Property)是类(class).结构(structure)和接口(interface)的命名(named)成员.类或结构中的成员变量或方法称为 域(Field).属性(Property)是 ...

  2. 《Java多线程面试题》系列-创建线程的三种方法及其区别

    1. 创建线程的三种方法及其区别 1.1 继承Thread类 首先,定义Thread类的子类并重写run()方法: package com.zwwhnly.springbootaction.javab ...

  3. nyoj 96-n-1位数 (strlen, atoi, ceil)

    96-n-1位数 内存限制:64MB 时间限制:3000ms 特判: No 通过数:30 提交数:47 难度:1 题目描述: 已知w是一个大于10但不大于1000000的无符号整数,若w是n(n≥2) ...

  4. NetCore基于EasyNetQ的高级API使用RabbitMq

    一.消息队列 消息队列作为分布式系统中的重要组件,常用的有MSMQ,RabbitMq,Kafa,ActiveMQ,RocketMQ.至于各种消息队列的优缺点比较,在这里就不做扩展了,网上资源很多. 更 ...

  5. 力扣(LeetCode)整数形式的整数加法 个人题解

    对于非负整数 X 而言,X 的数组形式是每位数字按从左到右的顺序形成的数组.例如,如果 X = 1231,那么其数组形式为 [1,2,3,1]. 给定非负整数 X 的数组形式 A,返回整数 X+K 的 ...

  6. node.js传参给PHP失败,headers加上'Content-Length': Buffer.byteLength(content)

    node.js需要传参给PHP,执行计划任务 var events = require('events'); start_cron(,,{"auth":"7wElqW6v ...

  7. 《算法导论中文版》PDF 下载

    电子版仅供预览及学习交流使用,下载后请24小时内删除,支持正版,喜欢的请购买正版书籍 在有关算法的书中,有一些叙述非常严谨,但不够全面:另一些涉及了大量的题材,但又缺乏严谨性.本书将严谨性和全面性融为 ...

  8. java多线程,多线程加锁以及Condition类的使用

    看了网上非常多的运行代码,很多都是重复的再说一件事,可能对于java老鸟来说,理解java的多线程是非常容易的事情,但是对于我这样的菜鸟来说,这个实在有点难,可能是我太菜了,网上重复的陈述对于我理解这 ...

  9. EFK教程(3) - ElasticSearch冷热数据分离

    基于ElasticSearch多实例架构,实现资源合理分配.冷热数据分离 作者:"发颠的小狼",欢迎转载与投稿 目录 ▪ 用途 ▪ 架构 ▪ 192.168.1.51 elasti ...

  10. ArcGIS 切片与矢量图图层顺序问题

    在项目中有个需求:根据图层索引添加图层 看到这个需求一下子想到 map.addLayer(layer,index?) 接口 但是问题出现了,我切片图加载顺序在矢量图之后就不行! map = new M ...