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. IDEA里面maven菜单解读

  2. redhat下配置SEED DVS6446开发环境2

    ---恢复内容开始--- 1.rpcbind步骤  linux包:portmap安装包 libgssglue-0.1-8.1.el6.i686.rpm libtirpc-0.2.1-1.el6.i68 ...

  3. 使用 Supervsior 守护进程

    概述 一般来说,在终端开启的服务,如果退出终端的话,就会自动关闭服务.这个时候需要守护这个服务的进程. Supervisor 是一个用 Python 写的进程管理工具,可以很方便的用在 UNIX-li ...

  4. Prism学习笔记-模块之间通信的几种方式

    在开发大型复杂系统时,我们通常会按功能将系统分成很多模块,这样模块就可以独立的并行开发.测试.部署.修改.使用Prism框架设计表现层时,我们也会遵循这个原则,按功能相关性将界面划分为多个模块,每个模 ...

  5. 阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解

    @Component spring容器是一个Map结构,是由于key 和vlaue组成的 运行测试 无法运行 出错的原因↓ 第一部是解析配置文件.但是配置文件这里是空的.我们的bean里面什么对象都没 ...

  6. MYSQL连接一段时间不操作后出现异常的解决方案

    最近做的网站使用的是MYSQL数据库 发现 果超过8小时应用程序不去访问数据库,数据库就断掉连接 .这时再次访问就会抛出异常,如下所示: com.mysql.jdbc.exceptions.jdbc4 ...

  7. etcd节点扩容至两个节点

    本篇已经安装了单个etcd,然后进行扩容etcd节点至2个,安装单节点请参照:https://www.cnblogs.com/effortsing/p/10295261.html 实验架构 test1 ...

  8. CSS 易混淆属性

    1. div中 height和line-height的区别 <div style="height:120px;">是用来规定整个div的高度,文字还是默认会在顶端开始向 ...

  9. spring ehcache 缓存框架

    一.简介 Ehcache是一个用Java实现的使用简单,高速,实现线程安全的缓存管理类库,ehcache提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案.同时ehcache ...

  10. Golang基础(5):Go语言反射规则

    Go语言反射规则 - The Laws of Reflection 转:http://my.oschina.net/qbit/blog/213720 原文地址:http://blog.golang.o ...