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
#include <cstdio>
#include <cassert>
#include <cstring>
#include <cstdint>
#include <algorithm>
#include <string>

using namespace std;

const int max_size = 502;

int n,m,k;

char t[max_size][max_size];
string mov;

char states[12][4] = {
// 0 - L
// 1 - R
// 2 - G
// 3 - D
      //    L,  R,  G,  D
/*  0 */ {  0,  1, 11,  0 },
/*  1 */ {  0,  1,  2,  1 },
/*  2 */ {  3,  2,  2,  1 },
/*  3 */ {  3,  2,  3,  4 },
/*  4 */ {  4,  5,  3,  4 },
/*  5 */ {  4,  5,  6,  5 },
/*  6 */ {  7,  6,  6,  5 },
/*  7 */ {  7,  6,  7,  8 },
/*  8 */ {  8,  9,  7,  8 },
/*  9 */ {  8,  9, 10,  9 },
/* 10 */ { 11, 10, 10,  9 },
/* 11 */ { 11, 10, 10,  0 },
};

int dir_val(char c)
{
	switch(c) {
		case 'L': return 0;
		case 'P': return 1;
		case 'G': return 2;
		case 'D': return 3;
	}
	assert(false);
	return -1;
}

void print()
{
	for(int i = 0; i < n; ++i) {
		for(int j = 0; j < m; ++j) {
			printf("%c", t[i][j]);
		}
		printf("\n");
	}
}

void printmov(string m)
{
        printf("%s\n", m.c_str());
}

string find_move()
{
	string fmov;
	char lk1, lk2;
	int i;
	int st;

	fmov.push_back(mov[0]);
	if (mov[0] == 'L' || mov[0] == 'P') {
		lk1 = 'G';
		lk2 = 'D';
	} else {
		lk1 = 'L';
		lk2 = 'P';
	}
	
	i = 1;
	while(i < k && mov[i] != lk1 && mov[i] != lk2) {
		fmov[0] = mov[i];
		++i;
	}

//	printf("i=%d, c=%c\n", i, mov[i]);

	if (i == k) {
		return fmov;
	}
	fmov.push_back(mov[i]);
	++i;
	if (i == k) {
		return fmov;
	}

	if (k - i < 20) {
		while(i < k) {
			fmov.push_back(mov[i]);
			++i;
		}
		return fmov;
	}

	if (fmov[0] == 'L') {
		switch(fmov[1]) {
			case 'D': st = 4; break;
			case 'G': st = 3; break;
		}
	}
	if (fmov[0] == 'P') {
		switch(fmov[1]) {
			case 'D': st = 1; break;
			case 'G': st = 2; break;
		}
	}
	if (fmov[0] == 'D') {
		switch(fmov[1]) {
			case 'L': st = 0; break;
			case 'P': st = 1; break;
		}
	}
	if (fmov[0] == 'G') {
		switch(fmov[1]) {
			case 'L': st = 3; break;
			case 'P': st = 2; break;
		}
	}
	
	int fs = st;
	//printf("first state %d\n", st);

	while(i < k) {
		int v = dir_val(mov[i]);
		st = states[st][v];
		++i;
	}

	//printf("last state %d\n", st);

	const std::string s0 = "PGLD";
	const std::string s1 = "GLDP";
	const std::string s2 = "LDPG";
	const std::string s3 = "DPGL";

	const std::string *s;
	switch(fs) {
		case 0: s = &s0; break; 
		case 1: s = &s1; break;
		case 2: s = &s2; break;
		case 3: s = &s3; break;
		case 4: s = &s0; break;
	}

	int ls = st;
	st = fs;
	i = 0;
	while(1) {
		if (i == 4) {
			i = 0;
		}
		char c = s->at(i);
		int v = dir_val(c);
		st = states[st][v];
		fmov.push_back(c);
		//printf("dir %c,  st %d\n", c, st);
		if (st == ls) {
			break;
		}
		++i;
	}

	return fmov;
}

