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
题意 给两个数a和b,可以任意调换a中数字的位置,问由a变成的最大的不超过b的数是什么? 分析 暴搜加剪枝。因为结果一定存在,那么可以分为两种情况,lena
#include#include #include #include #include #include #include #include #include using namespace std;typedef long long LL;const int maxn = 400;const int mod = 772002+233;typedef pair pii;#define X first#define Y second#define pb push_back//#define mp make_pair#define ms(a,b) memset(a,b,sizeof(a))const int inf = 0x3f3f3f3f;char a[20],b[20],ans[20];int cnt[10];int lena,lenb;bool dfs(int idx,int flag){ if(idx==lenb) return true; for(int ia=9;ia>=0;ia--){ if(cnt[ia]){ if(flag ||ia+'0'==b[idx]){ //此前b已比ans大了 ans[idx]=ia+'0'; cnt[ia]--; if(dfs(idx+1,flag)) return true; cnt[ia]++; }else if(ia+'0' =0;i--) putchar(a[i]); }else{ ms(cnt,0); for(int i=0;i