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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <iostream>
#include <cstdio>
#include <string>
#include <sstream> 
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <ctime>
#include <cassert>
#include <deque>
using namespace std;

#define assert_range(x,a,b) assert((a) <= (x) and (x) <= (b))
using ll = long long;
using TableInt = vector<vector<int>>;
using Table = vector<string>;
using Vi = vector<int>;
using Vii = vector<pair<int, int>>;
const int INF = 1e9;

const int MAX_N = 500;
const int DOT_INT = -1;
const char DOT = '.';
const char LEFT = 'L';
const char RIGHT = 'P';
const char DOWN = 'D';
const char UP = 'G';
const int VERTICAL = 0;
const int HORIZONTAL = 1;

Table a(MAX_N);
Table b(MAX_N);

int n, m;


Vii get_non_empty_indices(const Table& a) {
    Vii res;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (a[i][j] != DOT) {
                res.push_back({i, j});
            }
        }
    }
    return res;
}

TableInt create_int_table(const Table& a) {
    TableInt res(n, Vi(m, DOT_INT));
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (a[i][j] != DOT) {
                res[i][j] = cur;
                ++cur;
            }
        }
    }
    return res;
}

Vi get_perm_from_tables(const TableInt& a, const TableInt& b) {
    Vi p;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (a[i][j] != DOT_INT) {
                p.push_back(b[i][j]);
            }
        }
    }
    return p;
}

Vi mul_perms(const Vi& a, const Vi& b) {
    Vi p(a.size());
    for (int i = 0; i < a.size(); ++i) {
        p[i] = b[a[i]];
    }
    return p;
}

Vi perm_pow(const Vi& p, int k) {
    if (k == 1) {
        return Vi(p.begin(), p.end());
    }
    if (k % 2 == 0) {
        auto c = perm_pow(p, k / 2);
        return mul_perms(c, c);
    }
    return mul_perms(perm_pow(p, k-1), p);
}

int move_type(char move_dir) {
    return (move_dir == UP || move_dir == DOWN) ? VERTICAL : HORIZONTAL;
}


void print_board() {
    for (int i = 0; i < n; ++i) {
        cout << a[i] << endl;
    }
}

void make_move(Table& a, char move_dir) {
    if (move_dir == LEFT) {
        for (int i = 0; i < n; ++i) {
            int n_dots = 0;
            for (int j = 0; j < m; ++j) {
                if (a[i][j] == DOT) ++n_dots; 
                else a[i][j-n_dots] = a[i][j];
            }
            for (int j = 0; j < n_dots; ++j) {
                a[i][m-1-j] = DOT;
            }
        }
    } else if (move_dir == RIGHT) {
        for (int i = 0; i < n; ++i) {
            int n_dots = 0;
            for (int j = m-1; j >= 0; --j) {
                if (a[i][j] == DOT) ++n_dots; 
                else a[i][j+n_dots] = a[i][j];
            }
            for (int j = 0; j < n_dots; ++j) {
                a[i][j] = DOT;
            }
        }
    } else if (move_dir == UP) {
        for (int j = 0; j < m; ++j) {
            int n_dots = 0;
            for (int i = 0; i < n; ++i) {
                if (a[i][j] == DOT) ++n_dots;
                else a[i-n_dots][j] = a[i][j];
            }
            for (int i = 0; i < n_dots; ++i) {
                a[n-1-i][j] = DOT;
            }
        }
    } else if (move_dir == DOWN) {
        for (int j = 0; j < m; ++j) {
            int n_dots = 0;
            for (int i = n-1; i >= 0; --i) {
                if (a[i][j] == DOT) ++n_dots;
                else a[i+n_dots][j] = a[i][j];
            }
            for (int i = 0; i < n_dots; ++i) {
                a[i][j] = DOT;
            }
        }

    } else assert(false);
}

