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
#include <bits/stdc++.h>
#include <string>

using namespace std;

typedef long long ll;

const int mod = 100003;
const int prime = 5;

int n, m, k, akt;
string grid;
string s;

unordered_map<int, int> G[4];
map<int, string> h;

int hh(string &x){
    int curr = 0;
    for(int i = 0; i < n * m; i++){
        curr *= prime;
        curr += x[i];
    }
    h[curr] = x;
    return curr;
}

int new_grid(int curr_hash, char move){
    string ret = h[curr_hash];
    if(move == 1){
        for(int j = 0; j < m; j++){
            akt = 0;
            for(int i = 0; i < n; i++){
                if(ret[i * m + j] != 2){
                    swap(ret[i * m + j], ret[akt * m + j]);
                    akt++;
                }
            }
        }
    }else if(move == 2){
        for(int j = 0; j < m; j++){
            akt = n - 1;
            for(int i = n - 1; i >= 0; i--){
                if(ret[i * m + j] != 2){
                    swap(ret[i * m + j], ret[akt * m + j]);
                    akt--;
                }
            }
        }
    }else if(move == 0){
        for(int i = 0; i < n; i++){
            akt = 0;
            for(int j = 0; j < m; j++){
                if(ret[i * m + j] != 2){
                    swap(ret[i * m + j], ret[i * m + akt]);
                    akt++;
                }
            }
        }
    }else{
        for(int i = 0; i < n; i++){
            akt = m - 1;
            for(int j = m - 1; j >= 0; j--){
                if(ret[i * m + j] != 2){
                    swap(ret[i * m + j], ret[i * m + akt]);
                    akt--;
                }
            }
        }
    }
    return hh(ret);
}

int main(){
    cin.tie(NULL);
    cout.tie(NULL);
    ios_base::sync_with_stdio();
    cin >> n >> m;
    grid.resize(n * m);
    string tmp;
    for(int i = 0; i < n; i++){
        cin >> tmp;
        for(int j = 0; j < m; j++){
            if(tmp[j] == 'B')
                grid[i * m + j] = 0;
            if(tmp[j] == 'C')
                grid[i * m + j] = 1;
            if(tmp[j] == '.')
                grid[i * m + j] = 2;
        }
    }
    cin >> k;
    cin >> s;
    for(int i = 0; i < k; i++){
        if(s[i] == 'L'){
            s[i] = 0;
        }else if(s[i] == 'P'){
            s[i] = 3;
        }else if(s[i] == 'G'){
            s[i] = 1;
        }else{
            s[i] = 2;
        }
    }
    
    vector<int> horizontal, vertical;
    for(int i = 0; i < k; i++){
        if(s[i] % 3 == 0){
            //horizontal
            if(horizontal.size() == 0 or s[horizontal.back()] != s[i]){
                horizontal.push_back(i);
            }
        }else{
            //vertical
            if(vertical.size() == 0 or s[vertical.back()] != s[i]){
                vertical.push_back(i);
            }
        }
    }

    int not_deleted[horizontal.size() + vertical.size()];
    int pos = 0;
    int p1 = 0, p2 = 0;
    while(p1 < horizontal.size() and p2 < vertical.size()){
        if(horizontal[p1] < vertical[p2]){
            not_deleted[pos++] = horizontal[p1++];
        }else{
            not_deleted[pos++] = vertical[p2++];
        }
    }
    while(p1 < horizontal.size())
        not_deleted[pos++] = horizontal[p1++];
    while(p2 < vertical.size())
        not_deleted[pos++] = vertical[p2++];
    
    

    int curr_hash = hh(grid);
    //int ile_kurwa = 0;
    for(int i = 0; i < pos; i++){
        int move = s[not_deleted[i]];
        //if(i + 1 < pos and s[not_deleted[i]] + s[not_deleted[i + 1]] == 3 and not_deleted[i] != vertical[0] and not_deleted[i] != horizontal[0]) continue;
        if(G[move].find(curr_hash) == G[move].end()){
            //ile_kurwa++;
            int grid_after_move = new_grid(curr_hash, move);
            G[move][curr_hash] = grid_after_move;
            if((move % 3 == 0 and not_deleted[i] != horizontal[0]) or (move % 3 and not_deleted[i] != vertical[0])){
                G[3 - move][grid_after_move] = curr_hash;
            }
        }
        curr_hash = G[move][curr_hash];
    }
    grid = h[curr_hash];
    //cerr << ile_kurwa<< endl;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(grid[i * m + j] == 0)
                printf("B");
            if(grid[i * m + j] == 1)
                printf("C");
            if(grid[i * m + j] == 2)
                printf(".");
        }
        printf("\n");
    }
    return 0;
}