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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <math.h>
#include <stdio.h>
#include <deque>
#include <unordered_map>
#include <cassert>
#include <numeric>
#include <queue>
#include <list>

#include <string>
#include <stack>

#include <time.h>
#include <iomanip>      // std::setfill, std::setw

#define LL long long

using namespace std;


const int UP = 'G', DOWN = 'D', LEFT = 'L', RIGHT = 'P';

struct Plansza {
    vector<vector<int> > rows;

    int EMPTY;

    Plansza(int emptyId) : EMPTY(emptyId) {}

    void printInt() {
        cout << "PlanszaInt:\n";
        for (int i=0; i < (int)rows.size(); i++)
        {
            for (int j=0; j < rows[0].size(); j++)
            {
                cout << rows[i][j] << std::setw (4) << " ";
            }
            cout << "\n";
        }
    }

    void print() {
        //cout << "Plansza:\n";
        for (int i=0; i < (int)rows.size(); i++)
        {
            string s(rows[i].begin(), rows[i].end());
            cout << s << "\n";
        }
    }

    void transform_vertically(int direction) {
         for (int c=0; c < (int)rows[0].size(); c++) {
            vector<int> compressed;
            for (int r=0; r < (int)rows.size(); r++) {
                if (rows[r][c] != EMPTY) compressed.push_back(rows[r][c]);
            }
            if (direction == DOWN) { 
                int idx = compressed.size() - 1;
                for (int r=rows.size() - 1; r >= 0; r--) {
                    rows[r][c] = idx >= 0 ? compressed[idx] : EMPTY;
                    idx--;
                }
            }
            else if (direction == UP) {
                int idx = 0;
                for (int r=0; r < (int)rows.size(); r++) {
                    rows[r][c] = idx < (int)compressed.size() ? compressed[idx] : EMPTY;
                    idx++;
                }
            }
        }
    }

    void transform_horizontally(int direction) {
        for (int r=0; r < (int)rows.size(); r++) {
            vector<int> compressed;
            for (int c=0; c < (int)rows[0].size(); c++) {
                if (rows[r][c] != EMPTY) compressed.push_back(rows[r][c]);
            }

            if (direction == RIGHT) { 
                int idx = compressed.size() - 1;
                for (int c=rows[0].size() - 1; c >= 0; c--) {
                    rows[r][c] = idx >= 0 ? compressed[idx] : EMPTY;
                    idx--;
                }
            }
            else if (direction == LEFT) {
                int idx = 0;
                for (int c=0; c < (int)rows[0].size(); c++) {
                    rows[r][c] = idx < (int)compressed.size() ? compressed[idx] : EMPTY;
                    idx++;
                }
            }
        }
    }

    void transform(int direction) {
        //cout << "Transform: " << string(1, char(direction)) << "\n";
        if (direction == RIGHT || direction == LEFT) {
            transform_horizontally(direction);
        } else {
            transform_vertically(direction);
        }
    }

    void multiTransform(const string &moves) {
        for (int i=0; i < (int)moves.size(); i++)
        {
            transform(moves[i]);
            //print();
        }
    }


    bool suffixMatch(const vector<int> &v, const string &suffix) {
        if (suffix.size() > v.size()) return false;
        int v_start = v.size() - suffix.size();
        for (int i=0; i < (int)suffix.size(); i++) {
            if (v[v_start + i] != suffix[i]) return false;
        }
        return true;
    }

    string reduce(string moves) {
        vector<int> L;
        assert(moves.size() > 0);
        
        for (int i=0; i < (int)moves.size(); i++) {
            int c = moves[i];
            L.push_back(c);
            vector<string> shortcutsKeepLast = {
                "LP", "PL", "DG", "GD",
                "LL", "PP", "DD", "GG",
            };
            for (auto shortcut : shortcutsKeepLast) {
                if (suffixMatch(L, shortcut)) {
                    for (int j=0; j < (int)shortcut.size(); j++) L.pop_back();
                    L.push_back(shortcut.back());
                }
            }
            vector<string> shortcutsKeepTwo = {
                "LDL", "LGL", "PDP", "PGP",
                 "GLG", "GPG", "DPD", "DLD",
            };
            for (auto shortcut : shortcutsKeepTwo) {
                if (suffixMatch(L, shortcut)) {
                    for (int j=0; j < (int)shortcut.size(); j++) L.pop_back();
                    L.push_back(shortcut[0]);
                    L.push_back(shortcut[1]);
                }
            }
        }    
        string res = string(L.begin(), L.end());
        //cout << "Transformed moves:\n" << res << "\n";
        return res;
    }
};

struct PlanszaMapping {
    Plansza plansza;
    vector<int> transition;
    vector<vector<int> > cycles;
    vector<pair<int, int> > cycleAssignment;
    const int CELLS_MAX = 512 * 512 + 10;
    
    PlanszaMapping(const Plansza &p) : plansza(p) {
        transition.resize(CELLS_MAX, -1);
     }
    
    int fromRowCol(int r, int c) {
        return (r << 9) + c;
    }

    pair<int, int> toRowCol(int cellId) {
        int c = (cellId & 511);
        int r = (cellId >> 9) & 511;
        return make_pair(r,c);
    }