void do_move_left()
{
	for(int i = 0; i < n; ++i) {
		int ja = 0, jb;
		while(ja < m && t[i][ja] != '.') ++ja;
		if (ja == m) continue;
		jb = ja+1;
		while(1) {
			while(jb < m && t[i][jb] == '.') ++jb;
			if (jb == m) break;
			t[i][ja] = t[i][jb];
			t[i][jb] = '.';
			++ja;
			while(ja < m && t[i][ja] != '.') ++ja;
			if (ja == m) break;
			++jb;
		}
	}
}

void do_move_right()
{
	for(int i = 0; i < n; ++i) {
		int ja = m-1, jb;
		while(ja >= 0 && t[i][ja] != '.') --ja;
		if (ja < 0) continue;
		jb = ja-1;
		while(1) {
			while(jb >= 0 && t[i][jb] == '.') --jb;
			if (jb < 0) break;
			t[i][ja] = t[i][jb];
			t[i][jb] = '.';
			--ja;
			while(ja >= 0 && t[i][ja] != '.') --ja;
			if (ja < 0) break;
			--jb;
		}
	}
}

void do_move_up()
{
	for(int j = 0; j < m; ++j) {
		int ia = 0, ib;
		while(ia < n && t[ia][j] != '.') ++ia;
		if (ia == n) continue;
		ib = ia+1;
		while(1) {
			while(ib < n && t[ib][j] == '.') ++ib;
			if (ib == n) break;
			t[ia][j] = t[ib][j];
			t[ib][j] = '.';
			++ia;
			while(ia < n && t[ia][j] != '.') ++ia;
			if (ia == n) break;
			++ib;
		}
	}
}

void do_move_down()
{
	for(int j = 0; j < m; ++j) {
		int ia = n-1, ib;
		while(ia >= 0 && t[ia][j] != '.') --ia;
		if (ia < 0) continue;
		ib = ia-1;
		while(1) {
			while(ib >= 0 && t[ib][j] == '.') --ib;
			if (ib < 0) break;
			t[ia][j] = t[ib][j];
			t[ib][j] = '.';
			--ia;
			while(ia >= 0 && t[ia][j] != '.') --ia;
			if (ia < 0) break;
			--ib;
		}
	}
}

void do_moves(string &mv)
{
	int j = mv.size();
	int i = 0;
	while(i < j) {
		switch(mv.at(i)) {
			case 'L': do_move_left(); break;
			case 'P': do_move_right(); break;
			case 'D': do_move_down(); break;
			case 'G': do_move_up(); break;
		}
		++i;
	}
}

