题目链接:http://codeforces.com/problemset/problem/915/C点击打开链接
有点类似于数位dp的思想 就是用一个book记录他当前这位是否限制
然后dfs找最大
特判两数相同和a位数小于b的情况
#include <bits/stdc++.h>
using namespace std;
long long int num1[10];
long long int num2[10];
vector<long long int > judge;
long long int n,m;
long long int length1=0,length2=0;
long long int len;
int flag;
long long int ans;
void dfs(long long int now,long long int pos,long long int book)
{
if(flag)
return;
if(!len&&now<m)
{
flag=1;
ans=now;
return ;
}
if(book)
{
for(long long int i=9;i>=0;i--)
{
if(num1[i])
{
num1[i]--;
len--;
long long int mid=pos;
long long int midnow=now;
long long int sum=1;
while(mid--)
sum*=10;
sum*=i;
dfs(now+sum,pos-1,book);
num1[i]++;
len++;
}
}
}
else
{
for(long long int i=judge[pos];i>=0;i--)
{
if(num1[i])
{
if(i==judge[pos])
{
num1[i]--;
len--;
long long int mid=pos;
long long int midnow=now;
long long int sum=1;
while(mid--)
sum*=10;
sum*=i;
dfs(now+sum,pos-1,0);
num1[i]++;
len++;
}
else
{
num1[i]--;
len--;
long long int mid=pos;
long long int midnow=now;
long long int sum=1;
while(mid--)
sum*=10;
sum*=i;
dfs(now+sum,pos-1,1);
num1[i]++;
len++;
}
}
}
}
}
int main()
{
cin >>n >>m;
long long int mid=n;
while(mid)
{
num1[mid%10]++;
mid/=10;
length1++;
}
mid=m;
while(mid)
{
judge.push_back(mid%10);
num2[mid%10]++;
mid/=10;
length2++;
}
if(length1<length2)
{
for(int i=9;i>=0;i--)
{
while(num1[i])
{
cout <<i;
num1[i]--;
}
}
return 0;
}
len=length1;
int fflag=0;
for(int i=0;i<=9;i++)
if(num1[i]!=num2[i])
fflag=1;
if(fflag)
{
dfs(0,length1-1,0);
cout <<ans <<endl;
}
else
{
reverse(judge.begin(),judge.end());
int ffflag=0;
for(int i=0;i<judge.size();i++)
{
if(judge[i]==0&&!ffflag)
continue;
else
{
cout <<judge[i];
ffflag=1;
}
}
}
}