    void fullRotation(string moves) {
        assert (moves.size() == 4);
        // remap cells to unique integers
        for (int r=0; r < (int)plansza.rows.size(); r++) {
            for (int c=0; c < (int)plansza.rows[0].size(); c++) {
                int &cell = plansza.rows[r][c];
                if (cell == plansza.EMPTY) {
                    cell = -1;
                } else {
                    cell = fromRowCol(r,c);;
                }
            }
        }
        plansza.EMPTY = -1;
        //plansza.printInt();
        for (auto &m : moves) {
            plansza.transform(m);
        }
        //plansza.printInt();
        
        for (int r=0; r < (int)plansza.rows.size(); r++) {
            for (int c=0; c < (int)plansza.rows[0].size(); c++) {
                int &cell = plansza.rows[r][c];
                int originalCellId = fromRowCol(r,c);
                transition[originalCellId] = cell;
            }
        }
        //printTransition();

        calculateCycles();
    }

    void calculateCycles() {
        vector<bool> seen(CELLS_MAX, false);
        cycleAssignment.resize(CELLS_MAX, {-1,-1});
        for (int r=0; r < (int)plansza.rows.size(); r++) {
            for (int c=0; c < (int)plansza.rows[0].size(); c++) {
                int originalCellId = fromRowCol(r,c);
                if (transition[originalCellId] == -1) continue;
                if (seen[originalCellId]) continue;
                vector<int> currentCycle = {originalCellId};
                cycleAssignment[originalCellId] = {this->cycles.size(), 0};
                seen[originalCellId] = true;
                int x = originalCellId;
                while (transition[x] != originalCellId) {
                    x = transition[x];
                    assert (x >= 0 && x < CELLS_MAX);
                    currentCycle.push_back(x);
                    cycleAssignment[x] = {this->cycles.size(), currentCycle.size() - 1};
                    seen[x] = true;

                }
                this->cycles.push_back(currentCycle);
            }
        }
    }

    void printTransition() {
        for (int cellId=0; cellId < (int)transition.size(); cellId++) {
            if (transition[cellId] != -1) {
                pair<int,int> before = toRowCol(cellId);
                pair<int,int> after = toRowCol(transition[cellId]);
                cout << "(" << before.first << "," << before.second << ") -> (" << after.first << "," << after.second << ")\n";
            }
        }
        /*for (int i=0; i < (int)cycles.size(); i++)
        {
            printf("Cycle %d\n", i);
            for (int j=0; j < (int)cycles[i].size(); j++)
            {
                auto val = toRowCol( cycles[i][j]);
                printf("(%d,%d) ", val.first, val.second);
            }
            printf("\n");
        }*/
    }

    Plansza applyTransition(const Plansza &originalPlansza, int count) {
        Plansza afterPlansza('.');
        vector<int> emptyRow(originalPlansza.rows[0].size(), '.');
        for (int r=0; r < (int)plansza.rows.size(); r++) {
            afterPlansza.rows.push_back(emptyRow);
        }
        for (int r=0; r < (int)plansza.rows.size(); r++) {
            for (int c=0; c < (int)plansza.rows[0].size(); c++) {
                int originalCellId = fromRowCol(r,c);
                // Wiemy, na ktorym cyklu i na ktorej pozycji lezy ta komorka
                pair<int, int> cyclePos = cycleAssignment[originalCellId];
                if (cyclePos.first == -1) continue;
                int color = originalPlansza.rows[r][c];
                assert (cyclePos.first >= 0 && cyclePos.first < cycles.size());
                int rozmiarCyklu = cycles[cyclePos.first].size();
                int posAfter = (cyclePos.second - count % rozmiarCyklu + rozmiarCyklu) % rozmiarCyklu;
                assert (posAfter >= 0 && posAfter < cycles[cyclePos.first].size());
                int destinationCellId = cycles[cyclePos.first][posAfter];
                pair<int,int> destinationRowColl = toRowCol(destinationCellId);
                assert (destinationRowColl.first < afterPlansza.rows.size());
                assert (destinationRowColl.second < afterPlansza.rows[0].size());
                afterPlansza.rows[destinationRowColl.first][destinationRowColl.second] = color;
            }
        }
        return afterPlansza;
    }
};

string gen(int length) {
    string s;
    time_t nTime;
    srand((unsigned) time(&nTime));

    for (int i=0; i < length; i++) {
        int x = rand() % 4;
        char c = x == 0 ? 'G' : (x == 1 ? 'D' : (x == 2 ? 'L' : 'P'));
        s.push_back(c);
    }
    return s;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);

    int rows, cols;
    bool FASTER = true;

    cin >> rows >> cols;
    Plansza p('.');
    for (int i=0; i < rows; i++)
    {
        string s;
        cin >> s;
        p.rows.push_back(vector<int>(s.begin(), s.end()));
    }
    int k;
    string moves;
    cin >> k;
    cin >> moves;
    for (int i=0; i < 1; i++) {
        if (FASTER) {
            moves = p.reduce(moves);
        }
        
    }
    
    for (int i=0; i < (int)moves.size(); i++)
    {
        int movesLeft = moves.size() - i;
        if (FASTER && i > 8 && movesLeft > 4 && movesLeft % 4 == 0) {
            // cout << "movesLeft:" << movesLeft;
            string cycle = moves.substr(i, 4);
            PlanszaMapping planszaMapping(p);
            planszaMapping.fullRotation(cycle);
            Plansza p2 = planszaMapping.applyTransition(p, movesLeft / 4);
            p = p2;
            break;
        }
        p.transform(moves[i]);
        
        //cout << "Move=" << moves[i] << "\n";
    }
    p.print();

    return 0;
}