string find_move2()
{
	string mov1;
	string mov2;

	char lk1, lk2;
	int i;
	int st;

	mov1.push_back(mov[0]);
	if (mov[0] == 'L' || mov[0] == 'P') {
		lk1 = 'G';
		lk2 = 'D';
	} else {
		lk1 = 'L';
		lk2 = 'P';
	}
	
	i = 1;
	while(i < k && mov[i] != lk1 && mov[i] != lk2) {
		mov1[0] = mov[i];
		++i;
	}

	if (i == k) {
		return mov1;
	}
	mov1.push_back(mov[i]);
	++i;
	if (i == k) {
		return mov1;
	}

	string &movr = mov;
	string &movl = mov1;

	bool chg = true;
	bool first = true;
	while(chg) {
		chg = false;
		char lpst = 'X';
		char gdst = 'X';
		if (!first) {
			i = 2;
			string &tmp = movr;
			movr = movl;
			movl = tmp;
			movl.clear();
			movl.push_back(movr[0]);
			movl.push_back(movr[1]);
//			printf("movl size %d\n", movl.size());
		}
		first = false;
//		printf("movr %d\n", movr.size());
		while(i<movr.size()) {
//			if (chg) {
//				printf("%d\n", i);
//				printmov(movl);
//				printmov(movr);
//			}
			if (i == movr.size()-1) {
				movl.push_back(movr[i]);
				++i;
				break;
			}

			if (lpst == 'P' && movr[i] == 'L' && movr[i+1] == 'P') { i+=2; chg=true; continue; }
			if (lpst == 'L' && movr[i] == 'P' && movr[i+1] == 'L') { i+=2; chg=true; continue; }
			if (gdst == 'D' && movr[i] == 'G' && movr[i+1] == 'D') { i+=2; chg=true; continue; }
			if (gdst == 'G' && movr[i] == 'D' && movr[i+1] == 'G') { i+=2; chg=true; continue; }

			if (lpst == 'P' && movr[i] == 'P') { i+=1; chg=true; continue; }
			if (lpst == 'L' && movr[i] == 'L') { i+=1; chg=true; continue; }
			if (gdst == 'D' && movr[i] == 'D') { i+=1; chg=true; continue; }
			if (gdst == 'G' && movr[i] == 'G') { i+=1; chg=true; continue; }

		
			if (i + 11 < movr.size()) {
				string sub = movr.substr(i, 12);
				if (lpst == 'L' && gdst == 'G' && sub == "GLDPGLDPGLDP") { i+=12;chg=true;continue; }
				if (lpst == 'L' && gdst == 'D' && sub == "LDPGLDPGLDPG") { i+=12;chg=true;continue; }
				if (lpst == 'P' && gdst == 'D' && sub == "DPGLDPGLDPGL") { i+=12;chg=true;continue; }
				if (lpst == 'P' && gdst == 'G' && sub == "PGLDPGLDPGLD") { i+=12;chg=true;continue; }
				if (lpst == 'P' && gdst == 'G' && sub == "GPDLGPDLGPDL") { i+=12;chg=true;continue; }
				if (lpst == 'P' && gdst == 'D' && sub == "PDLGPDLGPDLG") { i+=12;chg=true;continue; }
				if (lpst == 'L' && gdst == 'D' && sub == "DLGPDLGPDLGP") { i+=12;chg=true;continue; }
				if (lpst == 'L' && gdst == 'G' && sub == "LGPDLGPDLGPD") { i+=12;chg=true;continue; }
			}
/*
			if (i + 10 < movr.size()) {
				string sub = movr.substr(i, 11);
				if (lpst == 'L' && gdst == 'G' && sub == "GLDPGLDPGLD") { movl.push_back('L'); i+=11;chg=true;continue; }
				if (lpst == 'L' && gdst == 'D' && sub == "LDPGLDPGLDP") { movl.push_back('D'); i+=11;chg=true;continue; }
				if (lpst == 'P' && gdst == 'D' && sub == "DPGLDPGLDPG") { movl.push_back('P'); i+=11;chg=true;continue; }
				if (lpst == 'P' && gdst == 'G' && sub == "PGLDPGLDPGL") { movl.push_back('L'); i+=11;chg=true;continue; }
				if (lpst == 'P' && gdst == 'G' && sub == "GPDLGPDLGPD") { movl.push_back('L'); i+=11;chg=true;continue; }
				if (lpst == 'P' && gdst == 'D' && sub == "PDLGPDLGPDL") { movl.push_back('L'); i+=11;chg=true;continue; }
				if (lpst == 'L' && gdst == 'D' && sub == "DLGPDLGPDLG") { movl.push_back('L'); i+=11;chg=true;continue; }
				if (lpst == 'L' && gdst == 'G' && sub == "LGPDLGPDLGP") { movl.push_back('L'); i+=11;chg=true;continue; }
			}
*/

			movl.push_back(movr[i]);

			if (movr[i] == 'L') lpst = 'L';
			if (movr[i] == 'P') lpst = 'P';
			if (movr[i] == 'G') gdst = 'G';
			if (movr[i] == 'D') gdst = 'D';
			++i;
		}
//		printf("movl %d\n", movl.size());
//		printmov(movl);
	}
	return movl;
}

int main()
{
        scanf("%d %d\n", &n, &m);

	for(int i = 0; i < n; ++i) {
		char s[max_size];
		scanf("%s\n", s);
		for(int j = 0; j < m; ++j) {
			t[i][j] = s[j];
		}
	}

	scanf("%d\n", &k);
	for(int i = 0 ; i < k; ++i) {
		char c;
		scanf("%c\n", &c);
		if (mov.size() == 0 or mov[mov.size()-1] != c) {
			mov.push_back(c);
		}
	}
	k = mov.size();

//	printf("%d", k);
	string final_move = find_move2();
//	printf(" %d\n", final_move.size());

	do_moves(final_move);

	print();


        return 0;
}