fork download
  1. #include<bits/stdc++.h>
  2. #define f1(i, n) for(ll i=1;i<=n;++i)
  3. #define f0(i, n) for(ll i=0;i<n;++i)
  4. #define ull unsigned long long
  5. #define ll long long
  6. #define rev(a) reverse(a.begin(),a.end())
  7. #define all(x) x.begin(),x.end()
  8. #define so(A, n) sort(A+1, A+n+1)
  9. using namespace std;
  10. const int maxn = 200010;
  11. const int N = 2e5 + 5;
  12. int main()
  13. {
  14. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  15. string s;
  16. cin >> s;
  17. vector<string> v;
  18. v.push_back(s);
  19. pair<char, int> MIN[s.size() + 1];
  20. MIN[s.size()].first = char('z' + 1);
  21. MIN[s.size()].second = s.size();
  22. for (int i = s.size() - 1; i >= 0; --i) {
  23. if (MIN[i + 1].first > s[i]) {
  24. MIN[i].first = s[i];
  25. MIN[i].second = i;
  26. }
  27. else {
  28. MIN[i].first = MIN[i + 1].first;
  29. MIN[i].second = MIN[i + 1].second;
  30. }
  31. }
  32. for (int i = 0; i < s.size() - 1; ++i) {
  33. string a = s;
  34. swap(a[MIN[i + 1].second], a[i]);
  35. v.push_back(a);
  36. }
  37. sort(all(v));
  38. cout << v[0];
  39.  
  40.  
  41. return 0;
  42. }
  43.  
  44.  
  45.  
Success #stdin #stdout 0.01s 5316KB
stdin
adebeb
stdout
abebed