Advertisement
K_Y_M_bl_C

Untitled

Sep 29th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. //#define _GLIBCXX_DEBUG
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. typedef pair<int, int> pii;
  9. typedef vector<int> vi;
  10. typedef long double ld;
  11.  
  12. #define mk make_pair
  13. #define inb push_back
  14. #define X first
  15. #define Y second
  16. #define all(v) v.begin(), v.end()
  17. #define sqr(x) (x) * (x)
  18. #define TIME 1.0 * clock() / CLOCKS_PER_SEC
  19. #define y1 AYDARBOG
  20. //continue break pop_back return
  21.  
  22. int solve();
  23.  
  24. int main()
  25. {
  26. //ios_base::sync_with_stdio(0);
  27. //cin.tie(0);
  28. #define TASK "password"
  29. #ifndef _DEBUG
  30. freopen(TASK".in", "r", stdin), freopen(TASK".out", "w", stdout);
  31. #endif
  32. solve();
  33. #ifdef _DEBUG
  34. fprintf(stderr, "\nTIME: %.3f\n", TIME);
  35. #endif
  36. }
  37.  
  38. /*const int BUFSZ = (int)1e5 + 7;
  39. char buf[BUFSZ];
  40. string get_str()
  41. {
  42. scanf(" %s", buf);
  43. return string(buf);
  44. }*/
  45.  
  46. int cnt[26], cntsk[26];
  47. char s[30000001], t[3000001];
  48. char ans[3000001];
  49. int sz = 0;
  50.  
  51. int solve()
  52. {
  53. scanf(" %s", s);
  54. scanf(" %s", t);
  55. int n = strlen(s);
  56. int m = strlen(t);
  57. for (int i = 0; i < n; ++i)
  58. {
  59. cntsk[s[i] - 'a']++;
  60. }
  61. for (int i = 0; i < m; ++i)
  62. {
  63. cnt[t[i] - 'a']++, cntsk[t[i] - 'a']--;
  64. }
  65. for (int i = 0; i < 26; ++i)
  66. if (cntsk[i] < 0)
  67. puts("impossible"), exit(0);
  68. for (int i = 0; i < n; ++i)
  69. {
  70. int cur = s[i] - 'a';
  71. if (cnt[cur])
  72. {
  73. while (sz && cntsk[ans[sz - 1] - 'a'] && s[i] < ans[sz - 1])
  74. {
  75. --cntsk[ans[sz - 1] - 'a'];
  76. ++cnt[ans[sz - 1] - 'a'];
  77. --sz;
  78. }
  79. ans[sz] = s[i];
  80. ++sz;
  81. --cnt[ans[sz - 1] - 'a'];
  82. }
  83. else
  84. {
  85. --cntsk[cur];
  86. }
  87. }
  88. ans[sz] = 0;
  89. printf("%s\n", ans);
  90. return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement