CSUOJ 1270 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
#include<stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
#include<iostream>
typedef long long ll;
using namespace std;
int T, s, len;
char ch[1010];
int main()
{
cin >> T;
while (T--)
{
cin >> s >> ch;
len = strlen(ch);
for (int i = 0; i < len; i++)
{
if (s <= 0)break;
char max = '0';
int key;
for (int j = i + 1; j < len && j <= i + s; j++)//找到能移动的最大位数
{
if (max < ch[j])
{
max = ch[j];
key = j;
}
}
if (max > ch[i])
{
for (int j = key; j > i; j--)
ch[j] = ch[j - 1];
ch[i] = max, s =s-( key - i);
}
}
cout << ch << endl;
}
return 0;
}
/**********************************************************************
Problem: 1270
User: leo6033
Language: C++
Result: AC
Time:12 ms
Memory:2024 kb
**********************************************************************/
CSUOJ 1270 Swap Digits的更多相关文章
- 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 ...
随机推荐
- Java并发编程原理与实战四十三:CAS ---- ABA问题
CAS(Compare And Swap)导致的ABA问题 问题描述 多线程情况下,每个线程使用CAS操作欲将数据A修改成B,当然我们只希望只有一个线程能够正确的修改数据,并且只修改一次.当并发的时候 ...
- Java写的数据库连接池
原文地址: http://lgscofield.iteye.com/blog/1820521 import java.sql.*; import java.util.Enumeration; impo ...
- (CoreText框架)NSAttributedString 2
CHENYILONG Blog (CoreText框架)NSAttributedString 2 Fullscreen © chenyilong. Powered by Postach.io Blog
- 让PHPCms内容页支持JavaScript_
在PHPCms内容页中,出于完全考虑,默认是禁止JavaScript脚本的,所以我们在添加文章时,虽然加入了js代码,但实际上并没有起作用,而是以文本形式显示.如果要让内容页支持JavaScript, ...
- InnoDB 引擎独立表空间
InnoDB 引擎独立表空间 使用过MySQL的同学,刚开始接触最多的莫过于MyISAM表引擎了,这种引擎的数据库会分别创建三个文件:表结构.表索引.表数据空间.我们可以将某个数据库目录直接迁移到 ...
- 如何清理休眠文件(hiberfil.sys)
如果使用了休眠功能,那么打开系统盘就会有一个很大(5.36G)的hiberfil.sys文件,它是将用户正在运行的程序,保存在这里,再启动系统就很快了.如要清理它(不用休眠功能,或者临时腾出空间),可 ...
- hdu 1004 Let the Balloon Rise(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...
- 利用JS验证查询参数-选择月份后必须选择年份
js代码: function queryAgentInfo(){ // 标记 var flag=false; //遍历所有月份 $(".month").each(function( ...
- idea开发工具下载安装教程
我用这款工具主要用于java开发 在安装这个工具之前需要配置java的环境 java的jdk环境配置 jdk:1.8 jdk官网下载链接 --->点我 进入之后,下拉 选择 jdk1.8版本 ...
- 数据结构之线性表(python版)
数据结构之线性表(python版) 单链表 1.1 定义表节点 # 定义表节点 class LNode(): def __init__(self,elem,next = None): self.el ...