fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <set>
  4. #include <algorithm>
  5. #include <cctype>
  6. #include <iomanip>
  7. #include <vector>
  8. #include <map>
  9. #include <queue>
  10. #include <numeric>
  11. #include <string>
  12. #include <cmath>
  13. #include <climits>
  14. #include <stack>
  15. #include <complex>
  16. using namespace std;
  17.  
  18. const int MOD = 7;
  19.  
  20. long invmod (long a,long m)
  21. {
  22. if (m==0) return 1;
  23. if (m%2)
  24. {
  25. long res = (invmod (a,(m-1)/2))%MOD ;
  26. return (a*res*res)%MOD;
  27. }
  28. else
  29. {
  30. long res=invmod(a,m/2)%MOD;
  31. return res*res%MOD;
  32. }
  33. }
  34.  
  35. long countdiv (long a)
  36. {
  37. long ans=0;
  38. long b=floor(sqrt(a));
  39. for (long i=1;i<=b;i++)
  40. {
  41. if ((a%i)==0) ans++;
  42. }
  43. ans*=2;
  44. if (b==sqrt(a)) ans--;
  45. return ans;
  46. }
  47.  
  48. long gcd(long long a,long long b)
  49. {
  50. if (a==0) return b;
  51. return gcd(b%a,a);
  52. }
  53.  
  54.  
  55. vector<long long> getDivisors(long long n) {
  56. vector<long long> divisors;
  57. for (long long i = 1; i * i <= n; ++i) {
  58. if (n % i == 0) {
  59. divisors.push_back(i);
  60. if (i != n / i) {
  61. divisors.push_back(n / i);
  62. }
  63. }
  64. }
  65. return divisors;
  66. }
  67.  
  68.  
  69.  
  70.  
  71. int main() {
  72. int t;
  73. cin >> t;
  74. while (t--) {
  75. int n;
  76. cin >> n;
  77. string s;
  78. cin >> s;
  79. map<char, int> mp;
  80. map<char, vector<int>> mp1;
  81. mp['N'] = 0;
  82. mp['W'] = 0;
  83. mp['S'] = 0;
  84. mp['E'] = 0;
  85.  
  86. for (int i = 0; i < n; i++) {
  87. mp[s[i]]++;
  88. mp1[s[i]].push_back(i);
  89. }
  90.  
  91. if ((mp['N'] + mp['S']) % 2 != 0 || (mp['W'] + mp['E']) % 2 != 0) {
  92. cout << "NO\n";
  93. } else {
  94. vector<char> ans(n, ' ');
  95. vector<int> r, h;
  96.  
  97. if (mp['S'] % 2 != 0) {
  98. mp['S']--;
  99. mp['N']--;
  100. r.push_back(mp1['S'].back());
  101. r.push_back(mp1['N'].back());
  102. mp1['S'].pop_back();
  103. mp1['N'].pop_back();
  104. }
  105. if (mp['E'] % 2 != 0) {
  106. mp['W']--;
  107. mp['E']--;
  108. h.push_back(mp1['E'].back());
  109. h.push_back(mp1['W'].back());
  110. mp1['E'].pop_back();
  111. mp1['W'].pop_back();
  112. }
  113.  
  114. while (mp['W'] > 0) {
  115. if (mp['W'] % 2 != 0) r.push_back(mp1['W'].back());
  116. else h.push_back(mp1['W'].back());
  117. mp1['W'].pop_back();
  118. mp['W']--;
  119. }
  120. while (mp['E'] > 0) {
  121. if (mp['E'] % 2 != 0) r.push_back(mp1['E'].back());
  122. else h.push_back(mp1['E'].back());
  123. mp1['E'].pop_back();
  124. mp['E']--;
  125. }
  126. while (mp['N'] > 0) {
  127. if (mp['N'] % 2 != 0) r.push_back(mp1['N'].back());
  128. else h.push_back(mp1['N'].back());
  129. mp1['N'].pop_back();
  130. mp['N']--;
  131. }
  132. while (mp['S'] > 0) {
  133. if (mp['S'] % 2 != 0) r.push_back(mp1['S'].back());
  134. else h.push_back(mp1['S'].back());
  135. mp1['S'].pop_back();
  136. mp['S']--;
  137. }
  138.  
  139. for (auto it : r) {
  140. ans[it] = 'R';
  141. }
  142. for (auto it : h) {
  143. ans[it] = 'H';
  144. }
  145.  
  146. if (r.empty() || h.empty()) {
  147. cout << "NO\n";
  148. } else {
  149. for (int i = 0; i < n; i++) {
  150. cout << ans[i];
  151. }
  152. cout << '\n';
  153. }
  154. }
  155. }
  156.  
  157. return 0;
  158. }
  159.  
  160.  
  161.  
  162.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty