F - Pasha Maximizes

如有错误,欢迎大神指出!!!

题目:

Pasha has a positive integer a without leading zeroes. Today he decided that the number is too small and he should make it larger. Unfortunately, the only operation Pasha can do is to swap two adjacent decimal digits of the integer.

Help Pasha count the maximum number he can get if he has the time to make at most k swaps.

Input

The single line contains two integers a and k (1 ≤ a ≤ 1018; 0 ≤ k ≤ 100).

Output

Print the maximum number that Pasha can get if he makes at most k swaps.

Example
Input
1990 1
Output
9190
Input
300 0
Output
300
Input
1034 2
Output
3104
Input
9090000078001234 6
Output
9907000008001234

题意:最多交换K次,交换只能两两相邻之间交换,获得最大的数。

解题思路:由于数据不大,直接进行暴力搜索。输入是用字符串输入。


ac code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s)  scanf("%s",s)
#define pi1(a)    printf("%d\n",a)
#define pi2(a,b)  printf("%d %d\n",a,b)
#define mset(a,b)   memset(a,b,sizeof(a))
#define forb(i,a,b)   for(int i=a;i<b;i++)
#define ford(i,a,b)   for(int i=a;i<=b;i++)
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define mod 1000000007
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
using namespace std;
char ss[1006];

int main()
{
    int n;
    scanf("%s",ss);
    int k;
    si1(k);
    for(int i=0;i<strlen(ss);i++)
    {
        if(ss[i]!='9')
        {
            char c=ss[i];
            int num=0;
            for(int j=1;j<=k;j++)
            {
                if(ss[j+i]>c)
                {
                    c=ss[i+j];//搜索在这个字符之后更大的字符。
                    num=j;//记录位置
                    if(c=='9')
                        break;
                }
            }
            k=k-num;//维护交换次数
            for(int j=num;j>=1;j--)//交换后的字符串
            {
                char a;
                a=ss[i+j];
                ss[i+j]=ss[i+j-1];
                ss[i+j-1]=a;
            }
            //printf("%s\n",ss);
        }
        if(k<=0)
            break;//交换次数使用完
    }
    printf("%s\n",ss);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值