fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define all(v) v.begin(),v.end()
  4. #define rall(v) v.rbegin(),v.rend()
  5. #define f first
  6. #define s second
  7. #define i128 __int128
  8. #define ll long long
  9. #define ull unsigned long long
  10. #define endl '\n'
  11. #define INF (unsigned long long)2000000000000000000
  12. #define input(v) for(auto& it : v){cin >> it;}
  13. void read128(i128 &x) {
  14. x = 0;
  15. string s;
  16. cin >> s;
  17. bool sign = 0;
  18. for (int i = 0; i < s.size(); i++) {
  19. if (s[i] == '-')sign = 1;
  20. else {
  21. x *= 10;
  22. x += s[i] - '0';
  23. }
  24. }
  25. if (sign)x *= -1;
  26. }
  27. void print128(i128 x) {
  28. string s = "";
  29. if (x == 0) {
  30. cout << '0';
  31. return;
  32. }
  33. bool sign = 0;
  34. if (x < 0) {
  35. sign = 1;
  36. x *= -1;
  37. }
  38. while (x) {
  39. s += (x % 10) + '0';
  40. x /= 10;
  41. }
  42. if (sign)
  43. s += '-';
  44. reverse(all(s));
  45. cout << s;
  46. }
  47. ll fastp(ll base,ll exp) {
  48. ll res = 1;
  49. while (exp) {
  50. if (exp & 1)res *= base;
  51. base *= base;
  52. exp >>= 1;
  53. }
  54. return res;
  55. }
  56. ll pmod(ll base,ll exp,ll mod) {
  57. ll res = 1;
  58. base %= mod;
  59. while (exp) {
  60. if (exp & 1) res *= base;
  61. res %= mod;
  62. base *= base;
  63. base %= mod;
  64. exp >>= 1;
  65. }
  66. return res;
  67. }
  68.  
  69. inline void solve(){
  70. string s;cin>>s;
  71. map<char,int>mp;
  72. for (auto &a:s) {
  73. mp[a]++;
  74. }
  75. int l = 0,r = 0;
  76. char cur = '#';
  77. while (r<s.size()) {
  78. cur = mp.size() + 'a'-1;
  79. if (s[r]<cur) {
  80. break;
  81. }else if (s[r] == cur){
  82.  
  83. }else {
  84. while (l<=r) {
  85. mp[s[l]]--;
  86. if (!mp[s[l]])mp.erase(s[l]);
  87. l++;
  88. }
  89. }
  90. r++;
  91. }
  92. cur = mp.size() + 'a'-1;
  93. int d = s.size()-1;
  94. while (s[d]>cur && mp[s[d]] > 1) {
  95. d--;
  96. }
  97. for (int i = l;i<=d;i++) {
  98. s[i] = cur;
  99. }
  100. cout<<s<<endl;
  101. }
  102. int main() {
  103. #ifdef LOCAL
  104. freopen("input.txt","r",stdin);
  105. #else
  106. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  107. #endif
  108.  
  109. int x = 1;
  110. // cin >> x;
  111. while (x--)
  112. solve();
  113. }
  114.  
  115.  
Success #stdin #stdout 0.01s 5316KB
stdin
badzzbabaa
stdout
dddddddddd