-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1272B.cpp
More file actions
47 lines (44 loc) · 1.22 KB
/
1272B.cpp
File metadata and controls
47 lines (44 loc) · 1.22 KB
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
string s;cin>>s;
int n = s.size();
vector<int> count(4,0);
for (int i = 0; i < n; i++)
{
if(s[i]=='L') count[0]++;
if(s[i]=='R') count[1]++;
if(s[i]=='U') count[2]++;
if(s[i]=='D') count[3]++;
}
int move_x=min(count[0],count[1]);
int move_y=min(count[3],count[2]);
if(move_x==0 && move_y>0) {
cout<<2<<'\n';
cout<<"UD\n";
}else if(move_y==0 && move_x>0) {
cout<<2<<'\n';
cout<<"LR\n";
} else if(move_x==0 && move_y==0) {
cout<<0<<'\n';
cout<<"\n";
}else {
string ans = "";
for (int i = 0; i < move_x; i++)
ans += 'L';
for (int i = 0; i < move_y; i++)
ans += 'U';
for (int i = 0; i < move_x; i++)
ans += 'R';
for (int i = 0; i < move_y; i++)
ans += 'D';
cout<< ans.size() <<'\n' << ans << '\n';
}
}
return 0;
}