Swap Digits
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的更多相关文章
- CSUOJ 1270 Swap Digits
Description ) in the first line, which has the same meaning as above. And the number is in the next ...
- Maximum Swap LT670
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
- 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 ...
- Codeforce 1251C. Minimize The Integer
C. Minimize The Integer time limit per test2 seconds memory limit per test256 megabytes inputstandar ...
- SZU:A66 Plastic Digits
Description There is a company that makes plastic digits which are primarily put on the front door o ...
- 交换基本数据类型的方法swap,并影响到主方法
不知道朋友在哪里看到的问题,qq来问我,题目是:在不修改主方法的前提下使用一个方法交换两个int的值,方法如下: public static void main(String[] args) { In ...
- [LeetCode] Maximum Swap 最大置换
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...
- [Swift]LeetCode670. 最大交换 | Maximum Swap
Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...
随机推荐
- jstl-将List中的数据展示到表格中
功能: 使用jstl将List中的数据动态展示到Jsp表格中,并实现隔行换色功能. 效果图: Jsp代码: <%@ page import="java.util.ArrayList&q ...
- springboot使用dubbo和zookeeper
2019-11-17 yls 创建服务接口模块 接口工程只提供接口,不提供实现,在后面的提供者和消费者中使用 在使用接口的模块中只需要写具体实现类,避免了在每个模块中重复编写接口 在接口中引入依赖包 ...
- linux中dd相关命令骚操作
一.dd如何快速将磁盘写满 方法一: dd if=/dev/zero of=/tmp/file bs=1G count=10 # 参数解释 1. if=文件名:输入文件名,缺省为标准输入.即指定源文件 ...
- Worktile正式发布全新研发产品!
经过近一年时间的打磨,Worktile研发产品正式发布啦!和以往Worktile版本升级不同的是,这是一个全新的产品形态,目前已上线 Agile(敏捷开发).Pipe(持续交付).Testhub(测试 ...
- SpringBoot 源码解析 (六)----- Spring Boot的核心能力 - 内置Servlet容器源码分析(Tomcat)
Spring Boot默认使用Tomcat作为嵌入式的Servlet容器,只要引入了spring-boot-start-web依赖,则默认是用Tomcat作为Servlet容器: <depend ...
- Serlvet之cookie和session学习
HTTP 协议 Web通信需要一种语言,就像中国人讲中文,欧美说英文,Web使用的HTTP协议,也叫超文本协议. 使用HTTP协议的人分为两类:客户端和服务端.请求资源的角色是客户端,提供资源的是服务 ...
- shell——数组
默认从0开始索引:也可以单独(像字典一样)pid[35420]=httpd -k ssl, 只能是一维的 bash4.0增加了关联数组 数组赋值: declare -a myarray声明数组 一次一 ...
- ACL2019: 《GraphRel: Modeling Text as Relational Graphs for Joint Entity and Relation Extraction》源码解析
论文地址:<GraphRel: Modeling Text as Relational Graphs for Joint Entity and Relation Extraction> G ...
- vim编辑中断后,重新编辑的警告删除
使用vim 编辑,遇到突然中断,比如ssh远程时断网了. 如果再次ssh连接,重新vim 打开之前在编辑的文件,会有类似如下的警告 这是因为vim会在被编辑的目录下新建一个名为.filename.sw ...
- Redis Geo HyperLogLog类型介绍
Geo类型 Redis3.2.0版本推出 可以将用户给定的地理位置信息存储起来,并对这些信息进行操作 GEOADD key longitude latitude member [longitude ...