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
#include "bits/stdc++.h" // Tomasz Nowak
using namespace std;     // University of Warsaw
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
#define ssize(x) int(x.size())
template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) {
	return o << '(' << p.first << ", " << p.second << ')';
}
ostream& operator<<(ostream &o, string s) {
	return o << s.data();
}
template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) {
	o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}';
}
#ifdef DEBUG
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n'
#else
#define debug(...) {}
#endif

template<class T>
void make_move(int move, vector<vector<T>> &board, T neutral) {
	bool transposed = false;
	auto transpose = [&] {
		transposed = not transposed;
		vector<vector<T>> changed(ssize(board[0]), vector<T>(ssize(board), neutral));
		REP(x, ssize(board))
			REP(y, ssize(board[0]))
				changed[y][x] = board[x][y];
		board = changed;
	};
	if(move == 1 or move == 3) {
		move ^= 3;
		transpose();
	}
	REP(x, ssize(board[0])) {
		vector<T> on_path;
		REP(y, ssize(board))
			if(board[y][x] != neutral)
				on_path.emplace_back(board[y][x]);
		REP(y, ssize(board))
			board[y][x] = neutral;
		REP(i, ssize(on_path))
			if(move == 0)
				board[i][x] = on_path[i];
			else
				board[ssize(board) - 1 - i][x] = on_path[ssize(on_path) - 1 - i];
	}
	if(transposed)
		transpose();
	assert(transposed == false);
	debug(move, board);
};

using CoordBoard = vector<vector<pair<int, int>>>;
CoordBoard compose(const CoordBoard &l, const CoordBoard &r) {
	CoordBoard ret(ssize(l), vector(ssize(l[0]), pair(-1, -1)));
	REP(y, ssize(l))
		REP(x, ssize(l[0]))
			if(l[y][x] != pair(-1, -1)) {
				auto [yy, xx] = l[y][x];
				ret[y][x] = r[yy][xx];
			}
	return ret;
}

CoordBoard powi(const CoordBoard &a, int k) {
	if(k == 1)
		return a;
	auto x = powi(compose(a, a), k / 2);
	if(k % 2 == 1)
		x = compose(x, a);
	return x;
}

vector<vector<int>> apply_composition(const CoordBoard &b, const vector<vector<int>> &in) {
	vector ret(ssize(b), vector(ssize(b[0]), -1));
	REP(y, ssize(b))
		REP(x, ssize(b[0]))
			if(in[y][x] != -1) {
				auto [yy, xx] = b[y][x];
				ret[y][x] = in[yy][xx];
			}
	return ret;
}

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int n, m;
	cin >> n >> m;
	vector<vector<int>> in(n);
	for(vector<int> &row : in) {
		string s;
		cin >> s;
		assert(ssize(s) == m);
		row.resize(m);
		REP(i, m)
			if(s[i] == '.')
				row[i] = -1;
			else if(s[i] == 'B')
				row[i] = 0;
			else if(s[i] == 'C')
				row[i] = 1;
			else
				assert(false);
	}
	debug(in);

	int k;
	cin >> k;
	vector<int> moves;
	map<char, int> char_to_int = {
		{'G', 0},
		{'P', 1},
		{'D', 2},
		{'L', 3},
	};

	auto from_end = [&](int i) {
		return moves.end()[-i - 1];
	};
	auto rm_from_end = [&](int i) {
		vector<int> sztos;
		REP(j, i) {
			sztos.emplace_back(moves.back());
			moves.pop_back();
		}
		moves.pop_back();
		REP(j, i)
			moves.emplace_back(sztos.end()[-1 - j]);
	};

	while(k --> 0) {
		char cc;
		cin >> cc;
		int c = char_to_int[cc];
		moves.emplace_back(c);
		while(true) {
			if(ssize(moves) >= 2 and from_end(0) == from_end(1))
				rm_from_end(0);
			else if(ssize(moves) >= 2 and from_end(0) == (from_end(1) ^ 2))
				rm_from_end(1);
			else if(ssize(moves) >= 3 and from_end(0) == from_end(1))
				rm_from_end(0);
			else if(ssize(moves) >= 3 and from_end(0) == from_end(2))
				rm_from_end(0);
			else
				break;
		}
		debug(cc, moves);
	}
	debug(moves);

	if(ssize(moves) >= 4) {
		REP(i, 4)
			make_move(moves[i], in, -1);
		REP(i, 4)
			moves.erase(moves.begin());
	}
	debug(in);

	pair<int, int> neutral_pp = {-1, -1};
	CoordBoard no_circle(n, vector(m, neutral_pp));
	REP(x, m)
		REP(y, n)
			if(in[y][x] != -1)
				no_circle[y][x] = pair(y, x);
	if(ssize(moves) >= 4) {
		CoordBoard single_circle = no_circle;
		REP(i, 4)
			make_move(moves[i], single_circle, neutral_pp);
		debug(single_circle);

		auto composed = powi(single_circle, ssize(moves) / 4);
		in = apply_composition(composed, in);
		moves.resize(ssize(moves) % 4);
	}

	for(int move : moves)
		make_move(move, in, -1);

	REP(y, n) {
		REP(x, m)
			cout << (in[y][x] == -1 ? '.' : in[y][x] == 0 ? 'B' : 'C');
		cout << '\n';
	}
}