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
123222
Output
213
Input
392110000
Output
9321
Input
49405000
Output
4940

题意:

给定两个数字a和b,重新构建a,使a≤b的同时,a是该情况下的最大值。

思路:

(1) 先判断a与b的长度,若a的长度<b的长度,那么直接输出a的降序。

(2) 否则,令a升序排列。L指向a的最高位,R指向a的最低位。依次交换a[L]和a[R](将最大数与最小数进行交换),最高位后面的数按照升序排序。如果a<b,则将a[L]变成a[R],否则不能交换。

例:a=78135,b=55634    (a按升序排序为:13578)
<1> 81357->71358->51378             (确定第五位是5)
<2> 58137->57138->53178             (确定第四位是3)
<3> 53817                                        (确定第三位是1)
<4> 53871                (确定第二、第一位分别为7、1)

#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
int main()
{
    string a,b,t;
    cin>>a>>b;
    int lena=a.length(),lenb=b.length();
    sort(a.begin(),a.end());
    if(lena<lenb)
        reverse(a.begin(),a.end());
    else
    {
        int L,R;
        ;L<lena;L++)
        {
            R=lena-;
            t=a;
            while(R>L)
            {
                swap(a[L],a[R--]);
                sort(a.begin()+L+,a.end());
                if(a>b)a=t;
                else break;
            }
        }
    }
    cout<<a<<endl;
    ;
} 

【CodeForces 915 C】Permute Digits(思维+模拟)的更多相关文章

  1. Codeforces 915 C. Permute Digits (dfs)

    题目链接:Permute Digits 题意: 给出了两个数字a,b(<=1e18),保证a,b都不带前缀0.用a的字符重组一个数字使这个值最大且小于b.(保证这个值存在) 题解: 这题遇到了不 ...

  2. CF--思维练习--CodeForces - 216C - Hiring Staff (思维+模拟)

    ACM思维题训练集合 A new Berland businessman Vitaly is going to open a household appliances' store. All he's ...

  3. 【Codeforces】879D. Teams Formation 思维+模拟

    题意 给定$n$个数,重复拼接$m$次,相邻$k$个重复的可消除,问最后序列中有多少个数 首先可以发现当$k>=n$时,如果要使$n$个数可以被消除,那么$n$个数必须一样,否则$n$个数不能被 ...

  4. CodeForces-915C Permute Digits

    C. Permute Digits time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. cf Permute Digits(dfs)

    C. Permute Digits You are given two positive integer numbers a and b. Permute (change order) of the ...

  6. Codeforces Round #706 (Div. 2)B. Max and Mex __ 思维, 模拟

    传送门 https://codeforces.com/contest/1496/problem/B 题目 Example input 5 4 1 0 1 3 4 3 1 0 1 4 3 0 0 1 4 ...

  7. C. Permute Digits dfs大模拟

    http://codeforces.com/contest/915/problem/C 这题麻烦在前导0可以直接删除,比如 1001 100 应该输出11就好 我的做法是用dfs,每一位每一位的比较. ...

  8. Codeforces 758D:Ability To Convert(思维+模拟)

    http://codeforces.com/problemset/problem/758/D 题意:给出一个进制数n,还有一个数k表示在n进制下的值,求将这个数转为十进制最小可以是多少. 思路:模拟着 ...

  9. Codeforces 758C:Unfair Poll(思维+模拟)

    http://codeforces.com/problemset/problem/758/C 题意:教室里有n列m排,老师上课点名从第一列第一排开始往后点,直到点到第一列第m排,就从第二列第一排开始点 ...

随机推荐

  1. C# Attribute应用:类签名

    在应用别人接口的时候,总是要用签名,很是不理解签名这是怎么知道做的.通过对Attribute的学习了解.大体可以用Attribute来做签名应用. 具体过程如下: 首先我们要先定义一个类,该类继承At ...

  2. Java笔记之Scanner先读取一个数字,在读取一行字符串方法分析

    问题:大家在学习Java读取数据的时候一般都是使用Scanner方法读取数据,但是其中有一个小问题大家可能不知道, 就是我们在使用scanner的时候如果你先读取一个数字,在读取一行带有空格的字符串, ...

  3. sass的安装

    sass的安装 1.当ruby安装完成后,在自己电脑开始菜单下面找到ruby的控制台"cmd"; 2.然后直接在命令行中输入 gem install sass 3.在安装过程中 可 ...

  4. 函数进阶3 —— 生成器、yield from

    今天我们在进一步了解一下,生成器. ①: def func(): print('这是函数func') return '函数func' func() 结果是 这是函数func ②: def func1( ...

  5. html5 嵌入元素 img map areaiframe embed meter object meter

    <img src="路径">            src 路径可以是img.jpg 也可以是 绝对和相对路径+img.jpg <img src="路径 ...

  6. 常用css属性记录

    CSS常用属性: 字体属性:(font)大小 font-size: x-large;(特大) xx-small;(极小) 一般中文用不到,只要用数值就可以,单位:PX.PD样式 font-style: ...

  7. Office 365系列(三) -Office 365 Pro plus 安装

    这一篇博客主要是说Office 365 Pro plus安装. 1. 当登陆到Office 365以后,点击右边链接“下载软件” 2. 安装最新Office 软件 3. 采用点对点安装,当安装成功以后 ...

  8. Fragstats:使用R软件读取frag78b.asc文件

    Fragstats中,有一个用于熟悉Fragstats软件的demon,在tutorial中的1种,有reg78b.asc文件,其文件内容是包含了山歌图像的行数和列数以及分辨率大小等基本信息. 采用R ...

  9. 用C#实现工资计算公式动态编写

    1,工资计算公式 每一个企业都一定会用到工资计算,发工资是一件非常神圣的事情,而计算工资就是一项非常重要的工作.Excel有非常强大的公式功能,帮助了很多财务人员计算工资,但如果企业的人数比较多,而且 ...

  10. 【NLP_Stanford课堂】词形规范化

    一.为什么要规范化 在做信息检索的时候,一般都是精确匹配,如果不做规范化,难以做查询,比如用U.S.A去检索文本,结果文本里实际上存的是USA,那么实际上应该能查到的结果查不到了. 所以需要对所有内容 ...