void make_int_move(TableInt& a, char move_dir) {
    if (move_dir == LEFT) {
        for (int i = 0; i < n; ++i) {
            int n_dots = 0;
            for (int j = 0; j < m; ++j) {
                if (a[i][j] == DOT_INT) ++n_dots; 
                else a[i][j-n_dots] = a[i][j];
            }
            for (int j = 0; j < n_dots; ++j) {
                a[i][m-1-j] = DOT_INT;
            }
        }
    } else if (move_dir == RIGHT) {
        for (int i = 0; i < n; ++i) {
            int n_dots = 0;
            for (int j = m-1; j >= 0; --j) {
                if (a[i][j] == DOT_INT) ++n_dots; 
                else a[i][j+n_dots] = a[i][j];
            }
            for (int j = 0; j < n_dots; ++j) {
                a[i][j] = DOT_INT;
            }
        }
    } else if (move_dir == UP) {
        for (int j = 0; j < m; ++j) {
            int n_dots = 0;
            for (int i = 0; i < n; ++i) {
                if (a[i][j] == DOT_INT) ++n_dots;
                else a[i-n_dots][j] = a[i][j];
            }
            for (int i = 0; i < n_dots; ++i) {
                a[n-1-i][j] = DOT_INT;
            }
        }
    } else if (move_dir == DOWN) {
        for (int j = 0; j < m; ++j) {
            int n_dots = 0;
            for (int i = n-1; i >= 0; --i) {
                if (a[i][j] == DOT_INT) ++n_dots;
                else a[i+n_dots][j] = a[i][j];
            }
            for (int i = 0; i < n_dots; ++i) {
                a[i][j] = DOT_INT;
            }
        }

    } else assert(false);
}


int main() {
    ios_base::sync_with_stdio(0);
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    int k;
    cin >> k;
    string moves_str;
    cin >> moves_str;
    deque<char> moves(moves_str.begin(), moves_str.end());
    deque<char> reduced_moves;
    int cur = 0;
    while (cur < k) {
        while (cur+1 < k && move_type(moves[cur+1]) == move_type(moves[cur])) {
            cur++;
        }
        reduced_moves.push_back(moves[cur]);
        cur++;
    }
    moves = reduced_moves;

    if (moves.size() == 1) {
        make_move(a, moves[0]);
    } else {
        assert(move_type(moves[0]) != move_type(moves[1]));

        deque<char> final_moves = {moves[0], moves[1]};

        vector<vector<int>> occ(2);
        occ[move_type(final_moves[0])] = {0};
        occ[move_type(final_moves[1])] = {1};

        for (int i = 2; i < moves.size(); ++i) {
            if (moves[i] == final_moves.back()) {
                continue;
            }

            auto m_type = move_type(moves[i]);
            if (m_type == move_type(final_moves.back())) {
                final_moves.pop_back();
                occ[m_type].pop_back();
                if (occ[m_type].empty() || moves[i] != final_moves[occ[m_type].back()]) {
                    final_moves.push_back(moves[i]);
                    occ[m_type].push_back(final_moves.size()-1);
                }
            } else if (moves[i] != final_moves[occ[m_type].back()]) {
                final_moves.push_back(moves[i]);
                occ[m_type].push_back(final_moves.size()-1);
            }
        }

        moves = final_moves;

        // always make first two moves
        for (int i = 0; i < 2; ++i) {
            make_move(a, moves[0]);
            moves.pop_front();
        }

        if (moves.size() >= 4) {
            int d = moves.size() / 4;
            int r = moves.size() % 4;

            auto indices = get_non_empty_indices(a);
            TableInt t1 = create_int_table(a);
            TableInt t2 = t1;
            for (int i = 0; i < 4; ++i) {
                make_int_move(t2, moves[i]);
            }
            auto p = get_perm_from_tables(t1, t2);
            p = perm_pow(p, d);

            for (int i = 0; i < n; ++i) {
                b[i] = string(m, DOT);
            }
            for (int idx = 0; idx < p.size(); ++idx) {
                pair<int, int> ix = indices[idx];
                pair<int, int> iy = indices[p[idx]];
                b[ix.first][ix.second] = a[iy.first][iy.second];
            }
            a = b;
            while (moves.size() > r) {
                moves.pop_front();
            }
        }
        for (int i = 0; i < moves.size(); ++i) {
            make_move(a, moves[i]);
        }
    }

    print_board();

    return 0;
}