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
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, b, e) for(int i = (b); i < (e); i++)
#define TRAV(x, v) for(auto &x: v)
#define PB push_back
#define SZ(x) ((int)x.size())
#define X first
#define Y second

using ll = long long;
using vi = vector<int>;
using ii = pair<int, int>;
constexpr int INF = 0x3f3f3f3f;

map<char, int> type = {{'D', 0}, {'G', 2}, {'L', 1}, {'P', 3}};


string trim(string seq) {
    vector<char> wyst[2];
    string nseq;
    TRAV(x, seq) {
        if(SZ(nseq) > 0 && type[nseq.back()] % 2 == type[x] % 2) {
            wyst[type[x] % 2].pop_back();
            nseq.pop_back();
        }
        if(SZ(wyst[type[x] % 2]) == 0 || wyst[type[x] % 2].back() != x) {
            wyst[type[x] % 2].PB(x);
            nseq += x;
        }
    }
    return nseq;
}

vector<vi> reverse(vector<vi> board) {
    vector<vi> res = board;
    TRAV(x, res) reverse(x.begin(), x.end());
    return res;
}

vector<vi> transpose(vector<vi> board) {
    vector<vi> res(SZ(board[0]));
    FOR(i, 0, SZ(board[0])) FOR(j, 0, SZ(board)) res[i].PB(board[j][i]);
    return res;
}

vector<vi> move(vector<vi> board, int typ) {
    if(typ == 3) {
        TRAV(x, board) {
            vi nowy;
            TRAV(y, x) if(y) nowy.PB(y);
            x = vi(SZ(x) - SZ(nowy));
            TRAV(y, nowy) x.PB(y);
        }
        return board;
    }
    if(typ == 1) return reverse(move(reverse(board), 3));
    return transpose(move(transpose(board), 3 - typ));
}

vector<vi> cast(vector<string> board) {
    vector<vi> res(SZ(board), vi(SZ(board[0])));
    int cntB = -1, cntC = 0;
    FOR(i, 0, SZ(board)) FOR(j, 0, SZ(board[i])) if(board[i][j] != '.') {
        res[i][j] = (board[i][j] == 'B' ? cntB += 2 : cntC += 2);
    }
    return res;
}

vi mnoz(vi a, vi b) {
    vi res(SZ(a));
    FOR(i, 0, SZ(res)) res[i] = b[a[i]];
    return res;
}

vi pot(vi base, int exp) {
    vi ret(SZ(base));
    iota(ret.begin(), ret.end(), 0);
    while(exp) {
        if(exp & 1) ret = mnoz(ret, base);
        base = mnoz(base, base);
        exp >>= 1;
    }
    return ret;
}

void solve() {
    int n, m, k;
    cin >> n >> m;
    vector<string> sboard(n);
    TRAV(x, sboard) cin >> x;
    string seq;
    cin >> k >> seq;
    seq = trim(seq);
    vector<vi> board = cast(sboard);
    FOR(i, 0, 2) {
        if(SZ(seq) > 0) {
            board = move(board, type[seq[0]]);
            seq = seq.substr(1);
        }
    }
    if(SZ(seq) >= 4) {
        vector<vi> nowa = board;
        FOR(i, 0, 4) nowa = move(nowa, type[seq[i]]);
        vi nast(n * m * 2 + 1);
        iota(nast.begin(), nast.end(), 0);
        FOR(i, 0, n) FOR(j, 0, m) nast[nowa[i][j]] = board[i][j];
        vi skok = pot(nast, SZ(seq) / 4);
        vi inv(n * m * 2 + 1);
        FOR(i, 0, SZ(skok)) inv[skok[i]] = i;
        FOR(i, 0, n) FOR(j, 0, m) board[i][j] = inv[board[i][j]];
        seq = seq.substr(SZ(seq) / 4 * 4);
    }
    TRAV(x, seq) board = move(board, type[x]);
    TRAV(x, board) {
        TRAV(y, x) cout << (y ? (y % 2 ? 'B' : 'C') : '.');
        cout << '\n';
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    // int tt; cin >> tt;
    // FOR(te, 0, tt) {
    //     // cout << "Case #" << te + 1 << ": ";
    //     solve();
    // }
    solve();
    return 0;
}