Advertisement
pb_jiang

huawei

May 20th, 2025
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.58 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #ifndef __DEBUG__
  5. #define dbg(...) 42
  6. #endif
  7. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  8.  
  9. namespace rngs = std::ranges;
  10. using ll = long long;
  11. using a2l = array<ll, 2>;
  12. using pll = pair<ll, ll>;
  13. using vl = vector<ll>;
  14.  
  15. void solve()
  16. {
  17.     string s;
  18.     cin >> s;
  19.     map<char, vl> pos;
  20.     for (ll i = 0; i < s.size(); ++i) {
  21.         char c = s[i];
  22.         pos[c].push_back(i);
  23.     }
  24.     for (auto &[k, v] : pos)
  25.         reverse(v.begin(), v.end());
  26.     dbg(s, pos);
  27.  
  28.     ll sz = pos.size();
  29.     string ans;
  30.  
  31.     for (ll i = 0, p = -1; i < sz; ++i) {
  32.         char op = 'a';
  33.         ll idx = -1;
  34.         for (auto ch = 'a'; ch <= 'z'; ++ch) {
  35.             if (pos.count(ch) == 0)
  36.                 continue;
  37.             auto &v = pos[ch];
  38.             while (!v.empty() && v.back() <= p)
  39.                 v.pop_back();
  40.  
  41.             ll id = v.back();
  42.             bool valid = true;
  43.             for (auto &[_, v2] : pos) {
  44.                 if (v2[0] < id) {
  45.                     valid = false;
  46.                     break;
  47.                 }
  48.             }
  49.             if (valid) {
  50.                 op = ch;
  51.                 idx = id;
  52.                 break;
  53.             }
  54.         }
  55.         assert(idx != -1);
  56.         dbg(op, pos);
  57.         ans += op;
  58.         pos.erase(op);
  59.         p = idx;
  60.     }
  61.  
  62.     dbg(ans);
  63.     cout << ans << '\n';
  64. }
  65.  
  66. int main(int argc, char **argv)
  67. {
  68.     std::ios::sync_with_stdio(false);
  69.     std::cin.tie(nullptr);
  70.  
  71.     solve();
  72.  
  73.     return 0;
  74. };
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement