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. CTF传送门

    https://www.zhihu.com/question/30505597详细见知乎 推荐书: A方向: RE for BeginnersIDA Pro权威指南揭秘家庭路由器0day漏洞挖掘技术自 ...

  2. oracle学习篇一:sqlplus常用命令

    1.程序运行--> cmd --> sqlplus 登陆普通用户:scott/brant;普通管理员用户登陆:system/brant;高级管理员用户登陆:1>先切换其他用户:SQL ...

  3. vuejs+axios发送请求

    Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,官方就不再更新 vue-resource 目前主流的 Vue 项目,都选择 axios  ...

  4. Linux CentOS如何汉化系统

    su root切换为root用户 写入环境变量 echo "export LANG="zh_CN.UTF8"">>/etc/profile sour ...

  5. Android学习——Fragment动态加载

    动态加载原理 利用FragmentManager来添加一套Fragment事务,最后通过commit提交该事务来执行对Fragment的相关操作. FragmentManager fragmentma ...

  6. IFrame安全问题解决办法(跨框架脚本(XFS)漏洞)

    最近项目要交付了,对方安全测试的时候检测出高危险漏洞,由于刚参加工作不久,经验不足,未涉及过此方面的东西.经过一番查询和探索,最终解决了这个问题,记录一下. 发现的漏洞为缺少跨框架脚本保护.跨框架脚本 ...

  7. ORACLE_ALIAS

    Oracle / PLSQL: ALIASES website:https://www.techonthenet.com/oracle/alias.php This Oracle tutorial e ...

  8. June 11th 2017 Week 24th Sunday

    I hope I can find the one who is afraid of losing me. 我希望找到一个担心失去我的人. When I was young, sometimes I ...

  9. selenium:IE浏览器获取cookie提示Could not retrieve cookies

    from selenium import webdriver url = "https://www.baidu.com" dr = webdriver.Ie() dr.get(ur ...

  10. JVM 虚拟机内存深入探究

    [<深入理解java虚拟机>-整理笔记] by  lijun JVM虚拟机内存逻辑模型: 方法区(全局变量 静态数据 常量等) 线程共享 堆栈区(对象实例 数组数据 new generat ...