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;
}

最新文章

  1. js 函数
  2. 我的权限系统设计实现MVC4 + WebAPI + EasyUI + Knockout(一)
  3. DEV 等待窗口
  4. s3c2440液晶屏驱动 (非内核自带) linux-4.1.24
  5. UITextView 文本垂直居中
  6. OS X Framework Library not loaded: &#39;Image not found&#39;的解决办法
  7. 重构第30天 尽快返回 (Return ASAP)
  8. ruby on rails 2.3+的版本不再支持cgi
  9. eclipse指定启动的jdk
  10. nginx模块开发(18)—日志分析
  11. JUC学习笔记--从阿里Java开发手册学习线程池的正确创建方法
  12. 在ZendStudio中增加新的php模板
  13. UITableView移除某行的分割线和让分割线宽度为cell的宽度
  14. Spring 事务管理笔记
  15. Unity插件 - MeshEditor(八)模型镜像特效
  16. Android UI技巧(一)——Android中伸缩自如的9patch图片切法,没有美工自给自足
  17. 3、设置jsp上的类容自动更新
  18. Core中使用Hangfire
  19. Sublime Text 3 Settings
  20. Tiny4412在Ubuntu下给MiniTools添加快捷方式

热门文章

  1. OpenCV学习笔记(4)——图像上的算术运算
  2. IDEA项目追踪快捷键
  3. 代码实现:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
  4. cocoapods [!] Unable to find a pod with name, author, summary, or description matching `xx`
  5. (WPF)360安全卫士界面设计
  6. django 通过模型类操作数据表(基础增删改查)
  7. python3实现自动化框架robotframework(最新)
  8. 关于glog使用中遇到的问题
  9. Keytool生成证书
  10. 【Qt开发】QTableWidget设置根据内容调整列宽和行高