fork download
  1. #include<unordered_map>
  2. #include<unordered_set>
  3. #include<functional>
  4. #include<algorithm>
  5. #include<iostream>
  6. #include<hash_map>
  7. #include<iterator>
  8. #include<iomanip>
  9. #include<numeric>
  10. #include<cstring>
  11. #include<vector>
  12. #include<bitset>
  13. #include<string>
  14. #include<deque>
  15. #include<stack>
  16. #include<queue>
  17. #include<array>
  18. #include<cmath>
  19. #include<list>
  20. #include<map>
  21. #include<set>
  22.  
  23. #include <ext/pb_ds/assoc_container.hpp>
  24. #include <ext/pb_ds/tree_policy.hpp>
  25.  
  26. using namespace __gnu_pbds;
  27. using namespace std;
  28.  
  29. typedef long long ll;
  30. typedef unsigned long long ull;
  31. typedef double db;
  32. typedef long double ldb;
  33.  
  34. #define ordered_set tree<ll, null_type,less_equal<ll>, \
  35. rb_tree_tag,tree_order_statistics_node_update>
  36. #define pii pair<int,int>
  37. #define pll pair<ll,ll>
  38. #define inf INT32_MAX
  39. #define linf INT64_MAX
  40. #define pf push_front
  41. #define pb push_back
  42. #define ppb pop_back
  43. #define ppf pop_front
  44. #define ff first
  45. #define ss second
  46.  
  47. ll fastPow(ll n, ll k/*,ll k*/) {
  48. if (k == 0)return 1;
  49.  
  50. ll res = fastPow(n, k / 2/*,k*/)/*%k*/;
  51.  
  52. res = (res * res)/*%k*/;
  53.  
  54. if (k & 1)res = (res * n)/*%k*/;
  55.  
  56. return res/*%k*/;
  57. }
  58.  
  59. ll fastPow(ll n, ll k, ll m) {
  60. if (k == 0)return 1;
  61.  
  62. ll res = fastPow(n, k / 2, m) % m;
  63.  
  64. res = (res * res) % m;
  65.  
  66. if (k & 1)res = (res * n) % m;
  67.  
  68. return res % m;
  69. }
  70.  
  71. ll calcMod(ll a, ll m) {
  72. return (a % m + m) % m;
  73. }
  74.  
  75. ll calcMod(ll a, ll b, ll m) {
  76. return ((a % m) * fastPow(b, m - 2, m)) % m;
  77. }
  78.  
  79. ll Ceil(ll n, ll m) {
  80. return (n + m - 1) / m;
  81. }
  82.  
  83. const ll mod = 998244353, N = 300000 + 5, M = 4 + 5;
  84.  
  85. /* Go little rockstar */
  86.  
  87.  
  88. bool ok(string s1){
  89. for(int i = 1; i < s1.size(); ++i){
  90. if(abs(s1[i] - s1[i - 1]) == 1)return 0;
  91. }
  92. return 1;
  93. }
  94.  
  95. void print(string s1, const vector<int> &fa){
  96. for(const auto&i : s1){
  97. cout << string(fa[i - 'a'], i);
  98. }
  99. cout << "\n";
  100. }
  101.  
  102. bool solve() {
  103. string s;
  104. cin >> s;
  105.  
  106. vector<int> fa(26);
  107. vector<char> c;
  108. for(int i = 0; i < s.size(); ++i){
  109. fa[s[i] - 'a']++;
  110. if(find(c.begin(), c.end(), s[i]) == c.end())c.pb(s[i]);
  111. }
  112. sort(c.begin(), c.end());
  113.  
  114.  
  115. string s1 = {c[c.size() / 2]};
  116. int l = c.size() / 2 - 2, r = c.size() / 2 + 2;
  117.  
  118. while(l >= 0 || r < c.size()){
  119. s1.pb(c[l--]);
  120. if(r < c.size())s1.pb(c[r++]);
  121. }
  122. if(c.size() / 2 - 1 >= 0)s1.pb(c[c.size() / 2 - 1]);
  123. if(c.size() / 2 + 1 < c.size())s1.pb(c[c.size() / 2 + 1]);
  124.  
  125. if(ok(s1)){
  126. print(s1, fa);
  127. }
  128. else {
  129. swap(s1[s1.size() - 1], s1[s1.size() - 2]);
  130. if(ok(s1)){
  131. print(s1, fa);
  132. }
  133. else cout << "No answer\n";
  134. }
  135. return 1;
  136. }
  137.  
  138. int main() {
  139. ios_base::sync_with_stdio(false);
  140. cin.tie(nullptr);
  141.  
  142. int t = 1;
  143. cin >> t;
  144. while (t--) {
  145. solve();
  146. }
  147. return 0;
  148. }
Runtime error #stdin #stdout 0.01s 5388KB
stdin
Standard input is empty
stdout
Standard output is empty