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
#include <bits/stdc++.h>
using namespace std;

map<char, char> OPP;
map<char, int> VAL;

void init() {
    OPP['G'] = 'D';
    OPP['D'] = 'G';
    OPP['L'] = 'P';
    OPP['P'] = 'L';

    VAL['B'] = 1;
    VAL['C'] = 2;
}

const int maxN = 5e5 + 10;

int X, Y;
int N;

char S[maxN];
vector<char> moves;

void addchar(char c) {
    if (moves.back() == c || moves.back() == OPP[c])
        moves.pop_back();
    // if (moves[moves.size() - 2] == c)
    //     return;
    moves.push_back(c);
}

struct Cube {
    int X, Y;
    vector<string> B;

    void get() {
        scanf("%d%d", &X, &Y);
        for (int x = 0; x < X; ++x) {
            char _[512];
            scanf("%s", &_);
            B.emplace_back(_);
        }
    }

    long long hsh() {
        const int mod1 = 1100000017, mod2 = 1e9 + 7;
        long long hsh1 = 0, hsh2 = 0;
        int p1 = 5, p2 = 512;
        for (int x = 0; x < X; ++x) {
            hsh1 = (hsh1 * p2) % mod1;
            hsh2 = (hsh2 * p2) % mod2;
            for (int c : B[x]) {
                hsh1 = (hsh1 * p1 + VAL[c]) % mod1;
                hsh2 = (hsh2 * p1 + VAL[c]) % mod2;
            }
        }
        return hsh1 + mod1 * hsh2;
    }

    void up() {
        for (int y = 0; y < Y; ++y) {
            int x0 = 0;
            while (x0 < X && B[x0][y] != '.')
                x0++;
            for (int i = x0; i < X; ++i)
                if (B[i][y] != '.') {
                    swap(B[x0][y], B[i][y]);
                    x0++;
                }
        }
    }

    void down() {
        for (int y = 0; y < Y; ++y) {
            int x0 = X - 1;
            while (x0 >= 0 && B[x0][y] != '.')
                x0--;
            for (int i = x0; i >= 0; --i)
                if (B[i][y] != '.') {
                    swap(B[x0][y], B[i][y]);
                    x0--;
                }
        }
    }

    void left() {
        for (auto &s : B) {
            int y0 = 0;
            while (y0 < Y && s[y0] != '.')
                y0++;
            for (int i = y0; i < Y; ++i)
                if (s[i] != '.') {
                    swap(s[y0], s[i]);
                    y0++;
                }
        }
    }

    void right() {
        for (auto &s : B) {
            int y0 = Y - 1;
            while (y0 >= 0 && s[y0] != '.')
                y0--;
            for (int i = y0; i >= 0; --i)
                if (s[i] != '.') {
                    swap(s[y0], s[i]);
                    y0--;
                }
        }
    }

    void move(char c) {
        if (c == 'G')
            up();
        if (c == 'D')
            down();
        if (c == 'L')
            left();
        if (c == 'P')
            right();
    }

    void print() {
        for (auto &s : B)
            puts(s.c_str());
    }
};

int main() {
    init();

    Cube C;
    C.get();

    scanf("%d", &N);
    scanf("%s", S);
    moves.push_back('$');
    moves.push_back('$');
    for (int i = 0; i < N; ++i)
        addchar(S[i]);

    N = moves.size();
    // for (int c : moves)
    //     printf("%c", c);
    // puts("");

    map<long long, int> hist;
    bool skipped = false;
    for (int i = 2; i < N; ++i) {
        C.move(moves[i]);
        // long long hsh = C.hsh();
        // if (!skipped && hist.find(hsh) != hist.end()) {
        //     skipped = true;
        //     int diff = i - hist[hsh];
        //     // printf("%d\n", diff);
        //     while (i + diff < N)
        //         i += diff;
        // }
        // hist[hsh] = i;
    }
    C.print();

    return 0;
}