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

      

  • 相关阅读:
    GUID概念
    某猿的饭局
    SVN切分支步骤
    OSX:设置用户默认浏览器
    值得推荐的android开发框架简单介绍
    用实力让情怀落地!阅兵前线指挥车同款电视TCL&#160;H8800受捧
    Excel查询序列所相应的值-vLoopup函数,求比例分子改变但分母不变
    CSS3制作W3cplus的关注面板
    Spring MVC框架实例
    @property 和@synthesize
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9460749.html
Copyright © 2011-2022 走看看