C. Permute Digits

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

Copy
123
222
output

Copy
213
input

Copy
3921
10000
output

Copy
9321
input

Copy
4940
5000
output

Copy
4940

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; int f, la, lb, vis[];
char a[], b[], ans[]; void dfs(int dep, int flag)//参数:当前位,结果当前位是否比 b小 ,dep是搜索深度
{
if (f == ) return; //如果已经找到结果则退出
if (dep == la)
{ //找到结果,将标志 f=1,并退出
f = ;
return;
}
for (int i = ; i >= ; i--)
{ //从大到小扫描数字
if (vis[i])//如果有可用数字 ,数组中0~9出现的次数不为零
{
if (flag || b[dep] == i + '')//如果已经出现某一位比 b小的或这当前位相等
{
vis[i]--;
ans[dep] = i + ''; //记录结果
dfs(dep + , flag); //处理下一个位置
if (f) return; //如果找到结果则退出
vis[i]++; //回溯时还原,重新处理当前位
}
else if (b[dep]>i + '')
{ //结果的当前位比 b小
vis[i]--;
ans[dep] = i + ''; //记录结果
dfs(dep + , ); //处理下一位,并将 flag=1
if (f) return; //如果找到结果则退出
//vis[i]++; //由于这个分支只可能进入一次,不还原也可以
}
}
}
}
int main()
{
int i;
while (cin >> a >> b)
{
la = strlen(a);
lb = strlen(b);
if (la<lb)
{ //当位数不同时
sort(a, a + la);
for (i = la - ; i >= ; i--)
cout << a[i];
cout << endl;
}
else
{ //当位数相同时
f = ;
memset(vis, , sizeof(vis));
for (i = ; i<la; i++)
vis[a[i] - '']++; //统计 a中每个数出现的次数
dfs(, ); //深搜
for (i = ; i<la; i++)
cout << ans[i];
cout << endl;
}
}
return ;
}

cf Permute Digits(dfs)的更多相关文章

  1. C. Permute Digits dfs大模拟

    http://codeforces.com/contest/915/problem/C 这题麻烦在前导0可以直接删除,比如 1001 100 应该输出11就好 我的做法是用dfs,每一位每一位的比较. ...

  2. Codeforces 915 C. Permute Digits (dfs)

    题目链接:Permute Digits 题意: 给出了两个数字a,b(<=1e18),保证a,b都不带前缀0.用a的字符重组一个数字使这个值最大且小于b.(保证这个值存在) 题解: 这题遇到了不 ...

  3. CodeForces-915C Permute Digits

    C. Permute Digits time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. Permute Digits 915C

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

  5. poj 3373 Changing Digits (DFS + 记忆化剪枝+鸽巢原理思想)

    http://poj.org/problem?id=3373 Changing Digits Time Limit: 3000MS   Memory Limit: 65536K Total Submi ...

  6. 【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 ...

  7. CF915C Permute Digits 字符串 贪心

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

  8. Permute Digits

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

  9. CF Preparing Olympiad (DFS)

    Preparing Olympiad time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. TextView下划线,部分文字并响应点击事件(SpannableString)

    TextView useInfo = (TextView) findViewById(R.id.info); useInfo.setText("开始即表示您同意遵守"); Stri ...

  2. fragment界面交互实操(步骤)

    首先,新建一个继承了fragment类的类,在oncreateview方法中,使用方法的参数inflater,用其inflater.inflate(R.layout.fragment1,contain ...

  3. Guava Cache本地缓存

    Guava介绍 Guava是一种基于开源的Java库,其中包含谷歌正在由他们很多项目使用的很多核心库. 这个库是为了方便编码,并减少编码错误. 这个库提供用于集合,缓存,支持原语,并发性,常见注解,字 ...

  4. 仿射变换详解 warpAffine

    转自 http://www.cnblogs.com/dupuleng/articles/4055020.html 博客园 首页 新随笔 联系 管理 订阅 随笔- 1  文章- 185  评论- 14  ...

  5. java内存模型和线程安全

  6. 547D Mike and Fish

    传送门 分析 见正睿10.3笔记 代码 #include<iostream> #include<cstdio> #include<cstring> #include ...

  7. vue 之 面向对象

    JavaScript 语言中,生成实例对象的传统方法是通过构造函数. function Animal(name,age){ this.name = name; this.age = age; } An ...

  8. Jquery 插件开发——citylinkage(省、市、县城市联动选择)

    第一部分:背景  开发源于需求,本次城市联动选择插件算是我写插件的一个特例吧,不是我目前工作需要些的,算是兴趣驱使吧.之前呢,一直想写这个插件,然后错过了一个写这个插件的机会(这个得回顾到很久以前了. ...

  9. SQL SERVER 提供了一些时间函数:

    SQL SERVER 提供了一些时间函数:取当前时间:select getdate()取前一个月的时间:SELECT DATEADD(MONTH,-1,GETDATE()) 月份减一个月取年份:SEL ...

  10. Net.Core导入EXCel文件里的数据

    1.前台的表单: <form enctype="multipart/form-data" method="post" id="inportFil ...