fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int Mod=1e9+7;
  5. const ll INF = 10000000000000;
  6. const int N = 1e6+7;
  7.  
  8. void solve() {
  9. int n;
  10. cin >> n;
  11. string s;
  12. cin >> s;
  13. vector<int> freq(26,0);
  14. for(int i=0;i<n;i++) freq[s[i]-'a']++;
  15. char last = '?';
  16. for(int i=0;i<n;i++){
  17. bool ok = false;
  18. for(int j=0;j<26 && !ok;j++){
  19. if(freq[j]>0 && j+'a'!=last){
  20. cout << (char)(j+'a');
  21. last=(char)(j+'a');
  22. freq[j]--;
  23. ok=true;
  24. }
  25. }
  26. if(!ok){
  27. for(int j=0;j<26;j++){
  28. if(freq[j]>0){
  29. cout << (char)(j+'a');
  30. freq[j]--;
  31. break;
  32. }
  33. }
  34. }
  35. }
  36. cout << '\n';
  37. }
  38.  
  39. int main(){
  40. ios::sync_with_stdio(false);
  41. cin.tie(nullptr);
  42.  
  43. int t;
  44. cin >> t;
  45. while (t--) solve();
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0.01s 5288KB
stdin
5
3
abc
5
edddf
6
turtle
8
pppppppp
10
codeforces
stdout
abc
dedfd
elrtut
pppppppp
cdcefeoros