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#】3.算法温故而知新
    【C#】2.算法温故而知新
    【C#】1.算法温故而知新
    【C#】SQL数据库助手类2.0(自用)
    【Javascript Demo】JS获取当前对象大小以及屏幕分辨率等
    【Android 基础】Android中全屏或者取消标题栏
    【ASP.NET 问题】System.InvalidOperationException: 对象的当前状态使该操作无效 【大量表单数据提交】错误解决
    【CSS】颜色码对照表
    【Ext.Net学习笔记】07:后续
    【Ext.Net学习笔记】06:Ext.Net GridPanel的用法(GridPanel 折叠/展开行、GridPanel Selection、 可编辑的GridPanel)
  • 原文地址:https://www.cnblogs.com/zhulei2/p/9118791.html
Copyright © 2011-2022 走看看