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. Ros学习——Python发布器publisher和订阅器subscriber

    1.编写发布器 初始化 ROS 系统 在 ROS 网络内广播我们将要在 chatter 话题上发布 std_msgs/String 类型的消息 以每秒 10 次的频率在 chatter 上发布消息 在 ...

  2. php验证是否建立数据库,否,则自动建立

    <?php /* 默认建立的数据库为test,数据表为admin,管理员只需要修改DB_PWD(即本地的服务器密码)即可,用户密码采用md5加密 */ define(DB_HOST," ...

  3. Condition实现多个生产者多个消费者

    Condition实现多对多交替打印: import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.R ...

  4. IFC数据模型在三维引擎中模拟

  5. g2o20160430下的csparse文件夹内的CMakeLists.txt

    1. g2o20160430下的csparse文件夹内的CMakeLists.txt cmake_minimum_required(VERSION 2.6) PROJECT(csparse) SET( ...

  6. JDK并发包2-线程池

  7. hdu6331 Problem M. Walking Plan

    传送门 题目大意 给你一个n点m条边的有向图,q次询问,给定s,t,k,求由s到t至少经过k条边的最短路. 分析 我们设dp[i][j][k]为从i到j至少经过k条边的最短路,sp[i][j]意为从i ...

  8. tab下拉菜单

    这个想法早就有的 (写tab下拉菜单)就觉得自己对js不是很熟   所以一直没有写 花了不少时间 <!DOCTYPE html> <html> <head> < ...

  9. Linux网络服务管理命令

    netstat命令 示例:查看指定的服务是否开启netstat | grep ssh | grep -v grep 网络下载器————wget wget是一个Linux环境下用于从WWW上提取文件的工 ...

  10. WordCount-软件测试初体验

    github:https://github.com/skz12345/WordCount PSP2.1 PSP阶段 预估耗时(分钟) 实际耗时(分钟) Planning 计划 40 60 · Esti ...