fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string makeString(string s, int k){
  5. for(int i = 0; i < (int)s.size() - k + 1; ++i){
  6. reverse(s.begin() + i, s.begin() + i + k);
  7. }
  8. return s;
  9. }
  10.  
  11. int main(){
  12.  
  13. ios::sync_with_stdio(0);
  14. cin.tie(0);
  15. cout.tie(0);
  16.  
  17. int t, n;
  18. string s;
  19. cin >> t;
  20. while(t--){
  21. cin >> n >> s;
  22. if(n == 1){
  23. cout << s << '\n' << 1 << '\n';
  24. continue;
  25. }
  26. bool ok = true;
  27. char mn = s[0];
  28. for(int i = 1; i < n; ++i){
  29. if(mn != s[i]){
  30. ok = false;
  31. }
  32. if(mn > s[i]){
  33. mn = s[i];
  34. }
  35. }
  36. if(ok){
  37. cout << s << '\n' << 1 << '\n';
  38. continue;
  39. }
  40. string small = s;
  41. int k = 1;
  42. for(int i = 0; i < n; ++i){
  43. if(s[i] == mn){
  44. string now = makeString(s, i + 1);
  45. if(now < small && i + 1 > k){
  46. small = now;
  47. k = i + 1;
  48. }
  49. }
  50. }
  51. cout << small << '\n' << k << '\n';
  52. }
  53. return 0;
  54. }
  55.  
Runtime error #stdin #stdout 0s 4380KB
stdin
Standard input is empty
stdout
Standard output is empty