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
#include <iostream>
#include <vector>
#include <cassert>

using namespace std;

// #define DEBUG
#ifdef DEBUG
#define dassert(x) assert(x);
#define df(...) printf(__VA_ARGS__)
#else
#define dassert(x)
#define df(...)
#endif

#define x first
#define y second
#define mp make_pair
#define pb push_back
#define ir(x, a, b) ((a) <= (x) && (x) <= (b))
#define vec vector
#define sz(x) (ll)x.size()
#define foru(i, n) for (int i = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()

typedef int64_t ll;

vec<int> calc(char c) {
    if (c == 'P') return {1, 0};
    if (c == 'L') return {0, 0};
    if (c == 'G') return {0, 1};
    if (c == 'D') return {0, 0};
    assert(0);
}

int coordinate(char c) {
    if (c == 'P') return 0;
    if (c == 'L') return 0;
    if (c == 'G') return 1;
    if (c == 'D') return 1;
    assert(0);
}

bool perpendicular(char x, char y) {
    return coordinate(x) != coordinate(y);
}

bool idempotent(vec<int> corner, char c) {
    int co = coordinate(c);
    return corner[co] == calc(c)[co];
}


vec<vec<int>> transpose(const vec<vec<int>>& a) {
    int n = a.size(), m = a[0].size();
    vec<vec<int>> b = vec<vec<int>>(m, vec<int>(n));
    foru (i, n) foru (j, m) b[j][i] = a[i][j];
    return b;
}

vec<vec<int>> reflect(vec<vec<int>> a) {
    foru (i, a.size()) {
        foru (j, a[0].size()/2) {
            swap(a[i][j], a[i][a[0].size()-1-j]);
        }
    }
    return a;
}

vec<vec<int>> move(vec<vec<int>> a, char c) {
    int _n = a.size(), _m = a[0].size();
    if (c == 'D' || c == 'G') a = transpose(a);
    if (c == 'L' || c == 'G') a = reflect(a);
    
    int n = a.size(), m = a[0].size();
    foru (i, n) {
        vec<int> x;
        foru (j, m) {
            if (a[i][j]) {
                x.pb(a[i][j]);
                a[i][j] = 0;
            }
        }
        for (int j = 0; j < x.size(); ++j) {
            a[i][m-x.size()+j] = x[j];
        }
    }
    
    if (c == 'L' || c == 'G') a = reflect(a);
    if (c == 'D' || c == 'G') a = transpose(a);
    dassert(_n == a.size() && _m == a[0].size());
    return a;
}

pair<string, string> prune(string s) {
    string beg = " ";
    beg[0] = s[0];
    int k = s.size();
    string moves;
    vec<int> corner = {-1, -1};
    // cout << beg << endl;
    for (int i = 1; i < k; ++i) {
        // cout << corner[0] << " " << corner[1] << " " << s[i] << endl;
        if (s[i-1] == s[i] || idempotent(corner, s[i])) continue;
        if (beg.size() == 1) {
            if (!perpendicular(beg[0], s[i])) {
                beg[0] = s[i];
            } else {
                beg += s[i];
                ++corner[0], ++corner[1];
                for (auto c : beg) {
                    auto x = calc(c);
                    foru (i, 2) corner[i] += x[i];
                }
            }
        } else {
            if (moves.size() == 0) {
                corner[coordinate(s[i])] ^= 1;
                moves.pb(s[i]);
            } else if (perpendicular(moves.back(), s[i])) {
                corner[coordinate(s[i])] ^= 1;
                moves.push_back(s[i]);
            } else {
                corner[coordinate(s[i])] ^= 1;
                moves.pop_back();
            }
        }
        // remove duplicates
    }
    return {beg, moves};
}

vec<vec<int>> exp(vec<vec<int>> x, string moves, int n) {
    auto y = x;
    int reps = moves.size()/4;
    vec<int> pos(n+1), rpos(n+1);
    foru (i, n+1) pos[i] = i;
    vec<int> mat(n+1), mat_(n+1);
    foru (i, 4) y = move(y, moves[i]);
    foru (i, x.size()) foru (j, x[0].size()) {
        assert((x[i][j] == 0) == (y[i][j] == 0));
        if (x[i][j] != 0) mat[y[i][j]] = x[i][j];
    }
//     foru (i, n+1) cout << mat[i] << " ";
    // cout << endl;
    while (reps) {
        if (reps & 1) {
            foru (i, n+1) pos[i] = mat[pos[i]];
        }
        foru (i, n+1) mat_[i] = mat[mat[i]];
        mat.swap(mat_);
        reps /= 2;
    }
    foru (i, n+1) rpos[pos[i]] = i;
    foru (i, x.size()) foru (j, x[0].size()) {
        x[i][j] = rpos[x[i][j]];
    }
    return x;
}

vec<vec<int>> brute(vec<vec<int>> a, string s) { 
    for (auto c : s) a = move(a, c);
    return a;
}


int main() {
    df("debug mode\n");
#ifndef DEBUG
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
    int n, m;
    cin >> n >> m;
    vec<vec<int>> a(n, vec<int>(m));
    vec<bool> colors;
    foru (i, n) {
        string s;
        cin >> s;
        foru (j, m) {
            if (s[j] != '.') {
                colors.pb(s[j] == 'B');
                a[i][j] = colors.size();
            }
        }
    }
    int k;
    cin >> k;
    string s;
    cin >> s;
    
#ifdef TEST
    vec<vec<int>> b = brute(a, s);
#endif
    
    auto [beg, moves] = prune(s);
    // cout << beg << " " << moves << endl;
    
    for (auto c : beg) {
        a = move(a, c);
    }
    if (moves.size() >= 4) a = exp(a, moves, colors.size());
    for (int i = moves.size() - (moves.size()%4); i < moves.size(); ++i) {
        a = move(a, moves[i]);
    }
    // cout << beg << " " << moves << endl;
#ifdef TEST
    foru (i, n) foru (j, m) assert(a[i][j] == b[i][j]);
#endif

    // foru (i, n) { foru (j, m) cout << a[i][j];  cout << endl; }
    foru (i, n) { foru (j, m) cout << (a[i][j] == 0 ? '.' : (colors[a[i][j]-1] ? 'B' : 'C')); cout << endl; }
    return 0;
}