Description
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
123
222
output
213
input
3921
10000
output
9321
input
4940
5000
output
4940
题目大意
给出两个整数,用第一个整数中的数字输出小于等于第二个整数的最大的值
解题思路
1、当第一个数的长度小于第二个数的长度时,直接将第一个数的数字从大到小输出;
2、从左至右遍历目标数串的每一位数字 t,在原数串中小于等于 t 的数字中挑选,
若原数串中有等于 t 的数字,则答案的当前位即为 t ;
若原数串中可选择的数字均小于t,则剩余位均从大到小选择即可;
3、若原数串中剩余的数字均大于 t ,则说明原来的挑选不合理,需要回退,即将原来的数变小,当回退到当前位数字在原数串剩余的数中存在比它小的数则停止回退,并更新当前位数字,且剩余位在原数串剩余数字中从大到小挑选即可。
代码实现
#include<bits/stdc++.h>
using namespace std;
#define maxn 27
char str1[maxn],str2[maxn];
int num[10];
bool cmp(char ch1,char ch2)
{
return ch1>ch2;
}
int main()
{
ios::sync_with_stdio(false);
cin>>str1>>str2;
int len1=strlen(str1);
int len2=strlen(str2);
if(len1<len2)
{
sort(str1,str1+len1,cmp);
cout<<str1<<endl;
}
else
{
bool flag=false;
for(int i=0; i<len1; i++)
num[str1[i]-'0']++;
strcpy(str1,str2);
for(int i=0; i<len2; i++)
{
int j;
if(!flag)
j=str1[i]-'0';
else
j=9;
for(; j>=0; j--)
{
if(num[j]>0)
{
str1[i]=j+'0';
num[j]--;
if(j<str2[i]-'0')
flag=true;
break;
}
}
if(j==-1&&(!flag))
{
--i;
num[str1[i]-'0']++;
str1[i]--;
--i;
}
}
cout<<str1<<endl;
}
return 0;
}