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
// #include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#include <iostream>
#include <vector>
#include <map>
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

void dots(int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << '.';
    }
}

int main()
{
    FAST int n, m;
    cin >> n >> m;
    // BOARD LOADING
    vector<map<int, char>> board(n);
    char tmp;
    for (int i = 0; i < n; i++)
    {
        map<int, char> xd;
        for (int j = 0; j < m; j++)
        {
            cin >> tmp;
            if (tmp != '.')
            {
                xd[j] = tmp;
            }
        }
        board[i] = xd;
    }

    int k;
    cin >> k;

    vector<char> processing;
    processing.reserve(k);
    cin >> tmp;
    processing.push_back(tmp);

    char last;
    char lastGD = 'x';
    char lastLP = 'x';

    for (int i = 1; i < k; i++)
    {
        last = tmp;
        cin >> tmp;

        if (tmp == 'G' || tmp == 'D')
        {
            if (lastGD == tmp)
                continue;

            lastGD = tmp;

            if (last == 'G' || last == 'D')
                processing[processing.size() - 1] = tmp;
            else
                processing.push_back(tmp);
        }
        else
        {
            if (lastLP == tmp)
                continue;

            lastLP = tmp;

            if (last == 'L' || last == 'P')
                processing[processing.size() - 1] = tmp;
            else
                processing.push_back(tmp);
        }
    }

    vector<int> redraw_row(m);
    for (int i = 0; i < processing.size(); i++)
    {
        switch (processing[i])
        {
        case 'G':
            for (int i = 0; i < m; i++)
                redraw_row[i] = 0;

            for (int i = 0; i < n; i++)
            {
                map<int, char> remap;
                for (auto const &[key, val] : board[i])
                {
                    if (redraw_row[key] == i)
                        remap[key] = val;
                    else
                        board[redraw_row[key]][key] = val;

                    redraw_row[key]++;
                }
                board[i] = remap;
            }
            break;
        case 'D':
            for (int i = 0; i < m; i++)
                redraw_row[i] = n - 1;

            for (int i = n - 1; i >= 0; i--)
            {
                map<int, char> remap;
                for (auto const &[key, val] : board[i])
                {
                    if (redraw_row[key] == i)
                        remap[key] = val;
                    else
                        board[redraw_row[key]][key] = val;

                    redraw_row[key]--;
                }
                board[i] = remap;
            }
            break;
        case 'L':
            for (int i = 0; i < n; i++)
            {
                map<int, char> remap;
                int idx = 0;
                for (auto const &[_, val] : board[i])
                {
                    remap[idx] = val;
                    idx++;
                }
                board[i] = remap;
            }
            break;
        case 'P':
            for (int i = 0; i < n; i++)
            {
                map<int, char> remap;
                int idx = m - 1;
                for (auto iter = board[i].rbegin(); iter != board[i].rend(); ++iter)
                {
                    remap[idx] = iter->second;
                    idx--;
                }
                board[i] = remap;
            }

            break;
        }
    }
    // PRINTING
    for (int i = 0; i < n; i++)
    {
        int lastSeen = -1;
        for (auto const &[key, val] : board[i])
        {
            dots(key - lastSeen - 1);
            cout << val;
            lastSeen = key;
        }
        dots(m - lastSeen - 1);
        cout << endl;
    }
}