zoukankan      html  css  js  c++  java
  • CodeForces

    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
    Copy
    123
    222
    output
    Copy
    213
    input
    Copy
    3921
    10000
    output
    Copy
    9321
    input
    Copy
    4940
    5000
    output
    Copy
    4940

    没什么难的,暴力模拟一下,

    如果a的长度小于b,把a数位分离后排序,从大到小输出

    如果长度相等,按数位一位一位去比较,先用与b对应相等的数,并进行标记,没相等对应位然后使用最接近并小于的对应(此时找到最大的a,按匹配b的前面一部分长度输出,剩下的按照a的数位数降序输出),如果没有,再退回去,重新匹配

    (但是我在求数字长度时用lon10(),当数字长度大于15时会出现误差,最后还是看后台样例发现的。。。。。)

    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<algorithm>
    #include<vector>
    #include<map>
    using namespace std;
    #define LL __int64
    
    int ff=-1;
    bool vis[22];
    int len1=0,len2=0;
    void inin1(LL n, int a[])///数位分离
    {
        while(n)
        {
            a[len1++]=n%10;
            n/=10;
        }
    }
    void inin2(LL m, int b[])///数位分离
    {
        while(m)
        {
            b[len2++]=m%10;
            m/=10;
        }
    }
    int pan1(int i, int a[], int b[], int len1)///数位相等
    {
        for(int j=len1-1; j>=0; j--)
        {
            if(b[i]==a[j]&&vis[j])
            {
                vis[j]=false;
                return 1;
            }
        }
        return 0;
    }
    int pan2(int ss, int a[], int len1)///数位小于
    {
        for(int i=len1-1; i>=0; i--)
        {
            if(a[i]<ss&&vis[i])
            {
                ff=i;
                vis[i]=false;
                return 1;
            }
        }
        return 0;
    }
    void pan3(int i, int a[], int b[], int len1)
    {
        for(int j=len1-1; j>=0; j--)
        {
            if(b[i]==a[j]&&vis[j]==false)
            {
                vis[j]=true;
                return ;
            }
        }
    }
    int main()
    {
        memset(vis, true, sizeof(vis));
        LL n, m;
        int a[22], b[22];
        scanf("%I64d%I64d", &n, &m);
        //int len1, len2;
        //len1=(int)log10(n)+1;
        //len2=(int)log10(m)+1;
        inin1(n, a);
        inin2(m, b);
        //printf("%d>>
    ", b[len2-1]);
        sort(a,a+len1);
        //printf("%d %d>>
    ", len1, len2);
        if(len1<len2)
        {
            for(int i=len1-1; i>=0; i--)
            printf("%d", a[i]);
            printf("
    ");
            return 0;
        }
        else
        {
            int i;
            for(i=len2-1; i>=0; i--)
            {
                int p=pan1(i, a, b, len1), q=0;
                //printf("p<<<<<%d
    ", p);
                if(p!=1)
                {
                    q=pan2(b[i], a, len1);
                    //printf("q<<<<<%d
    ", q);
                    while(q!=1)
                    {
                        i++;
                        pan3(i, a, b, len1);
                        //printf("%d :%d>>.
    ",i, q);
                        q=pan2(b[i], a, len1);
                    }
                }
                if(q==1)
                    break;
            }
            if(i==-1)
            {
                for(int k=len2-1; k>i; k--)
                    printf("%d",b[k]);
                printf("
    ");
                return 0;
            }
            for(int k=len2-1; k>i; k--)
                printf("%d",b[k]);
            printf("%d", a[ff]);
            for(int k=len1-1; k>=0; k--)
                if(vis[k])
                printf("%d", a[k]);
            printf("
    ");
    
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    C# Using MySQL
    C++ Asynchronous IO on Windows
    C++ Bind adapter usage
    C# Entity Framework with MSSQL, MYSQL
    Read a file into array for C++/C#
    上下移动 04.16
    盒子模型001基础
    JavaScript&JQ 001_五角星评分
    jQuery EasyUI tree的 使用
    自定义实现URL重写 04.18
  • 原文地址:https://www.cnblogs.com/zhulei2/p/9118791.html
Copyright © 2011-2022 走看看