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
#include <bits/stdc++.h>
using namespace std;
const int MAX = 5e2;
const int LMAX = 19;
int n, m, k, q;
int t[MAX + 7][MAX + 7], t1[MAX + 7][MAX + 7], t2[MAX + 7][MAX + 7];
int ancestor[MAX * MAX + 7][LMAX + 7];
pair<int, int> coor[MAX * MAX + 7];
string s;
vector<char> query;
char _rev(char c) {
    if (c == 'G') return 'D';
    if (c == 'P') return 'L';
    if (c == 'D') return 'G';
    return 'P';
}
int z = 0;
void iterate(int t[][MAX + 7]) {
    if (z >= k) return;
    z++;
    if (query[z - 1] == 'G') {
        for (int i = 1; i <= m; i++) {
            int l = 1;
            while (t[l][i]) l++;
            for (int j = l + 1; j <= n; j++) {
                if (t[j][i]) {
                    t[l++][i] = t[j][i];
                    t[j][i] = 0;
                }
            }
        }
    } else if (query[z - 1] == 'P') {
        for (int i = 1; i <= n; i++) {
            int l = m;
            while (t[i][l]) l--;
            for (int j = l - 1; j >= 0; j--) {
                if (t[i][j]) {
                    t[i][l--] = t[i][j];
                    t[i][j] = 0;
                }
            }
        }
    } else if (query[z - 1] == 'D') {
        for (int i = 1; i <= m; i++) {
            int l = n;
            while (t[l][i]) l--;
            for (int j = l - 1; j >= 0; j--) {
                if (t[j][i]) {
                    t[l--][i] = t[j][i];
                    t[j][i] = 0;
                }
            }
        }
    } else {
        for (int i = 1; i <= n; i++) {
            int l = 1;
            while (t[i][l]) l++;
            for (int j = l + 1; j <= m; j++) {
                if (t[i][j]) {
                    t[i][l++] = t[i][j];
                    t[i][j] = 0;
                }
            }
        }
    }
}
int32_t main() {
    ios_base::sync_with_stdio(0);
    cout.tie(0);
    cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> s;
        for (int j = 1; j <= m; j++) {
            char c = s[j - 1];
            if (c == 'B') {
                t[i][j] = 1;
            } else if (c == 'C') {
                t[i][j] = 2;
            }
        }
    }
    cin >> k >> s;
    for (int i = 0; i < k; i++) {
        if (query.empty()) query.push_back(s[i]);
        else if (query.back() != s[i]) {
            if (query.back() == _rev(s[i])) query.pop_back();
            if (query.size() < 2 || query[query.size() - 2] != s[i]) query.push_back(s[i]);
        }
    }
    k = query.size();
    iterate(t);
    iterate(t);
    do {
        iterate(t);
    } while ((k - z) % 4 != 0);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            if (t[i][j]) {
                t1[i][j] = t2[i][j] = ++q;
                coor[q] = {i, j};
            }
        }
    for (int i = 0; i < 4; i++) iterate(t2);
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (t1[i][j]) {
                ancestor[t1[i][j]][0] = t2[i][j];
            }
        }
    }
    for (int j = 1; j <= LMAX; j++)
        for (int i = 1; i <= q; i++) {
            ancestor[i][j] = ancestor[ancestor[i][j - 1]][j - 1];
        }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (!t[i][j]) cout << '.';
            else {
                int x = t1[i][j];
                int y = (k - z) / 4 + 1;
                for (int a = LMAX; a >= 0; a--) {
                    if (y & (1 << a)) {
                        x = ancestor[x][a];
                    }
                }
                auto [a, b] = coor[x];
                if (t[a][b] == 1) cout << 'B';
                else
                    cout << 'C';
            }
        }
        cout << "\n";
    }
    return 0;
}
// DAWID PAWELEC