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 <algorithm>
#include <bits/stdc++.h>
#include <cstdint>
#include <deque>
#include <ios>
#include <iostream>
#include <iterator>
#include <limits>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

// Debugging functions from https://codeforces.com/blog/entry/68809 (only for
// debugging).

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)

void prelude() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
}

struct Board {

  string board;
  int n;
  int m;
  int coord(int i, int j) {
    return i*m + j;
  }
  void process(char instr) {
        if (instr == 'L') {
            for (int i = 0; i < n; ++i) {
                int flip = 0;
                for(int j = 0; j < m; ++j) {
                    if(board[coord(i, j )] != '.') {
                        if (flip != j) {
                            std::swap(board[coord(i,j)], board[coord(i, flip)]);
                        }
                        ++flip;
                    }
                }
            }
        } else if (instr == 'P') {
            for (int i = 0; i < n; ++i) {
                int flip = m-1;
                for(int j = m-1; j >= 0; --j) {
                    if(board[coord(i, j )] != '.') {
                        if (flip != j) {
                            std::swap(board[coord(i,j)], board[coord(i, flip)]);
                        }
                        --flip;
                    }
                }
            }
        } else if (instr == 'G') {
            for (int j = 0; j < m; ++j) {
                int flip = 0;
                for(int i = 0; i < n; ++i) {
                    if(board[coord(i, j)] != '.') {
                        if (flip != i) {
                            std::swap(board[coord(i,j)], board[coord(flip, j)]);
                        }
                        ++flip;
                    }
                }
            }
        } else if (instr == 'D') {
            for (int j = 0; j < m; ++j) {
                int flip = n-1;
                for(int i = n-1; i >= 0; --i) {
                    if(board[coord(i, j)] != '.') {
                        if (flip != i) {
                            std::swap(board[coord(i,j)], board[coord(flip, j)]);
                        }
                        --flip;
                    }
                }
            }
        }
  }
};

struct CacheEntry {
    CacheEntry(string b): board(std::move(b)) {
        instr[0] = instr[1] = instr[2] = instr[3] = '.';
    }
    const int get(char c) {
        for(int i = 0; i < 4; ++i) {
            if (instr[i] == c) {
                //debug(c, "at index", i, idx[i]);
                return idx[i];
            }
        }
        return -1;
    }
    void add(char c, int added_idx) {
        for(int i = 0; i < 4; ++i) {
            if(instr[i] == '.') {
                //debug("adding", c, added_idx);
                instr[i] = c;
                idx[i] = added_idx;
                return;
            }
        }
    }
    string board;
    char instr[4];
    int idx[4];

};

int main() {
  prelude();
  int n, m;
  cin >> n >> m;
  cin.ignore();
  string board_input;
  for (int i = 0; i < n; ++i) {
    string tmp;
    std::getline(cin, tmp);
    board_input += tmp;
  }
  Board b{.board = std::move(board_input), .n = n, .m = m};
  int k;
  cin >> k; 
  cin.ignore();
  string instructions;
  getline(cin, instructions);
  int new_key_idx = 0;
  int current_key_idx = 0;
  std::vector<CacheEntry> memo;
  std::unordered_map<string, int> rev_boards;
  memo.push_back(CacheEntry(b.board));
  //debug(memo[0].board);
  new_key_idx++;
  rev_boards[b.board] = 0;
  for (auto c: instructions)  {
    //debug(c);
    auto& entry = memo[current_key_idx];
    int next_idx_cached = entry.get(c);
    //debug(next_idx_cached);
    if (next_idx_cached == -1) {
        b.board = entry.board;
        //debug("new board connection from", b.board);
        b.process(c);
        //debug("resulting board", b.board);
        auto iter = rev_boards.find(b.board);
        if(iter != rev_boards.end()) {
            current_key_idx = iter->second;
            entry.add(c, current_key_idx);
            //debug("found existing board", current_key_idx);
        } else {
            current_key_idx = new_key_idx++;
            entry.add(c, current_key_idx);
            //debug("adding new board", current_key_idx, b.board);
            memo.push_back(CacheEntry(b.board));
            rev_boards[b.board] = current_key_idx;
        }
    } else {
        current_key_idx = next_idx_cached;
    }
  }
  //debug(rev_boards);
  //debug(current_key_idx,memo[current_key_idx].board);
  b.board = memo[current_key_idx].board;
  for(int i = 0; i < n; ++i) {
    for(int j = 0; j< m; ++j) {
        cout << b.board[b.coord(i, j)];
    }
    cout << "\n";
  }
  return 0;
}