Advertisement
Graf_Rav

Untitled

Jan 13th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. int getLen(long long a){
  8.     int k=0;
  9.     while(a>0){
  10.         k++;
  11.         a/=10;
  12.     }
  13.     return k;
  14. }
  15.  
  16. void FillArr(vector<int> &numbers, long long a){
  17.     while(a>0){
  18.         numbers.push_back(a%10);
  19.         a/=10;
  20.     }
  21. }
  22.  
  23. void FillA(vector<int> &numbers, long long a){
  24.     while(a>0){
  25.         numbers[a%10]++;
  26.         a/=10;
  27.     }
  28. }
  29.  
  30. long long ans1(vector<int> numbers, int k){
  31.     long long a=0;
  32.     while(k>0){
  33.         for(int i=9;i>=0;i--){
  34.             if(numbers[i]>0){
  35.                 k--;
  36.                 numbers[i]--;
  37.                 a*=10;
  38.                 a+=i;
  39.             }
  40.         }
  41.     }
  42.     return a;
  43. }
  44.  
  45. void ans2(vector<int> numbers, int k, vector<int> numbersB, long long ans, bool bilo){
  46.     if(k==0){
  47.         cout<<ans;
  48.         exit(0);
  49.     }
  50.  
  51.     for(int i=9;i>=0;i--){
  52.         if(numbers[i]>0){
  53.             if(!bilo && i>numbersB[k-1]){
  54.             }
  55.             else{
  56.                 bool g=bilo;
  57.                 if(!bilo && i<numbersB[k-1]){
  58.                     g=true;
  59.                 }
  60.                 vector<int> clone=numbers;
  61.                 clone[i]--;
  62.                 ans2(clone, k-1, numbersB, ans*10+i, g);
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. int main(){
  69.     long long a, b;
  70.     cin>>a>>b;
  71.     int lenA=getLen(a);
  72.     int lenB=getLen(b);
  73.  
  74.     vector<int> numbers(10, 0);
  75.     FillA(numbers, a);
  76.  
  77.     vector<int> numbersB;
  78.     FillArr(numbersB, b);
  79.  
  80.     if(lenB>lenA){
  81.         cout<<ans1(numbers, lenA);
  82.     }
  83.     else{
  84.         ans2(numbers, lenA, numbersB, 0, false);
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement