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.
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.
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.
123 222
213
3921 10000
9321
4940 5000
4940 题意:给两个数a和b,可以打乱a每位数的顺序组成一个新的数c, 求满足c<=b的最大,保证结果一定存在。 分析:先求出a的每位数字,然后排个序,再dfs。
#include#include #include using namespace std;int A[100],B[100],C[100],alen,blen;int vis[30],ans[30],K=-1;int cmp(int x,int y){ return x>y;}int f(int a[],long long x){ int len=0; while(x) { a[len++]=x%10; x/=10; } return len;}int dfs(int k)//k为当前构造到第k位数 { if(k==alen) return 1; int last=-1;//前一个数 for(int i=0;i =0) { for(int i=0;i<=K;i++) printf("%d",ans[i]); for(int i=0;i