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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<vector> #include<queue> #include<stack> #include<map> #include<unordered_map> #include<set>
using namespace std; #define ll long long #define PII pair<int,int> #define PLL pair<ll,ll> #define PIII pair<int,PII> #define PLLL pair<ll,PLL> #define fi first #define se second #define pb push_back #define debug(a) cout << #a << " " << a << '\n'; const int N = 1e5 + 5; const int M = 1e5 + 5; const ll INF = 0x3f3f3f3f3f3f3f3f; const ll mod = 1e9 + 7;
ll mx, my;
bool tong(ll x, ll y) { if (x == 0 || y == 0)return true; if (x >= 0 && y >= 0)return true; if (x <= 0 && y <= 0)return true; return false;
}
void solve() { map<char, ll> mp; cin >> mx >> my; string path; cin >> path; int len = path.length(); for (int i = 0; i < len; i++) { if (path[i] == 'L')mp['L']++; else if (path[i] == 'R')mp['R']++; else if (path[i] == 'U')mp['U']++; else if (path[i] == 'D')mp['D']++; } ll x = mp['R'] - mp['L']; ll y = mp['U'] - mp['D']; if ((mx == x && my == y) || (mx == 0 && my == 0)) { cout << "Impossible\n"; } else if ((!mp['L'] && !mp['R']) && (tong(my, y) && abs(my) <= abs(y) && mx == 0)) { cout << "Impossible\n"; } else if ((!mp['U'] && !mp['D']) && (tong(mx, x) && abs(mx) <= abs(x) && my == 0)) { cout << "Impossible\n"; } else { if (x == 0) { string ans; if (mx > 0) { for (int i = 0; i < mp['L']; i++) ans += 'L'; if (my < 0) { for (int i = 0; i < mp['U']; i++) ans += 'U'; for (int i = 0; i < mp['D']; i++) ans += 'D'; } else { for (int i = 0; i < mp['D']; i++) ans += 'D'; for (int i = 0; i < mp['U']; i++) ans += 'U'; } for (int i = 0; i < mp['R']; i++) ans += 'R'; } else { for (int i = 0; i < mp['R']; i++) ans += 'R'; if (my < 0) { for (int i = 0; i < mp['U']; i++) ans += 'U'; for (int i = 0; i < mp['D']; i++) ans += 'D'; } else { for (int i = 0; i < mp['D']; i++) ans += 'D'; for (int i = 0; i < mp['U']; i++) ans += 'U'; } for (int i = 0; i < mp['L']; i++) ans += 'L'; } cout << ans << '\n'; return; } if (y == 0) { string ans; if (my > 0) { for (int i = 0; i < mp['D']; i++) ans += 'D'; if (mx < 0) { for (int i = 0; i < mp['R']; i++) ans += 'R'; for (int i = 0; i < mp['L']; i++) ans += 'L'; } else { for (int i = 0; i < mp['L']; i++) ans += 'L'; for (int i = 0; i < mp['R']; i++) ans += 'R'; } for (int i = 0; i < mp['U']; i++) ans += 'U'; } else { for (int i = 0; i < mp['U']; i++) ans += 'U'; if (mx < 0) { for (int i = 0; i < mp['R']; i++) ans += 'R'; for (int i = 0; i < mp['L']; i++) ans += 'L'; } else { for (int i = 0; i < mp['L']; i++) ans += 'L'; for (int i = 0; i < mp['R']; i++) ans += 'R'; } for (int i = 0; i < mp['D']; i++) ans += 'D'; } cout << ans << '\n'; return; } if ((my == 0 || mx == x)) { string ans; for (int i = 0; i < mp['U']; i++) ans += 'U'; for (int i = 0; i < mp['D']; i++) ans += 'D'; for (int i = 0; i < mp['L']; i++) ans += 'L'; for (int i = 0; i < mp['R']; i++) ans += 'R'; cout << ans << '\n'; } else { string ans; for (int i = 0; i < mp['L']; i++) ans += 'L'; for (int i = 0; i < mp['R']; i++) ans += 'R'; for (int i = 0; i < mp['U']; i++) ans += 'U'; for (int i = 0; i < mp['D']; i++) ans += 'D'; cout << ans << '\n'; } }
}
int main() { ios::sync_with_stdio(false); #ifdef LOCAL int begin_time = clock(); freopen("../input.txt", "r", stdin);
#endif int T = 1; cin >> T; for (int cas = 1; cas <= T; cas++) { #ifdef LOCAL printf("Case #%d: ", cas); #endif solve(); }
#ifdef LOCAL int end_time = clock(); printf("\nRun time: %.2lf ms", (double) (end_time - begin_time) / CLOCKS_PER_SEC * 1000); #endif
return 0; }
|