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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#include <algorithm>
#include <iostream>
#include <vector>
#include <cstdint>
#include <list>
#include <unistd.h>

//#define DEBUG

template<class T>
void
simulate_shift(char dir, const std::vector<std::vector<T>> &from, std::vector<std::vector<T>> &to, const T &zero)
{
	int w = from.size();
	int h = from[0].size();
	switch (dir) {
		case 'G': {
			for (int x = 0; x < w; ++x) {
				int y2 = 0;
				for (int y = 0; y < h; ++y) {
					const T &c = from[x][y];
					if (c != zero) {
						to[x][y2++] = c;
					}
				}
				for (; y2 < h; ++y2) {
					to[x][y2] = zero;
				}
			}
		} break;
		case 'D': {
			for (int x = 0; x < w; ++x) {
				int y2 = h-1;
				for (int y = h-1; y >= 0; --y) {
					const T &c = from[x][y];
					if (c != zero) {
						to[x][y2--] = c;
					}
				}
				for (; y2 >= 0; --y2) {
					to[x][y2] = zero;
				}
			}
		} break;
		case 'L': {
			for (int y = 0; y < h; ++y) {
				int x2 = 0;
				for (int x = 0; x < w; ++x) {
					const T &c = from[x][y];
					if (c != zero) {
						to[x2++][y] = c;
					}
				}
				for (; x2 < w; ++x2) {
					to[x2][y] = zero;
				}
			}
		} break;
		case 'P': {
			for (int y = 0; y < h; ++y) {
				int x2 = w-1;
				for (int x = w-1; x >= 0; --x) {
					const T &c = from[x][y];
					if (c != zero) {
						to[x2--][y] = c;
					}
				}
				for (; x2 >= 0; --x2) {
					to[x2][y] = zero;
				}
			}
		} break;
	}
}

void
quickpow(const std::vector<int> &perm, std::vector<int> &out, int ex)
{
	int n = perm.size();
	if (ex == 0) {
		for (int i = 0; i < n; ++i) {
			out[i] = i;
		}
	} else if (ex == 1) {
		for (int i = 0; i < n; ++i) {
			out[i] = perm[i];
		}
	} else if (ex % 2 == 0) {
		std::vector<int> tmp(n);
		quickpow(perm, tmp, ex/2);
		for (int i = 0; i < n; ++i) {
			out[i] = tmp[tmp[i]];
		}
	} else {
//		if (ex <= 2) {
//			std::cout << "DUUUUUUUUUUUUUUPA " << ex << '\n';
//		}
		std::vector<int> tmp(n);
		quickpow(perm, tmp, ex-1);
		for (int i = 0; i < n; ++i) {
			out[i] = tmp[perm[i]]; // order of application dont matter here
		}
	}
}

void
print_board(const std::vector<std::vector<char>> &board)
{
	int w = board.size();
	int h = board[0].size();
	for (int y = 0; y < h; ++y) {
		for (int x = 0; x < w; ++x) {
			std::cout << board[x][y];
		}
		std::cout << '\n';
	}
}

int main() {
#ifndef DEBUG
	std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0);
#endif //DEBUG
	
	int w, h;
	std::cin >> h >> w;
	
	std::string str;
	
	std::vector<std::vector<char>> board1(w, std::vector<char>(h));
	std::vector<std::vector<char>> board2(w, std::vector<char>(h));
	for (int y = 0; y < h; ++y) {
		std::cin >> str;
		for (int x = 0; x < w; ++x) {
			board1[x][y] = str[x];
		}
	}
	
	int Q;
	std::cin >> Q;
	
	std::cin >> str;
	
	std::string str2;
	str2.reserve(Q);
	str2.push_back(str[0]);
	for (int i = 1; i < Q; ++i) {
		char c = str[i];
		char pc = str2.back();
		if (c == pc) continue;
		if (
			(pc == 'L' && c == 'P') ||
			(pc == 'P' && c == 'L') ||
			(pc == 'D' && c == 'G') ||
			(pc == 'G' && c == 'D')
		) {
			str2.back() = c;
		} else {
			str2.push_back(c);
		}
	}
	std::swap(str2, str);
	Q = str.size();
	
