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
#include <bits/stdc++.h>

#define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

using namespace std;

const int LUCKY_NUM = 13;
const int MAX_K = 5e5 + LUCKY_NUM;
const int MAX_N_M = 5e2 + LUCKY_NUM;

int n, m, k, cycleLen, it;
string board[MAX_N_M];

string firstCanonicalState;
string currentBoardStr, tmpStr;

string moves, simplifiedMoves;


void getColStr(int col) {
    tmpStr.clear();
    for (int row = 1; row <= n; row++) {
        if (board[row][col] != '.') {
            tmpStr.push_back(board[row][col]);
        }
    }
}

void getRowStr(int row) {
    tmpStr.clear();
    for (int col = 0; col < m; col++) {
        if (board[row][col] != '.') {
            tmpStr.push_back(board[row][col]);
        }
    }
}

void moveColumnUp(int col) {
    getColStr(col);
    for (int row = 1; row <= tmpStr.size(); row++) {
        board[row][col] = tmpStr[row - 1];
    }
    for (int row = (int) tmpStr.size() + 1; row <= n; row++) {
        board[row][col] = '.';
    }
}

void moveColumnDown(int col) {
    getColStr(col);
    int blankCount = n - (int) tmpStr.size();
    for (int row = 1; row <= blankCount; row++) {
        board[row][col] = '.';
    }
    for (int row = blankCount + 1; row <= n; row++) {
        board[row][col] = tmpStr[row - blankCount - 1];
    }
}

void moveRowLeft(int row) {
    getRowStr(row);
    for (int col = 0; col < tmpStr.size(); col++) {
        board[row][col] = tmpStr[col];
    }
    for (int col = (int) tmpStr.size(); col < m; col++) {
        board[row][col] = '.';
    }
}

void moveRowRight(int row) {
    getRowStr(row);
    int blankCount = m - (int) tmpStr.size();
    for (int col = 0; col < blankCount; col++) {
        board[row][col] = '.';
    }
    for (int col = blankCount; col < m; col++) {
        board[row][col] = tmpStr[col - blankCount];
    }
}

void moveUp() {
    for (int col = 0; col < m; col++) {
        moveColumnUp(col);
    }
}

void moveDown() {
    for (int col = 0; col < m; col++) {
        moveColumnDown(col);
    }
}

void moveLeft() {
    for (int row = 1; row <= n; row++) {
        moveRowLeft(row);
    }
}

void moveRight() {
    for (int row = 1; row <= n; row++) {
        moveRowRight(row);
    }
}

void performMove(char move) {
    switch (move) {
        case 'G':
            return moveUp();
        case 'D':
            return moveDown();
        case 'L':
            return moveLeft();
        default:
            return moveRight();
    }
}

void printBoard() {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cout << board[i][j - 1];
        }
        cout << endl;
    }
}

void convertBoardToStr() {
    currentBoardStr.clear();
    for (int i = 1; i <= n; i++) {
        currentBoardStr.append(board[i]);
    }
}

bool oneOfLastTwoIs(char c) {
    return (simplifiedMoves.back() == c) ||
           (simplifiedMoves.size() >= 2 && simplifiedMoves[simplifiedMoves.size() - 2] == c);
}


char oppositeMove(char c) {
    switch (c) {
        case ('G'):
            return 'D';
        case ('D'):
            return 'G';
        case ('L'):
            return 'P';
        default:
            return 'L';
    }
}

bool lastIsOpposite(char c) {
    return simplifiedMoves.back() == oppositeMove(c);
}

void handle(char c) {
    while (!simplifiedMoves.empty()) {
        if (oneOfLastTwoIs(c)) return;
        if (lastIsOpposite(c)) {
            simplifiedMoves.pop_back();
        } else {
            break;
        }
    }
    simplifiedMoves.push_back(c);
}

void tryToSimplify(char c) {
    switch (c) {
        case ('G'):
            return handle('G');
        case ('D'):
            return handle('D');
        case ('L'):
            return handle('L');
        default:
            return handle('P');
    }
}


void readInput() {
    boost;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> board[i];
    }
    cin >> k;
    cin >> moves;
}

void simplifyMoves() {
    simplifiedMoves.push_back(moves[0]);
    for (int i = 1; i < moves.size(); i++) {
        tryToSimplify(moves[i]);
    }
}

void solve() {
    simplifyMoves();

    performMove(simplifiedMoves[0]);
    if (simplifiedMoves.size() == 1) return;
    performMove(simplifiedMoves[1]);
    convertBoardToStr();
    firstCanonicalState = currentBoardStr;

    for (it = 2; it < simplifiedMoves.size(); it++) {
        performMove(simplifiedMoves[it]);
        convertBoardToStr();
        if (firstCanonicalState == currentBoardStr) {
            break;
        }
    }

    cycleLen = it - 1;
    while (it + cycleLen < simplifiedMoves.size()) {
        it += cycleLen;
    }
    for (int i = it + 1; i < simplifiedMoves.size(); i++) {
        performMove(simplifiedMoves[i]);
    }
}

void printResult() {
    printBoard();
}

int32_t main() {
    readInput();
    solve();
    printResult();
}

/*

4 5
.....
.B.C.
..C..
...B.
3
GLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDPGLDP



 */