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

using namespace std;

string simplify(string v) {
  string w = "";

  while (v != w) {
    char prev = v[0];

    w = "";

    for (int i = 1; i < v.size(); i++) {
      char cur = v[i];

      if (cur == 'G' || cur == 'D') {
        if (prev != 'G' && prev != 'D') {
          w += prev;
        }
      } else {
        if (prev != 'L' && prev != 'P') {
          w += prev;
        }
      }

      prev = cur;
    }

    w += v[v.size() - 1];

    v = "";

    char prevHor = '#';
    char prevVer = '#';

    for (char c : w) {
      if (c == 'G' || c == 'D') {
        if (c != prevVer) {
          v += c;
        }
        prevVer = c;
      } else {
        if (c != prevHor) {
          v += c;
        }
        prevHor = c;
      }
    }
  }

  return v;
}

struct Board {
  int n, m;
  vector<vector<char>> board;

  void input() {
    cin >> n >> m;

    for (int i = 0; i < n; i++) {
      vector<char> row;
      for (int j = 0; j < m; j++) {
        char c;
        cin >> c;
        row.push_back(c);
      }
      board.push_back(row);
    }
  }

  string to_string() {
    string s = "";
    for (auto row : board) {
      for (auto c : row)
        s += c;
      s += '\n';
    }
    return s;
  }

  void output() { cout << to_string(); }

  void L() {
    for (int i = 0; i < n; i++) {
      vector<char> row(m, '.');
      int id = 0;
      for (int j = 0; j < m; j++) {
        char cur = board[i][j];
        if (cur != '.') {
          row[id++] = cur;
        }
      }
      board[i] = row;
    }
  }

  void P() {
    for (int i = 0; i < n; i++) {
      vector<char> row(m, '.');
      int id = m - 1;
      for (int j = m - 1; j >= 0; j--) {
        char cur = board[i][j];
        if (cur != '.') {
          row[id--] = cur;
        }
      }
      board[i] = row;
    }
  }

  void G() {
    vector<vector<char>> rotBoard;
    for (int j = 0; j < m; j++) {
      vector<char> col(n, '.');
      int id = 0;
      for (int i = 0; i < n; i++) {
        char cur = board[i][j];
        if (cur != '.') {
          col[id++] = cur;
        }
      }
      rotBoard.push_back(col);
    }

    for (int i = 0; i < n; i++) {
      vector<char> row;
      for (int j = 0; j < m; j++) {
        row.push_back(rotBoard[j][i]);
      }
      board[i] = row;
    }
  }

  void D() {
    vector<vector<char>> rotBoard;
    for (int j = 0; j < m; j++) {
      vector<char> col(n, '.');
      int id = n - 1;
      for (int i = n - 1; i >= 0; i--) {
        char cur = board[i][j];
        if (cur != '.') {
          col[id--] = cur;
        }
      }
      rotBoard.push_back(col);
    }

    for (int i = 0; i < n; i++) {
      vector<char> row;
      for (int j = 0; j < m; j++) {
        row.push_back(rotBoard[j][i]);
      }
      board[i] = row;
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  Board board;

  board.input();

  int k;
  cin >> k;

  string s;
  cin >> s;

  string v = simplify(s);

  k = v.size();

  unordered_set<string> history;
  vector<string> memory;

  for (int i = 0; i < k; i++) {
    char c = v[i];
    if (c == 'G') {
      board.G();
    } else if (c == 'D') {
      board.D();
    } else if (c == 'L') {
      board.L();
    } else if (c == 'P') {
      board.P();
    }

    if (i > 0) {
      string m = board.to_string();
      if (history.find(m) != history.end()) {
        int t = (k - 1) % (i - 1);
        if (t == 0)
          cout << memory[i - 2];
        else
          cout << memory[t - 1];
        return 0;
      } else {
        history.insert(m);
        memory.push_back(m);
      }
    }
  }

  board.output();

  return 0;
}