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. 从零开始编译属于你的FFmpeg

    一.前提: 编译FFmpeg可以是初学者,尤其是对C语言项目,Linux编译不熟悉的的初学者的一道门槛. 我曾经找过很多博客,文章,有些能编译成功,有些则不能.编译通过,能够运行也是云里雾里的.其实最 ...

  2. java集合框架(一):HashMap

    有大半年没有写博客了,虽然一直有在看书学习,但现在回过来看读书基本都是一种知识“输入”,很多时候是水过无痕.而知识的“输出”会逼着自己去找出没有掌握或者了解不深刻的东西,你要把一个知识点表达出来,自己 ...

  3. OLEDB 参数化查询

    一般情况下,SQL查询是相对固定的,一条语句变化的可能只是条件值,比如之前要求查询二年级学生信息,而后面需要查询三年级的信息,这样的查询一般查询的列不变,后面的条件只有值在变化,针对这种查询可以使用参 ...

  4. csharp: ClientScript.RegisterStartupScript int net4.0

    //彈出提示 ClientScriptManager cs = Page.ClientScript; StringBuilder sb = new StringBuilder(); sb.Append ...

  5. JS基础学习——闭包

    JS基础学习--闭包 什么是闭包 闭包的定义如下,它的意思是闭包使得函数可以记住和访问它的词法范围,即使函数是在它声明的词法范围外执行.更简单来讲,函数为了自己能够正确执行,它对自己的词法范围产生闭包 ...

  6. .NET开源工作流RoadFlow-表单设计-按钮

    在表单中添加一个按钮: 宽度,高度:按钮的宽度和高度. 文本:按钮显示的文本. 事件:点击按钮执行的操作.

  7. Sharepoint 2013企业内容管理学习笔记(一) 半自动化内容管理

    大家好,今天我来与大家分享一个关于sharepoint2013文档管理方面的一个知识,我相信也许早就有人了解并熟知这项技术了,呵呵,众所周知,sharepoint 有一个很亮的功能,什么?没错,就是文 ...

  8. html网页访问WebAPI中的方法遇到的问题

      1.移动端访问远程服务时,建议使用WebAPI 2.用不同浏览器访问WebAPI时返回的文本格式是不同的,Chrome Firefox将在浏览器中以XML形式显示此列表,IE浏览器将获得Json格 ...

  9. input type=number去掉上下箭头

    <input type="number" ...> <style> input::-webkit-outer-spin-button, input::-we ...

  10. Python3基本数据类型(三、列表)

    序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字-它的位置,或索引,第一个索引是0,第二个索引是1,以此类推.Python有6个序列的内置类型,但最常见的是列表和元组.序列都可以进 ...