-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathD.cpp
121 lines (107 loc) · 1.54 KB
/
D.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n; cin>>n;
string s; cin>>s;
string ans;
int x=0,y=0;
int flagx=0,flagy=0;
for(int i=0;i<n;i++){
if(s[i]=='N'){
y++; flagy=1;
}
else if(s[i]=='S'){
y--; flagy=1;
}
else if(s[i]=='E'){
x++; flagx=1;
}
else{
x--; flagx=1;
}
}
if(x%2 || y%2){
cout<<"NO"<<endl; return;
}
if(x==0 && y==0 && n==2){
cout<<"NO"<<endl; return;
}
//Edge case
if(x==0 && y==0 && (flagx==0 || flagy==0)){
int ind=-1;
for(int i=0;i<n-1;i++){
if(s[i]!=s[i+1] && ind==-1){
ans.push_back('H'); ans.push_back('H');
ind=i; i++;
}
else{
ans.push_back('R');
}
}
if(ind!=n-2){
ans.push_back('R');
}
cout<<ans<<endl;
return;
}
//Main Logic
int tx=x/2,ty=y/2,xx=0,yy=1,cntx=0,cnty=0;
for(int i=0;i<n;i++){
if(s[i]=='N'){
if(yy){
ans.push_back('H');
cnty=1;
}
else{
ans.push_back('R');
}
ty--;
}
else if(s[i]=='S'){
if(yy){
ans.push_back('H');
cnty=1;
}
else{
ans.push_back('R');
}
ty++;
}
else if(s[i]=='E'){
if(xx){
ans.push_back('H');
}
else{
ans.push_back('R');
cntx=1;
}
tx--;
}
else{
if(xx){
ans.push_back('H');
}
else{
ans.push_back('R');
cntx=1;
}
tx++;
}
if(tx==0 && cntx){
cntx=0; xx=1;
}
else if(ty==0 && cnty){
cnty=0; yy=0;
}
}
cout<<ans<<endl;
}
int main()
{
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}