-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1974D.cpp
91 lines (83 loc) · 1.42 KB
/
1974D.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
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
for (int tn = 0; tn < t; tn++)
{
int n;
cin >> n;
string s;
cin >> s;
int x = 0, y = 0;
int nidx = -1, sidx = -1, eidx = -1, widx = -1;
for (int i = 0; i < s.length(); i++)
{
const auto & c = s[i];
if (c == 'N')
{
y++;
nidx = i;
}
else if (c == 'S')
{
y--;
sidx = i;
}
else if (c == 'E')
{
x++;
eidx = i;
}
else if (c == 'W')
{
x--;
widx = i;
}
}
if (x % 2 || y % 2)
cout << "NO";
else
{
int rx = 0, ry = 0;
x /= 2, y /= 2;
string p;
for (const auto & c : s)
{
bool r = false;
if (c == 'N' || c == 'S')
{
r = c == 'N' && ry < y || c == 'S' && ry > y;
if (r)
ry += c == 'N' ? 1 : -1;
}
else if (c == 'E' || c == 'W')
{
r = c == 'E' && rx < x || c == 'W' && rx > x;
if (r)
rx += c == 'E' ? 1 : -1;
}
p.push_back(r ? 'R' : 'H');
}
int r = count(p.begin(), p.end(), 'R');
if (r == 0 || r == p.length())
{
if (nidx >= 0 && sidx >= 0)
p[nidx] = p[sidx] = r ? 'H' : 'R';
else if (eidx >= 0 && widx >= 0)
p[eidx] = p[widx] = r ? 'H' : 'R';
}
r = count(p.begin(), p.end(), 'R');
if (r == 0 || r == p.length())
cout << "NO";
else
cout << p;
}
cout << endl;
}
return 0;
}