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

using namespace std;

typedef vector<string> board_t;

void make_move(board_t& b, const char dir)
{
    int cols = b.front().size();
    int rows = b.size();

    if (dir == 'G')
    {
        for (int col = 0; col < cols; col++)
        {
            int free = 0, occupied = 0;
            while (occupied < rows && b[occupied][col] != '.') 
                occupied++, free++;

            while (true)
            {

                while (free < rows && b[free][col] != '.')
                    free++;

                while (occupied < rows && b[occupied][col] == '.')
                    occupied++;

                if (free < rows && occupied < rows)
                    swap(b[free][col], b[occupied][col]);
                else
                    break;

                free++, occupied++;
            }
        }
    }
    else if (dir == 'D')
    {
        for (int col = 0; col < cols; col++)
        {
            int free = rows-1, occupied = rows-1;
            while (occupied >= 0 && b[occupied][col] != '.') 
                occupied--, free--;

            while (true)
            {

                while (free >= 0 && b[free][col] != '.')
                    free--;

                while (occupied >= 0 && b[occupied][col] == '.')
                    occupied--;

                if (free >= 0 && occupied >= 0)
                    swap(b[free][col], b[occupied][col]);
                else
                    break;

                free--, occupied--;
            }
        }
    }
    else if (dir == 'L')
    {
        for (int row = 0; row < rows; row++)
        {
            int free = 0, occupied = 0;
            while (occupied < cols && b[row][occupied] != '.') 
                occupied++, free++;

            while (true)
            {

                while (free < cols && b[row][free] != '.')
                    free++;

                while (occupied < cols && b[row][occupied] == '.')
                    occupied++;

                if (free < cols && occupied < cols)
                    swap(b[row][free], b[row][occupied]);
                else
                    break;

                free++, occupied++;
            }
        }
    }
    else if (dir == 'P')
    {
        for (int row = 0; row < rows; row++)
        {
            int free = cols-1, occupied = cols-1;
            while (occupied >= 0 && b[row][occupied] != '.') 
                occupied--, free--;

            while (true)
            {

                while (free >= 0 && b[row][free] != '.')
                    free--;

                while (occupied >= 0 && b[row][occupied] == '.')
                    occupied--;

                if (free >= 0 && occupied >= 0)
                    swap(b[row][free], b[row][occupied]);
                else
                    break;

                free--, occupied--;
            }
        }
    }
}

void print(const board_t& b)
{
    for (const auto& e : b)
        cout << e << "\n";
}

inline bool check(const vector<char>& v, char c1, char c2)
{
    return v[v.size()-2] == c1 && v[v.size()-1] == c2; 
}

string moves_compression(const string& moves)
{
    vector<char> r;
    for (const auto& e : moves)
    {
        r.push_back(e);

        while (r.size() >= 2)
        {
            if (r[r.size()-2] == r[r.size()-1])
                r.pop_back();
            else if (check(r, 'G', 'D') || check(r, 'D', 'G') || check(r, 'P', 'L') || check(r, 'L', 'P'))
            {
                r[r.size()-2] = r.back();
                r.pop_back();
            }
            else if (r.size() >= 3 && r[r.size()-3] == r[r.size()-1])
                r.pop_back();
            else
                break;
        }
    }

    return string(r.begin(), r.end());
}

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

    int n, m; cin >> n >> m;
    board_t b(n);
    for (auto& e : b)
        cin >> e;
    
    int moves_size; cin >> moves_size;
    string moves; cin >> moves;

    if (moves.size() <= 10)
    {
        for (const auto& e : moves)
            make_move(b, e);
    }
    else
    {
        make_move(b, moves[0]);
        make_move(b, moves[1]);
        string moves_comp = moves_compression(moves.substr(2));
        make_move(b, moves_comp[0]);
        board_t b_ref = b;
        int cycle = -1;
        for (int i = 1; i < (int)moves_comp.size(); i++)
        {
            make_move(b, moves_comp[i]);
            if (b == b_ref && i % 4 == 0)
            {
                cycle = i; // cycle length
                break;
            }
        }

        if (cycle != -1)
        {
            int to_move = (moves_comp.size()-1) % cycle;
            for (int i = moves_comp.size()-1-to_move; i < (int)moves_comp.size(); i++)
                make_move(b, moves_comp[i]);
        }
    }

    print(b);
}