zoukankan      html  css  js  c++  java
  • Permute Digits 915C

    You are given two positive integer numbers a and b. Permute (change order) of the digits of a to 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

    题解:

    dfs,深搜,需要注意的是如果我们找到一个小于当前B中的值时,后面的只需要从小到大排列就好了

    #include <bits/stdc++.h>
    using namespace std;
    char a[20],b[20];
    int num[11],x[200],w[200],lb,la;
    long long MAX=0,B;
    inline long long MAx(long long z,long long w)
    {
        if(z>w) return z;
        return w;
    }
    bool cmp(char a,char b)
    {
        return a>b;
    }
    
    void dfs(long long ans,int len,int k,int v)
    {
        if(len==la&&len<=lb&&ans<=B) {
            MAX=MAx(ans,MAX);
            ans=0;
            return;
        }
        for (int i = 9; i >=0 ; i--) {
            if (num[i])
            {
                if(v==0&&i==w[k])
                {
                    num[i]--;
                    ans=ans*10+i;
                    dfs(ans,len+1,k+1,0);
                    ans-=i;
                    ans/=10;
                    num[i]++;
                } else if(i<w[k])
                {
                    num[i]--;
                    ans=ans*10+i;
                    for (int j = 9; j >=0 ; j--) {
                        for (int z = 0; z <num[j] ; ++z) {
                            ans=ans*10+j;
                        }
                    }
                    if(ans<B)
                    {
                        MAX=MAx(MAX,ans);
                    }
                    ans=0;
                }
            }
        }
    }
    int main()
    {
        scanf("%s",a);
        scanf("%s",b);
        la=(int)strlen(a);
        lb=(int)strlen(b);
        for (int i = 0; i <la ; ++i) {
            num[a[i]-'0']++;
            x[i]=a[i]-'0';
        }
        for (int i = 0;  i<lb; i++) {
            w[i]=b[i]-'0';
            B=B*10+w[i];
        }
        if(la<lb)
        {
            sort(a,a+la,cmp);
            for (int i = 0; i <la ; ++i) {
                printf("%c",a[i]);
            }
            printf("
    ");
            return 0;
        }
        dfs(0,0,0,0);
        printf("%lld",MAX);
        return 0;
    }
    

      

  • 相关阅读:
    【NodeJS】---express配置ejs mongoose route等
    【CSS3】---层模型position之fixed固定定位、absolute绝对定位和relative相对定位
    【CSS3】---:before :after生成内容
    px转rem的填坑之路
    markdown编写文件目录结构
    js reduce数组转对象
    处理Promise.reject()
    js事件循环
    为什么[] == false 为true
    为什么不建议用var
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9460749.html
Copyright © 2011-2022 走看看