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
#include <bits/stdc++.h> //Michał Kuśmirek
using namespace std;     //XIII LO Szczecin

#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define REP(i, n) for(int i = 0; i < (n); ++i)

template<class T> int size(T && a) {
	return (int)(a.size());
}
ostream& operator << (ostream &os, string str) {
    for(char c : str) os << c;
    return os;
}
template <class A, class B> ostream& operator << (ostream &os, const pair<A, B> &p) {
	return os << '(' << p.first << "," << p.second << ')';
}
template <class T> auto operator << (ostream &os, T &&x) -> decltype(x.begin(), os) {
	os << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		os << *it << (it == prev(x.end()) ? "" : " ");
	return os << '}';
}
template <class T> ostream& operator << (ostream &os, vector<vector<T>> vec) {
    for(auto x : vec)
    	os << "\n  " << x;
    return os;
}
void dump() {}
template <class T, class... Args> void dump(T &&x, Args... args) {
    cerr << x << "; ";
    dump(args...);
}
#ifdef DEBUG
# define debug(x...) cerr << "[" #x "]: ", dump(x), cerr << '\n'
#else 
# define debug(...) 0
#endif

//--------------------------------------------------

vector<vector<int>> move(const vector<vector<int>> &board, char dir) {
	int n = size(board),
		m = size(board[0]);

	vector<vector<int>> new_board(n, vector<int>(m, 0));

	if(dir == 'L' or dir == 'P') {
		REP(i, n) {
			int cur = (dir == 'L' ? 0 : m - 1);
			int d = (dir == 'L' ? 1 : -1);
			for(int j = cur; 0 <= j and j < m; j += d) {
				if(board[i][j] != 0) {
					new_board[i][cur] = board[i][j];
					cur += d;
				}
			}
		}
	}

	else {
		REP(j, m) {
			int cur = (dir == 'G' ? 0 : n - 1);
			int d = (dir == 'G' ? 1 : -1);
			for(int i = cur; 0 <= i and i < n; i += d) {
				if(board[i][j] != 0) {
					new_board[cur][j] = board[i][j];
					cur += d;
				}
			}
		}
	}

	return new_board;
}

char inv(char c) {
	if(c == 'L') return 'P';
	if(c == 'P') return 'L';
	if(c == 'G') return 'D';
	if(c == 'D') return 'G';
}

pair<string, string> simplify(string s) {
	string ret = "";
	int n = size(s);

	char horizontal = '.', vertical = '.';
	int start = n;
	REP(i, n) {
		debug(i, horizontal, vertical);
		char c = s[i];

		if(c == horizontal or c == vertical)
			continue;
		else if(size(ret) > 1 and ret.back() == inv(c)) {
			ret.pop_back();
			if(c == 'L' or c == 'P')
				horizontal = c;
			else
				vertical = c;
		} else if(ret.empty() or ret.back() != c) {
			ret += c;
			if(c == 'G' or c == 'D')
				vertical = c;
			if(c == 'P' or c == 'L')
				horizontal = c;
		}

		if(horizontal != '.' and vertical != '.') {
			start = i + 1;
			break;
		}
	}

	string ret2 = "";
	FOR(i, start, n - 1) {
		char c = s[i];
		debug(i, c, horizontal, vertical, ret2);
		if(c == horizontal or c == vertical)
			continue;
		else if(!ret2.empty() and ret2.back() == inv(c)) {
			if(c == 'L' or c == 'P')
				horizontal = c;
			else
				vertical = c;
			ret2.pop_back();
		} else if(ret2.empty() or ret2.back() != c) {
			ret2 += c;
			if(c == 'L' or c == 'P')
				horizontal = c;
			else
				vertical = c;
		}

	}

	debug(ret, ret2);
	return pair(ret, ret2);
}

vector<vector<pair<int, int>>> create_ptr(vector<vector<int>> board, vector<vector<int>> new_board) {
	int n = size(board),
		m = size(board[0]);

	int max_number = 0;
	REP(i, n) REP(j, m)
		max_number = max(max_number, board[i][j]);

	vector<pair<int, int>> new_pos(max_number + 1);
	REP(i, n) REP(j, m) {
		if(new_board[i][j] != 0)
			new_pos[new_board[i][j]] = pair(i, j);
	}

	vector<vector<pair<int, int>>> ret(n, vector<pair<int, int>>(m));
	REP(i, n) REP(j, m) {
		if(board[i][j] != 0)
			ret[i][j] = new_pos[board[i][j]];	
		else ret[i][j] = pair(i, j);
	}

	return ret;
}

vector<vector<int>> jump(vector<vector<int>> board, vector<vector<pair<int, int>>> ptr) {
	int n = size(board),
		m = size(board[0]);

	auto new_board = board;
	REP(i, n) REP(j, m) if(board[i][j] != 0) {
		auto [x, y] = ptr[i][j];
		new_board[x][y] = board[i][j];
	}
	return new_board;
}

vector<vector<pair<int, int>>> mult_ptr(vector<vector<pair<int, int>>> ptr) {
	int n = size(ptr),
		m = size(ptr[0]);

	auto new_ptr = ptr;
	REP(i, n) REP(j, m) {
		auto [x, y] = ptr[i][j];
		new_ptr[i][j] = ptr[x][y];
	}
	return new_ptr;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n, m;
	cin >> n >> m;
	vector<vector<int>> board(n, vector<int>(m));
	vector<string> input(n);
	vector<char> values = {'.'};
	REP(i, n) {
		cin >> input[i];
		REP(j, m) {
			if(input[i][j] != '.') {
				board[i][j] = size(values);
				values.emplace_back(input[i][j]);
			}
		}
	}

	debug(board);
	debug(values);

	int k; cin >> k;
	string operations;
	cin >> operations;

	auto tmp = simplify(operations);
	string preprocess = tmp.first,
		   s = tmp.second;

	for(char c : preprocess)
		board = move(board, c);
	debug(board);

	string cycle = "";
	string rest = "";
	int cycle_count = 0;
	REP(i, size(s)) {
		if(size(cycle) < 4) {
			cycle += s[i];
			if(size(cycle) == 4)
				++cycle_count;
		} else {
			rest += s[i];
			if(rest == cycle) {
				rest = "";
				++cycle_count;
			}
		}
	}

	debug(cycle_count);
	if(cycle_count > 0) {
		auto new_board = board;
		for(char c : cycle)
			new_board = move(new_board, c);
		debug(new_board);

		auto ptr = create_ptr(board, new_board);
		debug(ptr);

		while(cycle_count > 0) {
			if(cycle_count % 2 == 1) {
				board = jump(board, ptr);
				debug(board);
			}
			ptr = mult_ptr(ptr);
			debug(ptr);
			cycle_count /= 2;
		}
	} else {
		rest = cycle;
	}

	for(char c : rest)
		board = move(board, c);

	REP(i, n) {
		REP(j, m)
			cout << values[board[i][j]];
		cout << '\n';
	}
}