fork download
  1. #include<bits/stdc++.h>
  2. #define ii pair<int, int>
  3. #define fi first
  4. #define se second
  5. #define iii pair<ii, int>
  6. using namespace std;
  7.  
  8. const int MOD = int(1e5 + 7);
  9. const int nmax = int(1e6 + 7);
  10.  
  11. int n, m;
  12. vector<int> a;
  13. long long d[nmax] = {0};
  14.  
  15. void ip() {
  16. string s; cin >> s;
  17. n = s.size();
  18. if(n == 1) {
  19. cout << s << '\n'; return;
  20. }
  21. int pos_max = 0;
  22. int pos_min = -1;
  23. if(s[0] < s[1]) pos_min = 0;
  24. for(int i = 1; i < n; ++i) {
  25. if(s[i] >= s[pos_max]) pos_max = i;
  26. if(i < n-1 and s[i] < s[i+1] and pos_min == -1) pos_min = i;
  27. }
  28. string ans = s;
  29. if(pos_max > 0) {
  30. string re = s;
  31. reverse(re.begin(), re.begin() + pos_max+1);
  32. reverse(re.begin()+1, re.begin() + pos_max+1);
  33. ans = max(ans, re);
  34. }
  35. if(pos_min != -1) {
  36. string re = s;
  37. reverse(re.begin() + pos_min, re.end());
  38. reverse(re.begin() + pos_min, re.end()-1);
  39. ans = max(ans, re);
  40. }
  41. cout << ans << '\n';
  42. }
  43. int main(){
  44. ios_base::sync_with_stdio(0);
  45. cin.tie(0), cout.tie(0);
  46.  
  47. if(fopen("task.inp", "r")) {
  48. freopen("task.inp", "r", stdin);
  49. freopen("task.out", "w", stdout);
  50. }
  51. int tt = 1;
  52. cin >> tt;
  53. while(tt--) {
  54. ip();
  55. }
  56. }
  57.  
Success #stdin #stdout 0.01s 5320KB
stdin
5
1991
2024
919293
8866725
33222110
stdout
9911
4202
992931
8867256
33222110