915C Permute Digits

C. Permute Digits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two positive integer numbers a and b. Permute (change order) of the digits of ato construct maximal number not exceeding b. No number in input and/or output can start with the digit 0.

It is allowed to leave a as it is.

Input

The first line contains integer a (1 ≤ a ≤ 1018). The second line contains integer b(1 ≤ b ≤ 1018). Numbers don't have leading zeroes. It is guaranteed that answer exists.

Output

Print the maximum possible number that is a permutation of digits of a and is not greater than b. The answer can't have any leading zeroes. It is guaranteed that the answer exists.

The number in the output should have exactly the same length as number a. It should be a permutation of digits of a.

Examples
input
123
222
output
213
input
3921
10000
output
9321
input
4940
5000
output
4940

题意:给你一个数小于1e18 a和b,输出不大于b的数a,a是经过重排列了的。
题解:由于数很大,一位一位的找判是不行的。最简单的是转换成字符串做,先sort排序,然后交换n阶乘次相(当于排列组合,判断组合出来的a和b的关系,如果a<=b的,就存入c字符组。又继续找。
#include<bits/stdc++.h>
using namespace std;
char a[20],b[20],c[20];
int main()
{
    int m;
    while(cin>>a>>b)
    {
        m=strlen(a);
        sort(a,a+m);
        if(m<strlen(b))
        {
            for(int i=m-1; i>=0; i--)
                cout<<a[i];
            return 0;
        }
        strcpy(c,a);
        for(int i=0; i<m; i++)
        {
            strcpy(a,c);
            for(int j=i; j<m; j++)
            {
                swap(a[i],a[j]);
                if(strcmp(a,b)!=1)
                    strcpy(c,a);
            }
        }
        puts(a);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落凡尘.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值