//	std::cout << "str changed to: ";
//	for (auto x : str) std::cout << x << ' ';
//	std::cout << '\n';
	
	simulate_shift(str[0], board1, board2, '.');
	if (Q == 1) {
		print_board(board2);
		return 0;
	}
	simulate_shift(str[1], board2, board1, '.');
	if (Q == 2) {
		print_board(board1);
		return 0;
	}
	
//	std::cout << "after the special 2 first moves:\n";
//	print_board(board1);
	
	std::list<char> moves;
	for (int i = 2; i < Q; ++i) moves.push_back(str[i]);
	
	auto it = std::prev(moves.end());
	while (1) {
		
//		std::cout <<"dupa\n";
		// sleep(1);
//		for (auto iter = moves.begin(); iter != moves.end(); ++iter) {
//			std::cout << *iter;
//			if (iter == it) std::cout << '<';
//			else std::cout << ' ';
//		}
//		std::cout << '\n';
		
		if (it == moves.begin()) break;
		auto previt = std::prev(it);
		if (previt == moves.begin()) break;
		if (*previt == *it) {
			moves.erase(previt);
			if (std::next(it) != moves.end()) {
				it = std::next(it);
//				std::cout << "Dupa ";
			}
			continue;
		}
		if (
			(*previt == 'L' && *it == 'P') ||
			(*previt == 'P' && *it == 'L') ||
			(*previt == 'D' && *it == 'G') ||
			(*previt == 'G' && *it == 'D')
		) {
//			std::cout << "dupa ";
			moves.erase(previt);
			if (std::next(it) != moves.end()) {
				it = std::next(it);
//				std::cout << "Dupa ";
			}
//			if (std::next(it) != moves.end()) {
//				it = std::next(it);
//				std::cout << "DUPA ";
//			}
//			std::cout << '\n';
			continue;
		}
		auto prait = std::prev(previt);
		if (prait == moves.begin()) break;
		if (*prait == *it) {
			auto to_erase = it;
			if (std::next(it) != moves.end()) {
				it = std::next(it);
				if (std::next(it) != moves.end()) it = std::next(it);
			} else {
				it = previt;
			}
			moves.erase(to_erase);
			continue;
		}
		it = previt;
	}
	
//	for (auto x : moves) std::cout << x << ' ';
//	std::cout << '\n';
	
	if (moves.size() < 10) {
		for (char move : moves) {
			simulate_shift(move, board1, board2, '.');
			std::swap(board1, board2);
		}
		print_board(board1);
		return 0;
	}
	
	char cyc[4];
	{
		auto iter = std::prev(moves.end());
		cyc[3] = *iter;
		--iter;
		cyc[2] = *iter;
		--iter;
		cyc[1] = *iter;
		--iter;
		cyc[0] = *iter;
	}
	
