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
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define FOR(i, l, r) for (auto i = (l); i <= (r); ++i)
#define REP(i, n) FOR (i, 0, (n)-1)
#define ssize(x) int(x.size())
template<class A, class B>
auto&
operator<<(ostream& o, pair<A, B> p)
{
  return o << "(" << p.first << ", " << p.second << ")";
}
template<class T>
auto
operator<<(ostream& o, T x) -> decltype(x.end(), o)
{
  o << "{";
  int i = 0;
  for (auto e : x) o << (", ") + 2 * !i++ << e;
  return o << "}";
}
#ifdef DEBUG
#define debug(x...)                                                            \
  cerr << "[" #x "]: ", [](auto... $) { ((cerr << $ << "; "), ...); }(x),      \
    cerr << '\n'
#define debugnl(x...)                                                          \
  cerr << "[" #x "]:\n", [](auto... $) { ((cerr << $ << ";\n"), ...); }(x),    \
    cerr << '\n'
#else
#define debug(...)                                                             \
  {                                                                            \
  }
#define debugnl(...)                                                           \
  {                                                                            \
  }
#endif

#define IF_HOR(ch) (ch == 'L' || ch == 'P')
#define IF_VER(ch) (ch == 'G' || ch == 'D')

enum class field_t
{
  none = '.',
  black = 'C',
  white = 'B'
};

int nrows, ncols;
string inst;
vector<vector<field_t>> board;


void input();
string opt_inst(string inst);
void apply_inst(char dir);
ostream& operator<<(ostream& o, vector<vector<field_t>>& board);

int
main()
{
  cin.tie(0)->sync_with_stdio(0);
  input();

  // Process each instruction
  for (auto dir : inst) apply_inst(dir);

  // Output
  cout << board;
  return 0;
}

void
apply_inst(char dir)
{
  if (IF_HOR(dir))
  {
    for (auto& row : board)
      if (dir == 'L')
      {
        int pos = 0;
        for (int col = 0; col < ncols; ++col)
          if (row[col] != field_t::none)
            row[pos++] = row[col];
        while (pos < ncols) row[pos++] = field_t::none;
      }
      else
      {
        int pos = ncols - 1;
        for (int col = ncols - 1; col >= 0; --col)
          if (row[col] != field_t::none)
            row[pos--] = row[col];
        while (pos >= 0) row[pos--] = field_t::none;
      }
  }
  else
  {
    REP (col, ncols)
      if (dir == 'G')
      {
        int pos = 0;
        for (int row = 0; row < nrows; ++row)
          if (board[row][col] != field_t::none)
            board[pos++][col] = board[row][col];
        while (pos < nrows) board[pos++][col] = field_t::none;
      }
      else
      {
        int pos = nrows - 1;
        for (int row = nrows - 1; row >= 0; --row)
          if (board[row][col] != field_t::none)
            board[pos--][col] = board[row][col];
        while (pos >= 0) board[pos--][col] = field_t::none;
      }
  }
  debug(dir);
  debugnl(board);
}

void
input()
{
  cin >> nrows >> ncols;
  board.resize(nrows, vector<field_t>(ncols));
  REP (r, nrows)
  {
    string row;
    cin >> row;
    REP (c, ncols)
      board[r][c] = (field_t)row[c];
  }
  int k;  // number of instructions, unused
  cin >> k >> inst;
  inst = opt_inst(inst);
  debug(inst.c_str());
}

string
opt_inst(string inst)
{
  // Make sure we will go through inst at most once
  int pos = 0;

  // Find the first char that we will surely use
  char first = inst[pos];
  while (pos + 1 < inst.length() && IF_VER(first) == IF_VER(inst[pos + 1]))
    pos++;
  first = inst[pos++];
  if (pos == inst.length())
    return {first};

  // Find the second char that we will surely use
  char second = inst[pos];
  while (pos < inst.length())
  {
    while (pos + 1 < inst.length() && IF_VER(second) == IF_VER(inst[pos + 1]))
      pos++;
    second = inst[pos++];
    if (pos == inst.length())
      return {first, second};

    // Find the third char and check whether it conflicts with first
    char third = inst[pos];
    while (pos + 1 < inst.length() && IF_VER(third) == IF_VER(inst[pos + 1]))
      pos++;
    third = inst[pos++];

    // Third does not conflict with first
    if (first != third)
    {
      pos--;
      break;
    }
  }

  // Complete instruction compression
  string opt = {first, second};
  for (; pos < inst.length(); ++pos)
  {
    if (inst[pos] == opt[opt.length() - 2])
      continue;
    if (IF_VER(inst[pos]) == IF_VER(opt[opt.length() - 1]))
    {
      opt[opt.length() - 1] = inst[pos];
      if (opt[opt.length() - 1] == opt[opt.length() - 3])
        opt.pop_back();
    }
    else
      opt += inst[pos];
  }
  return opt;
}

ostream&
operator<<(ostream& o, vector<vector<field_t>>& board)
{
  for (auto& row : board)
  {
    for (auto& field : row) o << (char)field;
    o << "\n";
  }
  return o;
}