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. mongodb定时删除数据(索引删除)

    一 简介:本文介绍创建自动删除数据的TTL索引 二 目的 定时删除数据三 创建方法   db.collection.createIndex(keys, options)   options:   ex ...

  2. [笔记] HOW2J.CN网站记录的java笔记_第四部分_HTML

    1.写一个显示Hello World的网页, <html> <body> <p>Hello World</p> </body> </h ...

  3. nyoj 1022 合纵连横 (并查集<节点删除>)

    合纵连横 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 乱世天下,诸侯割据.每个诸侯王都有一片自己的领土.但是不是所有的诸侯王都是安分守己的,实力强大的诸侯国会设法 ...

  4. hdu 1087 Super Jumping! Jumping! Jumping!(动态规划DP)

    Super Jumping! Jumping! Jumping!Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  5. nyoj 113-字符串替换 (python replace, try ... except)

    113-字符串替换 内存限制:64MB 时间限制:3000ms 特判: No 通过数:31 提交数:71 难度:2 题目描述: 编写一个程序实现将字符串中的所有"you"替换成&q ...

  6. Project Euler 62: Cubic permutations

    立方数\(41063625 (345^3)\)的各位数重新排列形成另外两个立方数\(6623104 (384^3)\)和\(66430125 (405^3)\).事实上,\(41063625\)是满足 ...

  7. Centos7安装redis5.0.7

    1. 安装依赖包 yum install -y gcc gcc-c++ 2. 下载最新版redis安装包并解压安装 cd /usr/local/src wget http://download.red ...

  8. 缓冲&缓存&对象池概念的理解

    一).缓冲 作用:缓解程序上下层之间的性能差异. 1).当上层组件的性能优于下层组件时加入缓冲机制可以减少上层组件对下 层组件的等待时间. 2).上层组件不需要等待下层组件接收全部数据,即可返回操作, ...

  9. vue cli3.0 封装组件全局引入js文件并发布到npm

    首先用 vue create创建一个项目 当前的项目目录是这样的: 首先需要创建一个 packages 目录,用来存放组件 然后将 src 目录改为 examples 用作示例 二.修改配置 启动项目 ...

  10. 都是为了生活组——‘’都是为了吃饭”微信小程序评价

    基于NABCD评论作品,及改进建议 1.根据NABCD评论,作品的选题 N(Need,需求) 纠结症是目前在年轻人身上普遍存在着的问题,食堂食物众多,每次在吃饭前都要纠结好久,大大浪费了时间,还容易产 ...