C. Permute Digits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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

题目大意:给定两个正整数A,B(1<=A,B<=10e18),现在你可以对A进行任意排序,问在排序后A不大于B的情况下,A的最大值是多少。题目保证A、B没有前缀零。

题目分析:我是用贪心+DFS+剪枝优化过的,关键的地方在于剪枝,在排序后选取小于或等于B[i]时都是一次性的,具体的可以看看代码。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e6+100;
int N,M,pos,l,r,judge=0;
string ax,bx,cx,dx;
int vis[100];
void dfs(int now,int flag)
{
   if(flag)
    {
           for(int i=0;i<ax.length();i++)
           {
               if(!vis[i])
                dx+=ax[i];
           }
           cx=dx;
           judge=1;
           return ;
    }
   if(now==bx.length())
   {
       cx = dx;
       judge=1;
       return ;
   }
   int temp =0;
   for(int i=0;i<ax.length();i++)
   {
       if(!vis[i]&&ax[i]<bx[now])
       {
           dx+=ax[i];
            vis[i]=1;
           dfs(now+1,flag|1);
           return ;
       }
       if(!vis[i]&&ax[i]==bx[now]&&!temp)
       {
           dx+=ax[i];
           vis[i]=1;
           temp = 1 ;
           dfs(now+1,flag);
           if(judge)
            return ;
           else
           {
                vis[i]=0;
                dx = dx.substr(0,dx.length() - 1);
           }
       }
       if(judge)
        return ;
   }
}
int main()
{

     std::ios::sync_with_stdio(false);
     std::cin.tie(0);
     while(cin>>ax>>bx)
     {
         judge=0;
         dx = "";
         memset(vis,0,sizeof(vis));

         if(ax.length()<bx.length())
         {
             sort(ax.begin(),ax.end());
             reverse(ax.begin(),ax.end());
             cout<<ax<<endl;
             continue;
         }
         while(bx.length()<ax.length())
            bx = '0'+bx;
         vector<char> ac;
         vector<char> ans;
        sort(ax.begin(),ax.end());
        reverse(ax.begin(),ax.end());
            dfs(0,0);
           cout<<cx<<endl;
     }
    return 0;
}
   其实我觉得我这个方法蛮笨的,后面去看了别人的代码,感觉到了好大的差距= =..

   首先是string其实是很好用的,它自带很多很好的操作,很方便,像上面的          substring(begin,end)提取子字符串就很舒服。然后下面会用到stoll,是字符串转为long long类型,当然还有stoi转int。还会用到prev_permutation,和next_permutation相对,这个是求上一个排列的字符串。

下面的代码是学习别人的,主要就是从前往后,保证当前比较位最大,这样最后总串也就最大了,很巧妙。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
   string a;
   LL b;
   cin>>a>>b;
   priority_queue<int,vector<int>,greater<int> >Q;
   for(int i=0;i<a.length();i++)
   {
       sort(a.begin()+i,a.end(),greater<char>());
       sort(a.begin()+i+1,a.end());
       while(stoll(a)>b)
       {
           prev_permutation(a.begin()+i,a.end());
           sort(a.begin()+i+1,a.end());
       }
   }
   cout<<a<<endl;

   return 0;
}


Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