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.

Examples

input
123
222
output
213
input
3921
10000
output
9321
input
4940
5000
output
4940

题解:

dfs,深搜,需要注意的是如果我们找到一个小于当前B中的值时,后面的只需要从小到大排列就好了

#include <bits/stdc++.h>
using namespace std;
char a[20],b[20];
int num[11],x[200],w[200],lb,la;
long long MAX=0,B;
inline long long MAx(long long z,long long w)
{
if(z>w) return z;
return w;
}
bool cmp(char a,char b)
{
return a>b;
} void dfs(long long ans,int len,int k,int v)
{
if(len==la&&len<=lb&&ans<=B) {
MAX=MAx(ans,MAX);
ans=0;
return;
}
for (int i = 9; i >=0 ; i--) {
if (num[i])
{
if(v==0&&i==w[k])
{
num[i]--;
ans=ans*10+i;
dfs(ans,len+1,k+1,0);
ans-=i;
ans/=10;
num[i]++;
} else if(i<w[k])
{
num[i]--;
ans=ans*10+i;
for (int j = 9; j >=0 ; j--) {
for (int z = 0; z <num[j] ; ++z) {
ans=ans*10+j;
}
}
if(ans<B)
{
MAX=MAx(MAX,ans);
}
ans=0;
}
}
}
}
int main()
{
scanf("%s",a);
scanf("%s",b);
la=(int)strlen(a);
lb=(int)strlen(b);
for (int i = 0; i <la ; ++i) {
num[a[i]-'0']++;
x[i]=a[i]-'0';
}
for (int i = 0; i<lb; i++) {
w[i]=b[i]-'0';
B=B*10+w[i];
}
if(la<lb)
{
sort(a,a+la,cmp);
for (int i = 0; i <la ; ++i) {
printf("%c",a[i]);
}
printf("\n");
return 0;
}
dfs(0,0,0,0);
printf("%lld",MAX);
return 0;
}

  

Permute Digits 915C的更多相关文章

  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. CF915C Permute Digits 字符串 贪心

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

  6. 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. cookie 的 写入,读取, 删除

    页面跳转,cookie存储参数 1,设置cookie function setCookie(name,value) { var Days = 30; var exp = new Date(); exp ...

  2. jQuery异步提交时显示正在加载的提示

    $(element).ajaxStart(function() { loader.show(); }).ajaxStop(function() { loader.hide(); }).ajaxErro ...

  3. Struts2_带参数的结果集

    页面请求: <a href="user/user?type=1">传参数</a> action: public Integer type; public S ...

  4. C#队列Queue,利用队列处理订单

    一.什么是队列 队列(Queue)代表了一个先进先出的对象集合.当您需要对各项进行先进先出的访问时,则使用队列.当您在列表中添加一项,称为入队,当您从列表中移除一项时,称为出队. 这是摘抄网上的.做了 ...

  5. python3线程介绍01(如何启动和调用线程)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import osimport time,randomimport threading # 1-进程说明# 进程 ...

  6. iBrand 教程:Git 软件安装过程截图

    下载 教程中使用的相关软件下载网盘: https://pan.baidu.com/s/1bqVD5MJ 密码:4lku 安装 请右键 以管理员身份运行 进行软件安装,安装过程如下: 使用 安装完成后, ...

  7. 怎样学习webpack - 走心分享

    很多朋友可能都知道webpack,也见过webpack的代码,但是不明白里面一坨一坨的东西是什么意思,到底有什么用处!我们每个人学习一个新东西可能都会有这个过程,但是我个人觉得webpack可能是最混 ...

  8. [转]Android中Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)

    今天学习了Spinner组件,使用Spinner相当于从下拉列表中选择项目,下面演示一下Spinner的使用(分别使用ArrayAdapter和自定义Adapter实现) (一):使用ArrayAda ...

  9. 浏览器中使用calc不识别

    在使用css3中的calc运算函数时,发现浏览器不识别,当时代码是这样的 width:calc(100%-50px); 经过查询官网原来发现这里有个需要注意的地方就是在进行加减运算的时候,必须在运算符 ...

  10. tensorflow pb ckpt pbtxt

    ckpt存储参数值 pbtxt存储网络图 pb既存储参数值又存储网络图