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. 深入理解计算机系统 第八章 异常控制流 Part2 第二遍

    第二遍读这本书,每周花两到三小时时间,能读多少读多少(这次看了第 508~530 页,共 23 页) 第一遍对应笔记链接 https://www.cnblogs.com/stone94/p/10206 ...

  2. markdown文档

      标题 #加空格# 加粗 *加粗* 斜体 **斜体** 斜体加粗 ***斜体加粗*** 删除线 ~~删除线~~~ 引用 >引用 分割线 --- 超链接[题目](网址) 列表 -加空格 列表内容 ...

  3. fpm打包神奇rpm包升级python2.7.16

    fpm打包神器参考文档:https://www.cnblogs.com/flintlovesam/p/6594635.html FPM的安装:安装ruby环境和gem命令: yum -y instal ...

  4. 你能说说Java中Comparable和Comparator的区别吗

    之前面试中被问到这个问题,当时不屑(会)回答,下来特意查了查,整理如下. Java 中为我们提供了两种比较机制:Comparable 和 Comparator,二者都是用来实现对象的比较.排序. 下面 ...

  5. 关于@Autowired 与@Resource的

    @Autowired注解是spring自己定义的,@Resource是由JSR-250规范定义的注解. @Resource的作用相当于@Autowired,只不过@Autowired按byType自动 ...

  6. java高级——反射

    慕课网<反射——Java高级开发必须懂的>听课笔记 一.class类的使用 class ClassDemo { public static void main(String[] args) ...

  7. volatile变量能保证线程安全性吗?为什么?

    在谈及线程安全时,常会说到一个变量——volatile.在<Java并发编程实战>一书中是这么定义volatile的——Java语言提供了一种稍弱的同步机制,即volatile变量,用来确 ...

  8. 力扣(LeetCode)旋转字符串 个人题解

    给定两个字符串, A 和 B. A 的旋转操作就是将 A 最左边的字符移动到最右边. 例如, 若 A = 'abcde',在移动一次之后结果就是'bcdea' .如果在若干次旋转操作之后,A 能变成B ...

  9. python:调用bash

    利用os模块 python调用Shell脚本,有三种方法: os.system(cmd)返回值是脚本的退出状态码 os.popen(cmd)返回值是脚本执行过程中的输出内容 commands.gets ...

  10. scrapy介绍及使用

    scrapy的流程 其流程可以描述如下: 调度器把requests-->引擎-->下载中间件--->下载器 下载器发送请求,获取响应---->下载中间件---->引擎-- ...