Advertisement
deadwing97

ِALLSUB

Sep 28th, 2019
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.26 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int MX = (1<<18);
  6.  
  7. int T;
  8.  
  9. string A , B;
  10.  
  11. int cnt[300];
  12.  
  13. int main(){
  14.  
  15.     cin>>T;
  16.  
  17.     int alla = 0 , allb = 0;
  18.     while(T--){
  19.  
  20.         cin>>A>>B;
  21.  
  22.         memset(cnt , 0 , sizeof(cnt));
  23.  
  24.         for(auto ch : B) ++cnt[ch];
  25.  
  26.         bool imp = 0;
  27.  
  28.         for(auto ch : A){
  29.             --cnt[ch];
  30.         }
  31.  
  32.         for(int j = 'a' ; j <= 'z' ; j++){
  33.             if(cnt[j] < 0)
  34.                 imp = 1;
  35.         }
  36.  
  37.         if(imp){
  38.             puts("Impossible");
  39.             continue;
  40.         }
  41.  
  42.         string ans;
  43.  
  44.         for(int j = 'a' ; j < A[0] ; j++)
  45.             while(cnt[j] > 0) {
  46.                 ans.push_back(j);
  47.                 --cnt[j];
  48.             }
  49.  
  50.         int flet = A[0];
  51.  
  52.         for(int j = 0 ; j < A.size() ; j++){
  53.             if(A[j] > A[0]) break;
  54.             if(A[j] < A[0]){
  55.                 ans += A;
  56.                 A.clear();
  57.                 break;
  58.             }
  59.         }
  60.  
  61.         while(cnt[flet] > 0) {
  62.             ans.push_back(flet);
  63.             --cnt[flet];
  64.         }
  65.  
  66.         ans += A;
  67.  
  68.         for(int j = 'a' ; j <= 'z' ; j++)
  69.             while(cnt[j]--)
  70.                 ans.push_back(j);
  71.  
  72.         cout<<ans<<endl;
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement