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. python - 函数any() 和 all() 的区别

    转自:https://www.cnblogs.com/nulige/p/6128816.html any()与all()函数的区别: any是任意,而all是全部. 版本:该函数适用于2.5以上版本, ...

  2. Jmeter (四)聚合报告详解

  3. 安装MySQL Enterprise Monitor

    MySQL Enterprise Monitor是专门为MySQL数据库而设计的一款企业级监控,能非常好地与MySQL各方面特性相结合,包括:MySQL的关键性能指标.主机.磁 盘.备份.新特性相关以 ...

  4. python中利用队列asyncio.Queue进行通讯详解

    python中利用队列asyncio.Queue进行通讯详解 本文主要给大家介绍了关于python用队列asyncio.Queue通讯的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细 ...

  5. char* a = "abc" 和 char a[] = "abc" 之间的区别

    char* a = "abc"; 声明了一个字符类型的指针a,并为它赋值初始值为"abc",a的值是字符串"abc"的首地址[第一个字符的地 ...

  6. python线程池--threadpool

    在爬虫时,有时候解析获得了很多图片或视频地址时,如果一个个下载完成再去下载另一个,这样执行效率太慢了,此时就可用到线程池threadpool,使用基本步骤如下: 1.定于任务函数 2.创建线程池,定义 ...

  7. git上传项目到github教程

    1 本地下载git 2 进到你项目所在地 3 git init(初始化git仓) 4 git add .(git add <file>将文件添加到git) 5 git commit -m ...

  8. mysql——单表查询——分组查询——示例

    一.基本查询语句 select的基本语法格式如下: select 属性列表 from 表名和视图列表 [ where 条件表达式1 ] [ group by 属性名1 [ having 条件表达式2 ...

  9. idea 如何运行maven项目

    1:run→Edit configurations 2:配置tomcat,左边如果没有tomcat server的话,点击 “+”,选择tomcat  server→local,在右边server选项 ...

  10. vue--键盘修饰符以及自定义键盘修饰符

    键盘修饰符以及自定义键盘修饰符 1.vue键盘修饰符[了解即可] ​地址:https://cn.vuejs.org/v2/guide/events.html#%E6%8C%89%E9%94%AE%E4 ...