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
  1. 123
    222
output
  1. 213
input
  1. 3921
    10000
output
  1. 9321
input
  1. 4940
    5000
output
  1. 4940

题解:

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

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. char a[20],b[20];
  4. int num[11],x[200],w[200],lb,la;
  5. long long MAX=0,B;
  6. inline long long MAx(long long z,long long w)
  7. {
  8. if(z>w) return z;
  9. return w;
  10. }
  11. bool cmp(char a,char b)
  12. {
  13. return a>b;
  14. }
  15.  
  16. void dfs(long long ans,int len,int k,int v)
  17. {
  18. if(len==la&&len<=lb&&ans<=B) {
  19. MAX=MAx(ans,MAX);
  20. ans=0;
  21. return;
  22. }
  23. for (int i = 9; i >=0 ; i--) {
  24. if (num[i])
  25. {
  26. if(v==0&&i==w[k])
  27. {
  28. num[i]--;
  29. ans=ans*10+i;
  30. dfs(ans,len+1,k+1,0);
  31. ans-=i;
  32. ans/=10;
  33. num[i]++;
  34. } else if(i<w[k])
  35. {
  36. num[i]--;
  37. ans=ans*10+i;
  38. for (int j = 9; j >=0 ; j--) {
  39. for (int z = 0; z <num[j] ; ++z) {
  40. ans=ans*10+j;
  41. }
  42. }
  43. if(ans<B)
  44. {
  45. MAX=MAx(MAX,ans);
  46. }
  47. ans=0;
  48. }
  49. }
  50. }
  51. }
  52. int main()
  53. {
  54. scanf("%s",a);
  55. scanf("%s",b);
  56. la=(int)strlen(a);
  57. lb=(int)strlen(b);
  58. for (int i = 0; i <la ; ++i) {
  59. num[a[i]-'0']++;
  60. x[i]=a[i]-'0';
  61. }
  62. for (int i = 0; i<lb; i++) {
  63. w[i]=b[i]-'0';
  64. B=B*10+w[i];
  65. }
  66. if(la<lb)
  67. {
  68. sort(a,a+la,cmp);
  69. for (int i = 0; i <la ; ++i) {
  70. printf("%c",a[i]);
  71. }
  72. printf("\n");
  73. return 0;
  74. }
  75. dfs(0,0,0,0);
  76. printf("%lld",MAX);
  77. return 0;
  78. }

  

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. Java执行Shell脚本

    Linux 系统下采用 Java 执行 Shell 脚本,直接上代码: package com.smbea.demo; import java.io.BufferedReader; import ja ...

  2. svg的基本图形与属性【小尾巴的svg学习笔记1】

    因为项目有可能用到, 所以学习了一下,做此笔记,图截自慕课网,侵删. 一.基本图形 1.矩形 x,y定义矩形的左上角坐标: width,height定义矩形的长度和宽度: rx,ry定义矩形的圆角半径 ...

  3. Dreams save us. Dreams lift us up and transform us into something better.

    Dreams save us. Dreams lift us up and transform us into something better.梦想能够拯救我们.梦想能够激励我们并让我们成为更好的人 ...

  4. SpringMVC快速入门

    导入开发包 前6个是Spring的核心功能包[IOC],第7个是关于web的包,第8个是SpringMVC包 org.springframework.context-3.0.5.RELEASE.jar ...

  5. iptable防范ddos攻击

    Basic DoS Protection https://github.com/MPOS/php-mpos/wiki/Basic-DoS-Protection # Rule 1: Limit New ...

  6. 第四章 T-SQL编程

    1.前言->此T-SQL编程是基于sql server开发环境->关键字:T-SQL编程:游标:视图和索引 2.T-SQL编程基础->标识符:常规标识符必须以汉字.字母.下划线_.@ ...

  7. 用户管理的设计--3.jquery的ajax实现二级联动

    页面效果 实现步骤 1.引入struts整合json的插件包 2.页面使用jquery的ajax调用二级联动的js //ajax的二级联动,使用选择的所属单位,查询该所属单位下对应的单位名称列表 fu ...

  8. centos6.5_64bit-禅道安装及数据库操作

    linux一键安装包内置了apache, php, mysql这些应用程序,只需要下载解压缩即可运行禅道. 从7.3版本开始,linux一键安装包分为32位和64位两个包,请大家根据操作系统的情况下载 ...

  9. Html + JS : 点击对应的按钮,进行选择是隐藏还是显示(用户回复功能)

    例如: 当我点击按钮1时,点击第一下进行显示This is comment 01,点击第二下隐藏This is comment 01 当我点击按钮2时,点击第一下进行显示This is comment ...

  10. 2018.9.9 Tomcat是怎样运行的

    一. Servlet容器是怎样工作的 一个Servlet容器是一个复杂的系统.然而,对于处理对Servlet的请求,Servlet容器主要做三件事情: 1. 创建请求对象,并设置所调用的Servlet ...