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. leetcode 1两数之和

    使用哈希的方法:先将nums哈希表化,再遍历nums,寻找-nums[i]如果存在则为题目所求 class Solution { public: vector<int> twoSum(ve ...

  2. Ubuntu下查找nginx日志

    使用awk检测nginx日志, 按小时计数 awk '{split($4,array,"[");if(array[2]>="29/May/2016:00:00:26 ...

  3. win10 点击开始按钮无反应

    本人亲身经历 由于安装软件时需要注册表权限,在一顿猛如虎的操作下,将注册表中 HKEY_CURRENT_USER 的权限出问题.而导致无法打开 开始菜单 ----------------以下是本人为了 ...

  4. RAC FAILover详解(转载)

    Oracle  RAC 同时具备HA(High Availiablity) 和LB(LoadBalance). 而其高可用性的基础就是Failover(故障转移). 它指集群中任何一个节点的故障都不会 ...

  5. postMessage——解决跨域、跨窗口消息传递

    参考资料1:[http://www.cnblogs.com/dolphinX/p/3464056.html] 参考资料2:[https://developer.mozilla.org/en-US/do ...

  6. Redis 入门 3.2.4 命令拾遗

    Redis 入门 3.2 字符串类型 3.2.4 命令拾遗 1. 增加指定的整数 INCRBY key increment   INCRBY命令与INCR命令基本一样,只不过前者可以通过increme ...

  7. 清晰理解redux中的

    首先需要明白 Redux 的单一状态树的概念,所谓的单一状态树,就是指“所有的 state 都以一个对象树的形式储存在一个单一的 store 中.” 比如我们有这么一个状态树(或者你叫它状态对象也行) ...

  8. 什么是SQL注入以及mybatis中#{}为什么能防止SQL注入而${}为什么不能防止SQL注入

    1.什么是SQL注入 答:SQL注入是通过把SQL命令插入到web表单提交或通过页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL指令. 注入攻击的本质是把用户输入的数据当做代码执行. 举例如: ...

  9. A Dangerous Maze (期望值)

    https://vjudge.net/problem/LightOJ-1027?tdsourcetag=s_pctim_aiomsg [被满为姐姐碾压 降智打击题]

  10. 洛谷 P3368 树状数组 题解

    题面 本题随便看两眼就知道该题满足了优美的查分性质: 对于在区间[x,y]内操作时,应该将查分数组的第x项和第y+1项进行相反操作: 询问答案时,问第i个数的值就是查分数组的前i项和: 暴力+玄学卡常 ...