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
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#include <bits/stdc++.h>
using namespace std;
#define sim template < class c
#define ris return * this
#define mor > muu & operator << (
#define R22(r) sim > typename \
  enable_if<1 r sizeof dud<c>(0), muu&>::type operator<<(c g) {
sim > struct rge {c b, e; };
sim > rge<c> range(c h, c n) { return {h, n}; }
sim > auto dud(c * r) -> decltype(cerr << *r);
sim > char dud(...);
struct muu {
#ifdef LOCAL
stringstream a;
~muu() { cerr << a.str() << endl; }
R22(<) a << boolalpha << g; ris; }
R22(==) ris << range(begin(g), end(g)); }
sim, class b mor pair < b, c > r) { ris << "(" << r.first << ", " << r.second << ")"; }
sim mor rge<c> u) {
a << "[";
for (c i = u.b; i != u.e; ++i) 
  *this << ", " + 2 * (i == u.b) << *i;
 ris << "]";
 }
#else
sim mor const c&) { ris; }
#endif
muu & operator()() { ris; }
};
#define imie(r...) "[" #r ": " << (r) << "] "
#define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": ")

long long mod = 434834343;

long long hasz(vector<string> v) {
	int p1 = 8731;
	int p2 = 7283;
	long long p1_pot = 1;
	long long h = 0;
	for (int i = 0; i < v.size(); i ++) {
		p1_pot = (p1 * p1_pot) % mod;
		long long p2_pot = 1;
		for (int j = 0; j < v[i].size(); j++) {
			p2_pot = (p2 * p2_pot) % mod;
			if (v[i][j] == 'B') h += (p1_pot * p2_pot) % mod;
			if (v[i][j] == 'C') h += 2 * (p1_pot * p2_pot) % mod;
//			cout << i << " " << j << " " << v[i][j] << " " << h << "\n";
		}
	}
	return h % mod;
}

map<long long,map<char, long long>> moves_map;
map<long long, vector<string>> hasz_map;

long long insert_hasz(vector<string> v) {
	long long h = hasz(v);
	hasz_map[h] = v;
	return h;
}

long long real_move(long long in_hasz, char x) {
	vector<string> v = hasz_map[in_hasz];
	vector<string> sol;
	if (x == 'L') {
		for (int i = 0; i < v.size(); i ++) {
			string res = "";
			for (int j = 0; j < v[i].size(); j ++) {
				if (v[i][j] != '.') res += v[i][j];
			}
			res.resize(v[i].size(), '.');
			sol.push_back(res);
		}
	} else if (x == 'P') {
		for (int i = 0; i < v.size(); i ++) {
			string res = "";
			for (int j = v[i].size() - 1; j >= 0; j --) {
				if (v[i][j] != '.') res += v[i][j];
			}
			res.resize(v[i].size(), '.');
			reverse(res.begin(), res.end());
			sol.push_back(res);
		}
	} else if (x == 'G') {
		sol = v;
		for (int j = 0; j < v[0].size(); j ++) {
			int k = 0;
			for (int i = 0; i < v.size(); i ++) {
				if (v[i][j] != '.') {
					sol[k][j] = v[i][j];
					k ++;
				}
			}
			for (int i = k; i < v.size(); i ++) sol[i][j] = '.';
		}
	}  else if (x == 'D') {
		sol = v;
		for (int j = 0; j < v[0].size(); j ++) {
			int k = v.size() - 1;
			for (int i = k; i >= 0; i --) {
				if (v[i][j] != '.') {
					sol[k][j] = v[i][j];
					k --;
				}
			}
			for (int i = k; i >= 0; i --) sol[i][j] = '.';
		}
	}
	long long h = hasz(sol);
	hasz_map[h] = sol;
	moves_map[in_hasz][x] = h;
	return h;
}

long long move(long long hasz, char x) {
	if (moves_map.find(hasz) == moves_map.end()) return real_move(hasz, x);
	if (moves_map[hasz].find(x) == moves_map[hasz].end()) return real_move(hasz, x);
	return moves_map[hasz][x];
}

int main() {
	ios_base::sync_with_stdio(0);
	int n, m;	
	cin >> n >> m;
	vector<string> v;
	v.resize(n);
	for (int i = 0; i < n; i ++) cin >> v[i];
	int k;
	cin >> k;
	string moves;
	cin >> moves;
	long long h = insert_hasz(v);
//	cout << h << "\n";
	for (char x : moves) {
//		cout << x << ": ";
//		cout << h << "\n";
		h = move(h, x);
	}
	auto sol = hasz_map[h];
	for (auto x : sol) {
		cout << x << "\n";
	}
}