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.

Example

Input
123
222
Output
213
Input
3921
10000
Output
9321
Input
4940
5000
Output
4940

在调整过程中要判断大小,本来想是降序排序然后交换顺序,但是123和222,如果按照这样就是321,换3和2,就是231,还是大,然后就会换2和1,答案肯定不对,所以换了以后剩下的保持升序最好,也就是先升序排序,然后倒着把大的跟前面换,然后把剩下的升序排序,一直这么进行下去,纸上模拟了一遍可行。

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
char s1[],s2[],s[];
bool cmp(char a,char b)
{
return a > b;
}
int main()
{
int j;
cin>>s1>>s2;
if(strlen(s1) < strlen(s2))
{
sort(s1,s1 + strlen(s1),cmp);
}
else
{
sort(s1,s1 + strlen(s1));
for(int i = ;i < strlen(s1);i ++)
{
strcpy(s,s1);///保存原来的s1,如果交换不成功,还原,继续尝试其他交换
for(j = strlen(s1) - ;j > i;j --)
{
swap(s1[i],s1[j]);
sort(s1 + i + ,s1 + strlen(s1));
if(strcmp(s1,s2) <= )break;
else strcpy(s1,s);
}
}
}
cout<<s1;
}

Permute Digits的更多相关文章

  1. CodeForces-915C Permute Digits

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

  2. Codeforces 915 C. Permute Digits (dfs)

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

  3. cf Permute Digits(dfs)

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

  4. 【CodeForces 915 C】Permute Digits(思维+模拟)

    You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...

  5. Permute Digits 915C

    You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...

  6. CF915C Permute Digits 字符串 贪心

    You are given two positive integer numbers a and b. Permute (change order) of the digits of a to con ...

  7. C. Permute Digits dfs大模拟

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

  8. CF915C Permute Digits

    思路: 从左到右贪心放置数字,要注意判断这个数字能否放置在当前位. 实现: #include <bits/stdc++.h> using namespace std; typedef lo ...

  9. 【Educational Codeforces Round 36 C】 Permute Digits

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] //从大到小枚举第i(1..len1)位 //剩余的数字从小到大排序. //看看组成的数字是不是小于等于b //如果是的话. //说 ...

随机推荐

  1. HSV颜色识别-HSV基本颜色分量范围

    原文地址:https://blog.csdn.net/taily_duan/article/details/51506776 一般对颜色空间的图像进行有效处理都是在HSV空间进行的,然后对于基本色中对 ...

  2. ArcGISDynamicMapServiceLayer 和 ArcGISTiledMapServiceLayer 区别

    ArcGISDynamicMapServiceLayer(动态地图服务)通常用于实时显示经常变化的数据,支持控制单个图层可见性,可动态投影.但缺点是显示效果较差,整个服务出图较慢:ArcGISTile ...

  3. JavaScript Source Maps浅析

    阅读目录 有用的链接 Link: 原文链接 译文开始: 对网站进行性能优化对一个最容易的方法就是把JS和CSS进行打包压缩.但是当你需要调试这些压缩文件中的代码的时候,会发生什么?可能会是一场噩梦.但 ...

  4. centos6.5安装mysql报错

    安装完mysql后,mysql服务无法打开. 报错 查看mysql日志 执行命令:less /var/log/mysqld.log  发现是权限不够,不能创建pid文件.因此改变权限,再次启动服务 问 ...

  5. eureka配置参数

    org.springframework.cloud.netflix.eureka.EurekaClientConfigBean 参数名称 说明 默认值 eureka.client.enabled 用于 ...

  6. 创建可执行bin安装文件

    [应用场景] 简化操作,对于有些安装操作而言,需要包含安装脚本和脚本需要的文件两部分,封装成可执行bin文件之后就只有一个安装包了. 代码保护,在很多情况下,我们并不希望用户可以直接接触到代码部分,这 ...

  7. Oozie 3.3.1安装

    软件安装路径 软件名称 版本 安装路径 jdk 1.6.0_12 /usr/java/jdk1.6.0_12 maven 3.1.0 /usr/local//apache-maven-3.1.0 Oo ...

  8. spring boot-18.使用dubbo发布分布式服务

    我们新建两个项目分别模拟服务的提供者和服务的消费者,spring boot 集成dubbo主要分为以下几个步骤: 1.安装zookeeper 推荐使用docker 安装,使用以下几个命令即可完成 (1 ...

  9. [转帖]yum 下载rpm包 之前不知道具体的存放路径.

    使用Yum下载RPM包-进击的二狗子-51CTO博客 https://www.2cto.com/os/201905/807225.html yumdownloader 命令 yum install y ...

  10. tomcat的相关

    [针对tomcat修改,那么就直接找关于tomcat的相关文件目录进行修改即可] 1.对tomcat进行相关的操作,启动tomcat时,让其不要出现tomcat主页,与之相反的让其出现404的界面! ...