题目
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 ≤ 10^18). The second line contains integer b (1 ≤ b ≤ 10^18). 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
思路
这道题就尼玛离谱。。。。用C++的String来做,利用其成员函数,快的一批。。。就一道纯模拟题(没啥思维,就模拟( ╯□╰ ))
代码如下
#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
typedef long long ll;
using namespace std;
const int mx=20;
string s1,s2;
int main()
{
cin>>s1>>s2;
int l1=s1.length();
int l2=s2.length();
sort(s1.begin(),s1.end());
if(l1<l2) //如果上面长度小,那么就尽可能大就好了= =
{
reverse(s1.begin(),s1.end());
}
else
{
for(int i=0;i<l1;i++)从第一位开始枚举
{
for(int j=l1-1;j>i;j--)//从后面大的开始枚举==
{
string tmp=s1;
swap(s1[i],s1[j]);
sort(s1.begin()+i+1,s1.end());
if(s1.compare(s2)>0)
s1=tmp;
else
break;
}
}
}
cout<<s1<<"\n";
return 0;
}