fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int t;
  6. cin >> t;
  7. while(t--)
  8. {
  9. int n,r;
  10. cin >> n;
  11. string s;
  12. cin >> s;
  13. map<char, int> m;
  14. for(int i=0;i<n;i++)
  15. {
  16. m[s[i]]++;
  17. }
  18. int low=10;
  19. for(auto x: m)
  20. {
  21. if(low>x.second)
  22. {
  23. low = x.second;
  24. }
  25. }
  26. if(low >= 2 && (s[0]!=s[n-1]))
  27. {
  28. cout << s[n-1];
  29. for(int i=1;i<n;i++)
  30. {
  31. cout << s[i] ;
  32. }
  33. cout << endl;
  34. }
  35. else if(low >= 2 && (s[0]==s[n-1]) && (s[0]!=s[1]))
  36. {
  37. cout << s[0] << s[0] ;
  38. for(int i=2;i<n;i++)
  39. {
  40. cout << s[i] ;
  41. }
  42. cout << endl;
  43. }
  44. else if(low < 2)
  45. {
  46. int l;
  47. for(auto x : m)
  48. {
  49. if(low == x.second)
  50. {
  51. r = l;
  52. break;
  53. }
  54. l++;
  55. }
  56. if(l!=0 && l!=(n-1))
  57. {
  58. for(int i=0;i<n;i++)
  59. {
  60.  
  61. if(l==i)
  62. {
  63. s[i-1];
  64. }
  65. cout << s[i] ;
  66. }
  67. cout << endl;
  68. }
  69. if(l==0)
  70. {
  71. cout << s[1] ;
  72. for(int i=1;i<n;i++)
  73. {
  74. cout << s[i] ;
  75. }
  76. cout << endl;
  77. }
  78. else if(l==(n-1))
  79. {
  80. for(int i=0;i<n-1;i++)
  81. {
  82. cout << s[i] ;
  83. }
  84. cout << s[n-2] << endl;
  85. }
  86. }
  87. }
  88. return 0;
  89. }
Success #stdin #stdout 0.01s 5320KB
stdin
6
3
abc
4
xyyx
8
alphabet
1
k
10
aabbccddee
6
ttbddq
stdout
abc
xxyx
alphabet
k
eabbccddee
ttbddq