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;
    }
    

      

  • 相关阅读:
    云计算是什么?它有哪些形式?
    TensorFlow从0到1之浅谈深度学习(5)
    excel如何快速统计出某一分类的最大值?
    Excel怎样根据出生日期,快速计算出其年龄呢?
    Excel只想显示一部分日期,怎样把其余部分隐藏起来?
    Excel数据透视表的日常应用技巧
    人工智能(机器学习)学习之路推荐
    人工智能之常用数据结构与算法(python)
    excel如何快速计算日期对应的生肖?
    excel 如何制作带下拉框的动态折线图表
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9460749.html
Copyright © 2011-2022 走看看