-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeedleinaHaystack.cpp
More file actions
66 lines (64 loc) · 1014 Bytes
/
NeedleinaHaystack.cpp
File metadata and controls
66 lines (64 loc) · 1014 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int MOD = 1e9 + 7;
void solve(string &s,string &t)
{
map<char,int> mpp;
for(auto &x:t)
mpp[x]++;
for(auto &x:s)
{
auto it=mpp.find(x);
if(it==mpp.end() || it->second==0)
{
cout<<"Impossible"<<endl;
return;
}
mpp[x]--;
}
int chr = 0;
for(auto it=mpp.begin();it!=mpp.end();it++)
{
if(chr==s.size())
{
for(int i=0;i<it->second;i++)
cout<<it->first;
continue;
}
if(it->first<s[chr])
{
for(int i=0;i<it->second;i++)
cout<<it->first;
}
else
{
while(chr<s.size() && s[chr]<=it->first)
{
cout<<s[chr];
chr++;
}
for(int i=0;i<it->second;i++)
cout<<it->first;
}
}
cout<<endl;
return;
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=1;
cin>>t;
while(t--)
{
string s,t;
cin>>s;
cin>>t;
solve(s,t);
}
return 0;
}