辞树的最大数
Description
给出两个整数a和b,对于数字a可以无限次更换两个数位上的数字以构造不超过b的最大数。
Input
输入:
第一行输入一个整数 T(0<T<11),代表有T组数据。
每行输入两个整数 a,b (0<a,b < 1e18)
Output
输出:
打印最大可能的数字,该数字是a的数字排列并且不大于b。
每组数据输出后换行
Sample Input 1
3 123 222 3921 10000 4940 5000
Sample Output 1
213 9321 4940
思路:分两类:
如果a的长度比b的长度小 直接升序输出a
如果两者长度相同,模拟排序的过程,最后输出
#include<bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
#define maxn 105
#define mod 1e9+7
typedef long long ll;
ll T;
string a,b;
bool cmp(char a,char b)
{
return a > b;
}
int main()
{
ios::sync_with_stdio(false);
cin >> T;
while(T--)
{
cin >> a >> b;
if(a.size() < b.size())
{
sort(a.begin(),a.end(),cmp);
cout << a << endl;
}
else
{
sort(a.begin(),a.end());
string temp = a;
for(ll i = 0; i < a.size(); i++)
{
temp = a;
for(ll j = i+1; j < a.size(); j++)
{
swap(temp[i],temp[j]);
if(temp <= b)
a = temp;
}
}
cout << a << endl;
}
}
return 0;
}