• Codeforce915C


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

    题意:给两个数a和b,可以打乱a每位数的顺序组成一个新的数c,
    求满足c<=b的最大,保证结果一定存在。


    分析:先求出a的每位数字,然后排个序,再dfs。

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int A[100],B[100],C[100],alen,blen;
    int vis[30],ans[30],K=-1;
    int cmp(int x,int y)
    {return x>y;}
    
    int f(int a[],long long x)
    {
        int len=0;
        while(x)
        {
            a[len++]=x%10;
            x/=10;
        }
        return len;
    }
    
    int dfs(int k)//k为当前构造到第k位数 
    {
        if(k==alen) return 1;
        int last=-1;//前一个数 
        for(int i=0;i<alen;i++)
        {    
            if(last==A[i]) continue;
            if(vis[i]==0&&A[i]<=B[k])
            {
                if(k==0&&A[i]==0) continue;
                vis[i]=1;
                ans[k]=A[i];
                last=A[i];
                if(A[i]<B[k]) 
                {
                    K=k;
                    return 1;
                }
                if(dfs(k+1)) return 1;
                vis[i]=0;
            }
        }
        return 0;
    }
    
    int main()
    {
        long long a,b;
        scanf("%lld%lld",&a,&b);
        alen=f(A,a);
        blen=f(C,b);
        for(int i=0;i<blen;i++)
        B[i]=C[blen-i-1]; 
        sort(A,A+alen,cmp);//排序 
        if(alen<blen)
        {
            for(int i=0;i<alen;i++)
                printf("%d",A[i]);
            printf("
    ");
        }
        else//alen==blen
        {
            dfs(0);
            if(K>=0)
            {
                for(int i=0;i<=K;i++)
                printf("%d",ans[i]);
                for(int i=0;i<alen;i++)
                if(!vis[i]) printf("%d",A[i]);
            }
            else
            {
                for(int i=0;i<alen;i++)
                printf("%d",ans[i]);
            }
            printf("
    ");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    osg::PagedLOD example
    osg::NodeVisitor example
    osg::NodeVisitor
    osg::NodeVisitor osg3.4.0
    Visual studio 正在从以下位置加载符号:Microsoft符号服务器 尝试取消禁用后续符号加载
    osgViewer::Viewer::Windows
    Inventor2018专业版软件安装激活教程
    osg osgUtil::LineSegmentIntersector
    Civil 3D百度云地址
    osg define shape(create box)
  • 原文地址:https://www.cnblogs.com/ACRykl/p/8311644.html
Copyright © 2020-2023  润新知