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. css3之移动平台资源

    随着移动端越来越普及,前端技术也是百花齐放,但目前移动平台的技术已经趋向于成熟,记得刚实习的时候就是接触的移动端,但现在2年多来,期间遇到了很多莫名其妙的问题,见证了手机用户量的突飞猛进,兴喜的是更多 ...

  2. 【JavaScript】JavaScript赋值语句中的逻辑与&&和逻辑或||

    在其他语言中,我们往往看到逻辑符号出现在判断语句当中,如 if(a||b){} 但在一些js相关的面试题或者书中,我们有时会看到逻辑与&&和逻辑或||符号出现在赋值语句或者返回语句中, ...

  3. [RabbitMQ]Windows环境下rabbitmqclt(Command Line Tools)出现Erlang distribution failed错误的解决方法

    摘要 当使用rabbitmqctl时出现Erlang distribution failed,把%SystemRoot%Windows\System32\config\systemprofile下的. ...

  4. 选择、循环与函数结构:MATLAB VS Python

    选择.循环与函数结构:MATLAB VS Python 整理基本的程序控制结构,主要是选择 和 循环. 1.MATLAB选择结构 (1)单分支if语句格式: if 条件 语句组 end (2)双分支i ...

  5. 【起航计划 024】2015 起航计划 Android APIDemo的魔鬼步伐 23 App->Notification->IncomingMessage 状态栏通知

    应用程序可以使用Notifications来通知用户某个事件发生了(如收到短信).类NotificationManager 用来处理Notification, NotificationManager可 ...

  6. wechat开发笔记之1.线上环境搭建与测试

    Wechat开发笔记 线上环境搭建: 申请一个wechat公众平台. 手机个人微信可以用webwechat来测试. Website:https://web.weixin.qq.com/ 手机客户端扫一 ...

  7. 【QT】【OpenCv】初始配置以及测试功能

    #include "mainwindow.h" #include "ui_mainwindow.h" #include<opencv2/core/core ...

  8. Java -GUI开发九九乘法表

    Java GUI开发九九乘法表 (1)实现目标: 利用java自带的awt包,基础控件开发一个九九乘法表,点击可以显示对应的乘法口诀. (2)控件选择: 点击——Button 显示——TextFiel ...

  9. python3基础13(format的使用)

    #!/usr/bin/env python# -*- coding:utf-8 -*- from string import Templatedict={'name':'python','age':2 ...

  10. 什么是 pwd

    pwd print work directory, 指linux terminal的当前目录 $ pwd