//	std::cout << "cycle: " << cyc[0] << cyc[1] << cyc[2] << cyc[3] << '\n';
	
	{
		auto iter = moves.begin();
		int initial = moves.size() % 4 + 4;
		for (int i = 0; i < initial; ++i) {
			simulate_shift(*iter, board1, board2, '.');
			std::swap(board1, board2);
			++iter;
		}
		std::vector<std::vector<std::pair<short,short>>> rotboard1(w, std::vector<std::pair<short,short>>(h));
		std::vector<std::vector<std::pair<short,short>>> rotboard2(w, std::vector<std::pair<short,short>>(h));
		for (int x = 0; x < w; ++x) {
			for (int y = 0; y < h; ++y) {
				if (board1[x][y] == '.') {
					rotboard1[x][y] = {-1, -1};
				} else {
					rotboard1[x][y] = {x, y};
				}
			}
		}
		for (char c : cyc) {
			simulate_shift(c, rotboard1, rotboard2, {-1, -1});
			std::swap(rotboard1, rotboard2);
		}
		std::vector<int> permutation(w*h);
		for (int x = 0; x < w; ++x) {
			for (int y = 0; y < h; ++y) {
				auto [xx, yy] = rotboard1[x][y];
				if (xx < 0) {
					permutation[y*w+x] = y*w+x;
				} else {
//					std::cout << x << "," << y << " ";
					permutation[yy*w+xx] = y*w+x;
				}
			}
		}
		int exponent = (moves.size() - initial)/4;
//		std::cout << initial + 4*exponent << " vs " << moves.size() << '\n';
		//std::vector<int> temperm(w*h);
		std::vector<int> out(w*h);
//		for (int i = w*h-10; i < w*h; ++i) std::cout << permutation[i] << ' ';
//		std::cout << "dUpA\n";
//		std::cout << "\n\n\n\n";
//		print_board()
//		std::cout << "\n\n\n\n";
		quickpow(permutation, out, exponent);
//		std::cout << "\n\n\n\n";
		for (int x = 0; x < w; ++x) {
			for (int y = 0; y < h; ++y) {
				int foo = out[y*w+x];
				int xx = foo%w;
				int yy = foo/w;
				board2[xx][yy] = board1[x][y];
			}
		}
		print_board(board2);
	}
	
	/*
	std::vector<char> moves;
	moves.push_back(str[2]);
	for (int i = 3; i < Q; ++i) {
		if (str[i] == moves.back()) continue;
		while (
			!moves.empty() && (
				(str[i] == 'L' && moves.back() == 'P') ||
				(str[i] == 'P' && moves.back() == 'L') ||
				(str[i] == 'D' && moves.back() == 'G') ||
				(str[i] == 'G' && moves.back() == 'D')
			)
		) {
			moves.pop_back();
		}
		if (moves.empty()) {
			moves.push_back(str[i]);
			continue;
		}
		
	}
	*/
	
	
	/*
	std::vector<char> moves;
	moves.reserve(str.size());
	moves.push_back(str[0]);
	for (int i = 1; i < Q; ++i) {
		char c = str[i];
		char prev = moves.back();
		if (c == 'G' || c == 'D') {
			if (prev == 'G' || prev == 'D') {
				moves.back() = c;
			} else {
				moves.push_back(c);
			}
		} else {
			if (prev == 'L' || prev == 'P') {
				moves.back() = c;
			} else {
				moves.push_back(c);
			}
		}
	}
	int M = moves.size();
	
#ifdef DEBUG
	std::cout << "moves[]:";
	for (auto &x : moves) std::cout << ' ' << x;
	std::cout << '\n';
#endif //DEBUG
	
	std::vector<char> lrs;
	lrs.reserve(M);
	lrs.push_back('x');
	lrs.push_back('d');
	char cur_bot = moves[1];
	for (int i = 2; i < M; ++i) {
		switch (cur_bot) {
			case 'G': {
				switch (moves[i]) {
					case 'L': lrs.push_back('R'); break;
					case 'P': lrs.push_back('L'); break;
				}
			} break;
			case 'D': {
				switch (moves[i]) {
					case 'L': lrs.push_back('L'); break;
					case 'P': lrs.push_back('R'); break;
				}
			} break;
			case 'L': {
				switch (moves[i]) {
					case 'G': lrs.push_back('L'); break;
					case 'D': lrs.push_back('R'); break;
				}
			} break;
			case 'P': {
				switch (moves[i]) {
					case 'G': lrs.push_back('R'); break;
					case 'D': lrs.push_back('L'); break;
				}
			} break;
		}
		cur_bot = moves[i];
	}
	
#ifdef DEBUG
	std::cout << "lrs[]:";
	for (auto &x : lrs) std::cout << ' ' << x;
	std::cout << '\n';
#endif //DEBUG
	*/
	
	return 0;
}