Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#define _GLIBCXX_DEBUG
- #define _CRT_SECURE_NO_WARNINGS
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef pair<int, int> pii;
- typedef vector<int> vi;
- typedef long double ld;
- #define mk make_pair
- #define inb push_back
- #define X first
- #define Y second
- #define all(v) v.begin(), v.end()
- #define sqr(x) (x) * (x)
- #define TIME 1.0 * clock() / CLOCKS_PER_SEC
- #define y1 AYDARBOG
- //continue break pop_back return
- int solve();
- int main()
- {
- //ios_base::sync_with_stdio(0);
- //cin.tie(0);
- #define TASK "password"
- #ifndef _DEBUG
- freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
- #endif
- solve();
- #ifdef _DEBUG
- fprintf(stderr, "\nTIME: %.3f\n", TIME);
- #endif
- }
- /*const int BUFSZ = (int)1e5 + 7;
- char buf[BUFSZ];
- string get_str()
- {
- scanf(" %s", buf);
- return string(buf);
- }*/
- int cnt[26], cntsk[26];
- char s[30000001], t[3000001];
- char ans[3000001];
- int sz = 0;
- int solve()
- {
- scanf(" %s", s);
- scanf(" %s", t);
- int n = strlen(s);
- int m = strlen(t);
- for (int i = 0; i < n; ++i)
- {
- cntsk[s[i] - 'a']++;
- }
- for (int i = 0; i < m; ++i)
- {
- cnt[t[i] - 'a']++, cntsk[t[i] - 'a']--;
- }
- for (int i = 0; i < 26; ++i)
- if (cntsk[i] < 0)
- puts("impossible"), exit(0);
- for (int i = 0; i < n; ++i)
- {
- int cur = s[i] - 'a';
- if (cnt[cur])
- {
- while (sz && cntsk[ans[sz - 1] - 'a'] && s[i] < ans[sz - 1])
- {
- --cntsk[ans[sz - 1] - 'a'];
- ++cnt[ans[sz - 1] - 'a'];
- --sz;
- }
- ans[sz] = s[i];
- ++sz;
- --cnt[ans[sz - 1] - 'a'];
- }
- else
- {
- --cntsk[cur];
- }
- }
- ans[sz] = 0;
- printf("%s\n", ans);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement